Showing posts with label Link List. Show all posts

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

#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();
}

Create single link list and insert a new node in link list.

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
struct node
{
       int data;
       struct node *link;
};
struct node *n, *first, *this1, *pre;
int main()
{
    char ch;
    int position;
    first=NULL;
    printf("Enter q to quit: ");
    ch=getche();
    while(ch!='q')
    {
           n=(struct node *)malloc(sizeof(struct node));
           printf("\nEnter data to store in node: ");
           scanf("%d", &n->data);
           n->link=NULL;
           if(first==NULL)
           {
                  first=n;
           }
           else
           {
                  this1=first;
                  while(this1->link!=NULL)
                  {
                          this1=this1->link;
                  }
                  this1->link=n;
           }
           printf("Press q to quit: ");
           ch=getche();
    }
    printf("\n");
    this1=first;
           while(this1!=NULL)
           {
                   printf("%d %d\n", this1->data, this1->link);
                   this1=this1->link;
           }
           //insertion
    printf("where you want to enter number: ");
    scanf("%d", &position);
if(first->data==position)
{
             n=(struct node *)malloc(sizeof(struct node));
             printf("Enter number: ");
             scanf("%d", &n->data);
             n->link=first;
             first=n;
}
else
{
    this1=first;
    while(this1!=NULL && position!=this1->data)
    {
             pre=this1;
             this1=this1->link;
    }
    if(this1==NULL)
    printf("Number is not found.");
    else
    {
       
             n=(struct node *)malloc(sizeof(struct node));
             printf("Enter number: ");
             scanf("%d", &n->data);
             n->link=this1;
             pre->link=n;
    }
}
    //treversing after insertion
    this1=first;
           while(this1!=NULL)
           {
                   printf("%d\n", this1->data);
                   this1=this1->link;
           }
    getch();
   
}