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.

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