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;


    }  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
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
              Set flag=1
Step 5: [end of loop]
Step 6: if flag=1
Print valfound in list at pos and update with new      number
[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.



typedef struct list
    int data;
    struct list *next;

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

 void main()

     int option;
     printf("\n Main Menu\n");
     printf("\n1:Creat list\n");
     printf("\n2:Display list\n");
     printf("\n4:update list\n");
     printf("\n1:Enter your option\n");
         case 1:start=creat(start);
         printf("\nlinked list created");
         case 2:start=display(start);

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


node *creat(node *start)
    node *ptr,*newnode;
    int num;
    printf("Enter data,pres 999 to exit:-");
         printf("\nEnter another data:-");
     return start;

  node*  display(node* start)
  node *ptr;
  return start;

    node* update(node* start)
     node *ptr;
     int updateNo,newNo;
     printf("Enter no to update:-");
     printf("\nEnter new note update\n");
     return start;

Post a Comment

Previous Post Next Post