Program to update a node in a linked list. 



Data Structure :- 

 It is a critical part of data management. A data structure is a basically a group of data elements that are put together under one name and which defines a particular way of storing and organizing data in a computer, so that it can be used efficiently.
  

                   In other words we can say that , Data structure is a representation of the logical relationship existing between individual elements of data and it is the way of organizing all data item that consider not only the element stored but also there relationships to each other.


Classification of Data Structure:-

Two classes of data structure are use in data structuring:-
·         
   Primitive data structure
·         
   Non Primitive data structure
\
Primitive data structure :- 

Primitive data structure are integer, real and  character data type.

Non primitive data structure:-


Non primitive data structure are of two type:-

·        Linear data structure
·        Non linear data structure

Linear data structure :-

Linear data structure are as follows:-
·        Array
·        Linked list
·        Stack
·        Queue

Non linear data structure :-

Non linear data structure are as follows :-
·        Tree
·        Graph










 Linked list:-

Linked list are the special type of data element usually structure that contain a reference to the data of its same type. So it is called self referential structure. In addition to another data linked list contains a pointer to a data that is the same type of as that of the structure.
With the help of this pointer data element links to one another.

Struct link_list
    {
Int data;

Structlink_list*next;

    }  L;

Types of linked list:-

Their are various type of linked list:-
·         

      Singly linked list
·        Doubly linked list
·        Circular singly linked list
·        Circular doubly linked list

This program is based on singly linked list.

Singly linked list:-

A singly linked list is the simple type of list in which every node contains two parts first is data and second is a pointer to the next node. A singly linked list allows traversal of data only in one way.

The first node of the list is pointed by a pointer which is usually a head or start pointer. The last node of the list contains NULL value in its pointed field.

Struct linked_list
    {
Int data;
Struct linked_list*next;
   } node*

Algorithm to create a linked list


Step 1: [initialize] start=NULL
Step 2: set ptr=start
Step 3:repeat step 4 to 6 while(ptr->! =NULL)
Step 4: allocate memory for ptr
Ptr=(node*)malloc(sizeof(node))
Start=ptr
Step 5: take value for ptr->data=val
Step 6: set ptr=ptr->next
Step 7: end of loop
Step 8: exit

Algorithm to display a linked list :-

Step 1: [initialize] start=NULL, ptr=NULL
Step 2: ptr=start
Step 3: repeat step 4 to 5 while(ptr! =NULL)
Step 4: print ptr->data
Step 5: set ptr=ptr->next
Step 6: end of loop
Step 7: exit

Algorithm to updatea node in the linked list :-

Step 1: [initialize] set *start=NULL, *ptr=NULL, *newnode, take upnp, flag=1, pos=0, newno
Step 2: ptr=start
Step 3:take value to update in the upno and take newno for upno
Step 4: repeat step 4 and 5 while(ptr!=NULL)
Set pos=pos+1
              If(ptr->data==upno)
              Set flag=1
             Else
Ptr=ptr->next
Step 5: [end of loop]
Step 6: if flag=1
Print valfound in list at pos and update with new      number
             Else
Ptr->data=newno
[end of if]
            Print val not found in list

Step 7:exit


In the program we have to first write the header file of C language that is #include<stdio.h>, #include<conio.h>. The we declare structure of list by writing keyword typedef struct list. Which contain two variables one is data of int type to store value and second is next of struct list type to store the adders of next node. 

Then we write function prototype :-
·        Create of void type to  create a linked list
·        Display of void type to display the list
·        Update of void type to delete a node at the beginning of the list

Then we start our main() which is known as driver of all the functions. In that we firstly take a variable option of int type to for the switch, then our loop starts do in that we firstly print massage for user like enter 1 to create list, 2 to display list, 3 to update a number at the specific position and 0 to exit. Then the user will give her choice and the switch starts case 1 is to create a list, case 2  is to display a list, case 3 is to upd  str and case 0 is to exit. Then our loop ends by writing keyword while(option! =0)  and bracket get closed.
Then we starts writing the functions definition.
·    
    Void create() : 

      to create a list
We first give our first element in the data part  and put NULL at the next part. Then we start malloc function to take more memory for the new nodes and put that address in the previous node next part, though which they are linked to each other until we 999 as a number then it stops taking number and get exit.
·         
  Void display() : 
       to display a list
We initialize ptr=start and starts loop while(ptr! =NULL) and print ptr->data.
·         
  Void update:  
      to update a number at the specific position.
We first initialize node *ptr,int update no, new no, ptr=start. Then we print a message enter no to update and the we enter new number to be update. After that we start a loop till while(ptr! =NULL) then we check the given number is in the list or not if(ptr->data==update no) if present then update the new number with the old one ptr->data=newno. The loop will increase by the condition ptr=ptr->next. Through this old  element will Update by the new element.









 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