Pages

Create a double link list which have functionality of insertion, deletion , searching and traversing

Thursday 6 June 2013

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
       int data;
       struct node *forw, *backw;
};
struct node *n, *first, *last, *this1, *dlt, *pre;
int main()
{
    char ch;
    int item, position;
    first=last=NULL;
    printf("Enter q to quit: ");
    while((ch=getche())!='q')
    {
          n=(struct node *)malloc(sizeof(struct node));
          printf("\nEnter data in node: ");
          scanf("%d", &n->data);
          n->forw=NULL;
          n->backw=NULL;
          if(first==NULL)
          {
                  first=n;
                  last=n;
          }
          else
          {
                  last->forw=n;
                  n->backw=last;
                  last=n;
          }
          printf("Enter q to quit: ");
    }
    // treversing
    printf("\n");
    this1=first;
    while(this1!=NULL)
    {
           printf("%d\n", this1->data);
           this1=this1->forw;
    }
    //searching
    printf("Enter number which you want to find in list: ");
    scanf("%d", &item);
    this1=first;
    while(this1!=NULL && item != this1->data)
    {
           this1=this1->forw;
    }
    if(this1==NULL)
    printf("Number is not found.\n");
    else
    printf("Number is found.\n");
    // deletion
    printf("Enter number which you want to delete in list: ");
    scanf("%d", &item);
    this1=first;
if(item==this1->data)
{
            first=this1->forw;
            this1->forw->backw=NULL;
            free(this1);
}
else if(item==last->data)
{
     dlt=last;
     last->backw->forw=NULL;
     free(dlt);
}
else
{
    while(this1!=NULL && item != this1->data)
    {
           this1=this1->forw;
    }
    if(this1==NULL)
    printf("Number is not found.");
    else
    {
           this1->backw->forw=this1->forw;
           this1->forw->backw=this1->backw;
           free(this1);
    }
}
    printf("\nTreversing after deletion.\n");
    this1=first;
    while(this1!=NULL)
    {
           printf("%d\n", this1->data);
           this1=this1->forw;
    }
    //insertion--------------
    printf("Enter position where you want to enter element: ");
    scanf("%d", &position);
if(first->data==position)
{
    n=(struct node *)malloc(sizeof(struct node));
    printf("Enter data in new node: ");
    scanf("%d", &n->data);
    n->backw=NULL;
    n->forw=first;
    first=n;
}
    else
{
    this1=first;
    while(this1!=NULL && position!=this1->data)
    {
           pre=this1;
           this1=this1->forw;
    }
    if(this1==NULL)
    printf("Number is not found.");
    else
    {
           n=(struct node *)malloc(sizeof(struct node));
           printf("Enter data in new node: ");
           scanf("%d", &n->data);
           n->backw=pre;
           n->forw=pre->forw;
           pre->forw=n;
    }
}
// treversing
    printf("\n");
    this1=first;
    while(this1!=NULL)
    {
           printf("%d\n", this1->data);
           this1=this1->forw;
    }
    getch();
}
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *