BASIC OPERATIONS IN LINKED LIST



#include<conio.h>

#include<stdio.h>

 

 

typedef struct list

{

int data;

struct list *next;

} node;

 

   node *start=NULL;

   node *create(node*);

   void display(node*);

    node*insertbeg(node*);

    node*update(node*);

   node* sortlist(node*);

   int countnode(node*);

   node*updatepos(node*);

   node* search(node*);

   node* searchpos(node*);

   node* insertafter(node*);

    node*deleteafter(node*);

    node*deletenode(node*);

    node*insertend(node*);

    node*insertbefore(node*);

 

  void  main()

    {

    node *ptr=NULL;

    int option;

    int c;

 

    clrscr();

 

 

    do

    {

     printf("\n main menu\n");

     printf(" 1: create list\n");

     printf(" 2: display list\n");

     printf(" 3: sort list\n");

     printf(" 4: update list\n");

     printf(" 5: count in list\n");

    printf(" 6: update list by position\n");

     printf(" 7: search a value in list\n");

     printf(" 8: search list by its position\n");

     printf(" 9: insert at beg   position\n");

     printf("10: insert node at end\n");

     printf("11: insert before specified position\n");

     printf("12: insert after specified position\n");

     printf("13: delete after specified position\n");

     printf("14: delete node");

     printf("0: exit \n");

 

     printf("\n enter your option");

     scanf("%d",&option);

 

    switch(option)

    {

 

    case 1: start =create(start);

     printf("\n linked list created->");

 

    break;

 

    case 2: display(start);

    break;

    case 3: start=sortlist(start);

     printf("\n list sorted ,call display() to check  ");

    break;

    case 4:start=update(start);

     printf("\n list updated ,call display() to check  ");

    break;

    case 5: c=countnode(start);

     printf("\n there are %d nodes in list\n",c);

    break;

    case 6: start=updatepos(start);

     printf("\n value of node update on its given position  ");

    break;

    case 7:start=search(start);

     printf("\n search operation preformed  ");

    break;

    case 8:start=searchpos(start);

     printf("\n search operation preformed  ");

    break;

    case 9:start=insertbeg(start);

     printf("\n node insert at begining");

    break;

    case 10:start=insertend(start);

 

     printf("\n node inserted at end");

    break;

    case 11: start=insertbefore(start);

     printf("\n node inserted before specifieed value:-");

    break;

    case 12: start=insertafter(start);

     printf("\n node isnertede after specified");

    break;

    case 13: start =deleteafter(start);

     printf("\n node deleted after specified value\n");

    break;

    case 14: start=deletenode(start);

    printf("\n specified node deleted");

 

    case 0:

    exit (0);

    }

}while(option !=0);

 

    getch();

    }

 

 

////////////////////////////////1

 

    node *create(node*start)

    {

     node*ptr,*newnode;

    int num;

 

     printf("enter dataa,press 99 to exit\n");

     scanf("%d",&num);

     while(num!=99)

    {

 

     newnode=(node*)malloc(sizeof(node));

 

     newnode->data=num;

     if(start==NULL)

     {

      newnode->next=NULL;

     start =newnode;

     }

     else

     {

     ptr=start;

      while(ptr->next!=NULL)

      ptr=ptr->next;

      ptr->next=newnode;

      newnode->next=NULL;

    }

      printf("\n enter another data");

      scanf("%d",&num);

     }

      return(start);

}

 

 

      ///////////////////////////////////////2

 

 

     void display(node *start)

     {

      node*ptr=NULL;

     ptr=start;

 

      while(ptr!=NULL)

     {

      printf("\t%d->",ptr->data);

 

      ptr=ptr->next;

 

     }

 

    // return(start);

     }

                  /////////////////////////////////////////////3

 

    node *sortlist(node *start)

     {

     int temp;

     node *ptr1,*ptr2 ;

     ptr1=start;

 

     while (ptr1!=NULL)

     {

      ptr2=ptr1->next ;

 

      while(ptr2!=NULL)

     {

      if((ptr1->data)>(ptr2->data))

     {

      temp=ptr1->data;

      ptr1->data=ptr2->data;

      ptr2->data=temp;

     }

 

      ptr2=ptr2->next;

     }

      ptr1=ptr1->next;

     }

     return start;

     }

       /////////////////////////////////////////////4

 

