Tuesday, 30 August 2022

Single linked list using C programming advance data structure

IMPLEMENTATION OF SINGLY LINKED LIST AND ITS OPERATIONS

           



 TO LEARN C PROGRAMMING FOLLOW THIS YOUTUBE CHANNELCode with u - YouTube                                                                                                     

 AIM:

     To implement singly linked list and performing insert, search, display and delete operations. 

 ALGORITHM:

     1: CREATION:

                 a. Creating a node b. Reading details for a node from user c. Connect the node with the list 

                                                                    

    

 2. INSERTION:

                 a. Get the node using the structure node (), and read the node details using the new node().

                 b. Check if the list is empty or not 

                 c. FIRST: The binding field of the new node is made to point to the data field of the first node in the list by assigning the first node. 

                 d. The head pointer is made to point the data field of the new one by assigning the address of the new node.

                 e. LAST: The binding field of the last node is made to point to the data field of the first node in the list by assigning the new node. 

                f. The link field of the new node is set to NULL. 

                g. MIDDLE- the binding field of the new node is made to point to the data field of the next node in the list by assigning its address 

                h. The binding field of the next node is made to point to the data field of the new one by assigning the address of the new node. 

                                                                        


    

 3. DELETION:

                 a. Check whether the list is empty or not. 

                 b. FIRST – set the head pointer of the second node in the list 

                 c. Release the memory for the deleted node d. LAST- the link field of the previous node 

                 e. Release the memory for the deleted node 

                 f. MIDDLE- the link field of the previous node 

                 g. Release the memory for the deleted node 

  4. DISPLAY:

                 a. Get the contents from the list 

                 b. If the list is empty print it is empty. 

                 c. If the list is not empty print the entire list. 

PROGRAM : 

 #include<studio. h>

#include<con io. h>

#include<std lib. h>

 typedef struct node 

{

 int data;

 struct node *next;

 }node;

 node *create();

 node *insert b(node *head, int x);

 node *insert e(node *head, int x);

 node *insert in(node *head, int x);

 node *delete b(node *head);

 node *delete e(node *head); 

 node *delete in(node *head);

 void search(node *head);

 void print(node *head);

 int main() 

{

 int op,op1,x;

 node *head=NULL;

 do

 {

 print f("\n\n1)Create\n2)Insert\n3)Delete\n4)Search"); 

print f("\n5)Print\n6)Quit");

 print f("\n Enter your Choice:");

 scan f("%d", &op);

 switch(op)

 {

 case 1:head=create();

 break;

 case 2:printf("\n\t1)Beginning\n\t2)End\n\t3)In between");

 print f("\n Enter your choice : ");

 scan f("%d",&op1);

 print f("\n Enter the data to be inserted : ");

 scan f("%d", &x);

 switch(op1)

 {

 case 1: head=insert b(head, x);

 break; 

case 2: head=insert e(head, x);

 break;

 case 3: head=insert in(head, x);

 break; 

}

 break;

 case 3:printf("\n\t1)Beginning\n\t2)End\n\t3)In between");

 print f("\n Enter your choice : ");

 scan f("%d",&op1);

 switch(op1)

 {

 case 1:head=delete b(head);

 break;

 case 2:head=delete e(head); 

break;

 case 3:head=delete in(head);

 break;

 }

 break;

 case 4:search(head);

 break; 

case 5:print(head);

 break; 

}

 }

while(op<6);

 }

 node *create() 

{

 node *head,*p;

 int I, n; head=NULL;

 print f("\n Enter no of data:");

 scan f("%d", &n);

 print f("\n Enter the data:"); 

for(I=0;

index=(node*)malloc(size of(node)); 

p=p->next;

 }

 p->next=NULL;

 scan f("%d",&(p->data));

 }

 return(head);

 }

 node *insert b(node *head, int x)

 { 

node *p;

 p=(node*)malloc(size of(node));

 p->data=x;

 p->next=head; 

head=p; 

return(head); 

}

 node *insert e(node *head, int x)

 {

 node *p,*q; p=(node*)malloc(size of(node));

 p->data=x;

 p->next=NULL; 

if(head==NULL) 

return(p); //locate the last node 

for(q=head ;q->next!=NULL ;q=q->next) ;

 q->next=p; return(head);

 }

 node *insert in(node *head, int x)

 { 

node *p,*q;

 int y;

 p=(node*)malloc(size of(node));

 p->data=x;

 p->next=NULL;

 print f("\n Insert after which number ? : ");

 scan f("%d", &y); //locate the data 'y' 

for(q=head ; q != NULL && q->data != y ; q=q->next) ;

 if(q!=NULL)

 {

 p->next=q->next; 

q->next=p;

 }

 else

 print f("\n Data not found ");

 return(head);

 }

 node *delete b(node *head)

 {

 node *p,*q; 

if(head==NULL)

 {

 print f("\n Underflow....Empty Linked List");

 return(head);

 }

 p=head;

 head=head->next;

 free(p);

 return(head);

 }

 node *delete e(node *head)

 {

 node *p,*q; 

if(head==NULL) 

{

 print f("\n Underflow....Empty Linked List");

 return(head);

 }

 p=head;

 if(head->next==NULL) 

{

 // Delete the only element 

head=NULL; 

free(p);

 return(head);

 } 

//Locate the last but one node 

for(q=head ;q->next->next !=NULL ;q=q->next) ;

 p=q->next;

 q->next=NULL;

 free(p);

 return(head);

 }

 node *delete in(node *head)

 {

 node *p,*q; int x, I;

 if(head==NULL)

 {

 print f("\n Underflow....Empty Linked List");

 return(head); 

}

 print f("\n Enter the data to be deleted : "); 

scan f("%d", &x);

 if(head->data==x) 

{

 // Delete the first element 

p=head; 

head=head->next;

 free(p);

 return(head);

 }

 //Locate the node previous to one to be deleted

 for(q=head ;q->next->data!=x && q->next !=NULL ;q=q->next ) ;

 if(q->next==NULL)

 {

 print f("\n Underflow.....data not found"); 

return(head);

 }

 p=q->next; 

q->next=q->next->next;

 free(p);

 return(head);

 }

 void search(node *head)

 {

 node *p;

 int data, loc=1;

 print f("\n Enter the data to be searched: ");

 scan f("%d", &data);

 p=head; 

while(p!=NULL && p->data != data)

 {

 loc++;

 p=p->next; 

}

 if(p==NULL) 

print f("\n Not found:");

 else 

print f("\n Found at location=%d", loc);

 }

 void print(node *head)

 {

 node *p;

 print f("\n\n");

 for(p=head ;p!=NULL ;p=p->next) 

print f("%d ",p->data);

 }

 OUTPUT :

 1)create

 2)Insert 

3)Delete

 4)Search

 5)Reverse 

6)Print 

7)Quit 

Enter your Choice:1

 Enter no of data:5

 Enter the data:1 4 7 2 6 


 OUTPUT :

 1)create

 2)Insert 

3)Delete

 4)Search

 5)Reverse 

6)Print 

7)Quit

 Enter your Choice:6 

1 4 7 2 6 


 OUTPUT :

 1)create

 2)Insert 

3)Delete

 4)Search

 5)Reverse 

6)Print 

7)Quit

 Enter your Choice:

5 6 2 7 4 1


 OUTPUT :

 1)create

 2)Insert 

3)Delete

 4)Search

 5)Reverse 

6)Print 

7)Quit

 Enter your Choice:7 


RESULT: 

             Thus the program for singly linked List and its operations was implemented and it’s executed successfully.

No comments:

Post a Comment

FRIENDSHIP & GOALS

WHAT IS FRIENDSHIP:  For all the attention we pay to love stories, some of the most compelling stories (in fiction or not) are about best fr...