Tuesday, 30 August 2022

Doubly linked list using C programming

 IMPLEMENTATION OF DOUBLY LINKED LIST AND ITS OPERATIONS 

TO LEARN C PROGRAMMING FOLLOW THIS YOUTUBE CHANNEL : Code with u - YouTube

AIM:

     To implement doubly linked list and performing insert, search, view 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 struct node(), and read the details of the node using new node() 

             b. Check whether the list is empty or not 

             c. FIRST- The forward link field of the new node is made to point the data field of the first node in the list by assigning of the first node.

             d. The backward link field of the first node is made to point the data field of the first node in the list by assigning of the new node. 

             e. Assigning the new node as the head pointer

             f. LAST – the forward link field of the last node is made to point the new node by assigning the address of the new node.

             g. The backward link field of the new node is made to point the last node in the list by assigning the address of the new node.

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

             I. MIDDLE- the forward link field of the new node is made to point the next node in the list by assigning its address

             j. The backward link field of the next node is made to point the new node, by assigning the address of the preceding node

             k. The forward link field of the preceding node is made to point the new node, by assigning the address of the new node.

     3. 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. 

     4. FIND

             a. It finds the address of the given element and returns the address of the particular element.

             b. Returns null if the element is not found.

     5. DELETION

                                                                        



             a. Check whether the list is empty or not.

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

             c. Set the backward link filed of the head in the list to NULL

             d. Release the memory for the deleted node

             e. LAST- The link field of the previous node

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

             g. Release the memory for the deleted node

             h. MIDDLE- The forward link field of the previous node is made to point the previous node, by assigning its address 

             I. Release the memory for the deleted node.

                                                                                



NOTE : 

        In the below content incase of some error problem the program was slightly modified, so to remodel it the following steps should be taken 

            1.)In "d node" remove the space between "d" and "node".

            2.)In "print f" remove the space between "print" and "f".

            3.)In "scan f" remove the space between "scan" and "f".

 PROGRAM :

 #include<std io. h>

  #include<con io. h>

#include<std lib. h>

typedef struct d node

 {

 int data;

 struct d node *next,*pre v;

 }d node;

 d node * create()

 {

 int x, n, I;

 d node *head=NULL;

 d node *p;

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

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

 for(I=0;I<n; I++)

{

print f("\n Next Data : ");

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

if(head==NULL)//insert the first node

{

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

p->next=p->p rev=NULL; 

p->data=x;

 }

 else

 {

 p->next=(d node *) malloc(size of(d node));

 p->next->data=x;

 p->next->p rev=p;

 p=p->next;

 p->next=NULL;

 }

 }

 return(head);

 } 

void print(d node *head)

 {

 d node *p;

 print f("\n Data stored in the Doubly linked list : ");

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

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

 }

 int search(d node *head, int x)

 {

 int I=0;

 d node *p; 

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

{

 if(p->data == x) 

return(I);

 I++; 

}

 return -1;

 }

 d node *insert(d node *head, int x, int loc)

 {

 d node *p,*q;

 int I;

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

 p->data=x;

 p->next=p->pre v=NULL;

 if(loc==1) // inserting as a first node 

{

 p->next=head;

 head->pre v=p;

 head=p;

 }

 else

 {

 q=head;

 for(I=1; I<loc-1;I++)

if(q->next!=NULL) 

q=q->next;

 else

 { 

print f("\n Overflow **** "); 

return head;

 } //insert a node as next node of q

 p->next=q->next;

 p->pre v=q;

 q->next=p;

 q->next->pre v=q;

 }

 return(head);

 }

 d node *Delete(d node *head, int loc) 

{

 d node *p,*q;

 int I;

 if(loc==1) //Deleting the first node 

{

 p=head;

 head=head->next;

 head->pre v=NULL; 

free(p);

 }

 else

 {

 q=head;

 for(I=1;I<loc; I++)//position q on the node to be deleted

if(q->next==NULL)

 {

 print f("\n Underflow *****");

 return(head);

 }

 else 

q=q->next; 

}

 if(q->next != NULL) 

q->next->pre v=q->pre v;

 q->pre v->next=q->next;

 free(q);

 }

 return(head);

 }

 int main() 

{

 int op, x, loc;

 d node *head=NULL; 

do

 {

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

 print f("\n6)Quit");

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

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

 switch(op)

 { 

case 1: head=create();

break; 

case 2: print(head);

break; 

case 3: print f("Enter the location :"); 

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

 print f("Enter the data :");

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

 head=insert(head, x, loc);

break;

 case 4: print f("Enter the location :");

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

head=Delete(head, loc);

break; 

case 5: print f("Enter element to be searched : "); 

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

 loc=search(head, x);

 if(loc==-1) 

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

 else

 print f("\n Found at location :%d ",loc+1);

 break;

 }

 }

while(op<6);

 }

 OUTPUT : 

1)Create

 2)Print

 3)Insert 

4)Delete 

5)Search 

6)Quit 

Enter your Choice : 1 

Enter no of data : 3 

Next Data : 3

 Next Data : 4

 Next Data : 5 

1)Create

 2)Print

 3)Insert 

4)Delete 

5)Search 

6)Quit 

 Enter your Choice : 2

 Data stored in the Doubly linked list : 3 4 5 


1)Create

 2)Print

 3)Insert 

4)Delete 

5)Search 

6)Quit 

Enter your Choice : 4

 Enter the location :2


1)Create

 2)Print

 3)Insert 

4)Delete 

5)Search 

6)Quit 

Enter your Choice : 2 

Data stored in the Doubly linked list : 3 5


1)Create

 2)Print

 3)Insert 

4)Delete 

5)Search 

6)Quit 

 Enter your Choice : 3

 Enter the location :55

 Enter the data :2

 Overflow **** 

1)Create

 2)Print

 3)Insert 

4)Delete 

5)Search 

6)Quit  

Enter your Choice : 5

 Enter element to be searched : 3

 Found at location :1

1)Create

 2)Print

 3)Insert 

4)Delete 

5)Search 

6)Quit 

 Enter your Choice : 6

 RESULT:

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

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.

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...