node *update(node *start)

     {

     int temp,no;

     node *ptr ;

     ptr=start;

      printf("enter value to be updated and new value also:-");

      scanf("%d%d",&temp,&no);

 

     while (ptr!=NULL)

     {

      if(ptr->data==temp)

 

      ptr->data=no;

 

       ptr=ptr->next;

     }

     return start;

     }

               ////////////////////////////////////////////5

 

     int countnode(node *start)

     {

     node *ptr;

     int x=0;

     ptr=start;

 

 

     while (ptr!=NULL)

     {

     x++;

      ptr=ptr->next;

     }

     return x;

     }

     /////////////////////////////////////////////6

 

     node* updatepos(node *start)

     {

     node *ptr;

     int x,c=0,no;

     ptr=start;

      printf("\n enter position & update option:-");

      scanf("%d%d",&x,&no);

 

 while(ptr->next!=NULL)

     {

     ++c;

     if(c==x)

 

      ptr->data=no;

      ptr=ptr->next;

    }

     return start;

 

      }

 

       /////////////////////////////////////////////////////7

 

      node *search(node*start)

      {

      node *ptr ;

      int no,c=0,flag=0;

      ptr=start;

       printf("\n enter no to search ");

       scanf("%d",&no);

 

       while(ptr!=NULL)

      {

      c++;

       if(ptr->data==no)

      {

      flag=1;

       printf("\n%d found on %dth position",no,c);

      }

       ptr=ptr->next;

      }

       if(flag==0)

      {

       printf("\n %d not found in list",no);

       }

      return start;

      }

                //////////////////////////////////////////////////////8

 

      node *searchpos(node*start)

      {

      node *ptr ;

      int no,c=0,flag=0;

      ptr=start;

       printf("\n enter position search ");

       scanf("%d",&no);

 

       while(ptr!=NULL)

      {

      c++;

      if(c==no)

      {

      flag=1;

       printf("\n%d found on %dth position",ptr->data,no);

      }

       ptr=ptr->next;

      }

       if(flag==0)

      {

       printf("\n %d positon not exist in list",no);

       }

      return start;

      }

 

 

                ///////////////////////////////////////////////////9

 

       node*insertbeg(node*start)

      {

      node  *newnode;

      int num;

 

      newnode=(node*)malloc(sizeof(node));

       printf("enter num");

       scanf("%d",&num);

       newnode->data=num;

       newnode->next=start;

       start=newnode;

 

      return (start);

      }

 

 

 

       ///////////////////////////////////////////////10

       node*insertend(node*start)

    {

 

 

      node*newnode,*ptr;

     int num;

 

      newnode=(node*)malloc(sizeof(node));

      printf("enter num");

      scanf("%d",&num);

      newnode->data=num;

      newnode->next=NULL ;

     ptr=start;

      while(ptr->next!=NULL&&ptr!=NULL)

 

      ptr=ptr->next;

      ptr->next=newnode;

 

     return start;

     }

 

 

      ////////////////////////////////////////////////////////////11

 

      node*insertbefore(node*start)

     {

     node *preptr,*ptr,*newnode ;

     int num ,value ;

      newnode=(node*)malloc(sizeof(node));

      printf("enter num");

      scanf("%d",&num);

      newnode->data=num;

      printf("enter value before which node  inserted:-");

      scanf("%d",&value);

     ptr=start;

      while(ptr->data!=value)

     {

     preptr=ptr;

      ptr=ptr->next;

     }

      preptr=newnode;

      newnode->next=ptr;

     return start;

     }

 

 

 

 

                 //////////////////////////////////////////12

 

      node*insertafter(node*start)

     {

     node *newnode , *preptr,*ptr ;

     int num ,value ;

      newnode=(node*)malloc(sizeof(node));

      printf("enter value ");

      scanf("%d",&num);

      newnode->data=num;

      printf("enter value after which node  inserted:-");

      scanf("%d",&value);

     ptr=start;

     preptr=ptr;

 

      while(preptr->data!=value)

     {

    preptr= ptr;

     ptr=ptr->next;

 

 

     }

      preptr->next=newnode;

      newnode->next=ptr;

     return start;

               }

 

 

 

      //////////////////////////////////////////////////////13

 

 

               node*deleteafter(node *start)

               {

               node *ptr,*preptr;

               int val;

               ptr=start;

               preptr=ptr;

               printf("enter value after which node to be deleted");

 

               scanf("%d",val);

 

 

               while(preptr!=NULL)

               {

               if(preptr->data==val)

               {

               preptr=ptr;

               preptr=ptr->next ;

 

               }

               preptr->next=ptr->next;

               }

               free(preptr);

               return(start);

               }

 

        /////////////////////////////////////////////////14

 

               node*deletenode(node*start)

               {

               node *ptr,*preptr;

               int val;

               ptr=start;

               preptr=ptr;

 

               printf("enter value for node which to be deleted");

               scanf("%d",&val);

               while(ptr->data!=val)

               {

               preptr=ptr;

               ptr=ptr->next;

 

               preptr->next=ptr->next;

               }

               free(ptr);

               return(start);

               }

 

 

 

Post a Comment

Previous Post Next Post