UPDATE A LINKED LIST VALUE IN A DATA           STRUCTURE



#include<stdio.h>
#include<conio.h>

typedef struct list
  {
    int data;
    struct list *next;
  }node;

node *start=NULL;
node *creat(node*);
node *display(node*);
node *update(node*);

 void main()
   {

     int option;
     clrscr();
     do
       {
     printf("\n Main Menu\n");
     printf("\n1:Creat list\n");
     printf("\n2:Display list\n");
     printf("\n4:update list\n");
     printf("\n0:Exit\n");
     printf("\n1:Enter your option\n");
     scanf("%d",&option);
     switch(option)
       {
         case 1:start=creat(start);
         printf("\nlinked list created");
         break;
         case 2:start=display(start);
         break;

         case 4:start=update(start);
         printf("\nlinked list updated");
         break;
         case 0:
         exit(0);
       }
       }

   while(option!=0);
   getch();
 }


node *creat(node *start)
  {
    node *ptr,*newnode;
    int num;
    printf("Enter data,pres 999 to exit:-");
    scanf("%d",&num);
    while(num!=999)
      {
     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("\nEnter another data:-");
         scanf("%d",&num);
       }
     return start;
      }


  node*  display(node* start)
  {
  node *ptr;
  ptr=start;
  while(ptr!=NULL)
  {
  printf("\%d->",ptr->data);
  ptr=ptr->next;
  }
  return start;
  }



    node* update(node* start)
   {
     node *ptr;
     int updateNo,newNo;
     ptr=start;
     printf("Enter no to update:-");
     scanf("%d",&updateNo);
     printf("\nEnter new note update\n");
     scanf("%d",&newNo);
     while(ptr!=NULL)
       {
     if(ptr->data==updateNo)
       {
         ptr->data=newNo;
       }
     ptr=ptr->next;
       }
     return start;
   }

Post a Comment

Previous Post Next Post