Write a C program to implement the operations on a Doubly linked list. The program should include the following operations.
1)
Creation of list
2)
Traversal of list
a) Forward traversal
b) Reverse traversal
3)
Insertion of element
a) At the beginning of a list
b) At the end of a list
c) Before a given node
d) After a given node
e) At a given position.
4)
Deletion of a node
a) First node
b) Last node
c) Node with a particular value
d) Node at a given position.
**CODING**
Source Code:
- #include<stdio.h>
- #include<stdlib.h>
- #include<malloc.h>
- //definition of the basic element of the linked list, node
- struct node
- {
- int data;
- struct node * prev;
- struct node * next;
- }*head, *end;
- //Prototype definitions of the functions
- void createList(int n);
- void displayList();
- void revdisplaylist();
- void insertAtBeginning(int data);
- void insertAtEnd(int data);
- void insertAtBN(int data, int position);
- void DlListDeleteFirstNode();
- void DlListDeleteendNode();
- void DlListDeleteAnyNode(int pos);
- int main()
- {
- //Declaration and initialization of variables
- int n, data, choice=1, a,insPlc;
- //Initializing head and end pointers to NULL
- head = NULL;
- end = NULL;
- //Creation of the linked list
- printf("\n CREATE A DOUBLY LINKED LIST ");
- printf("\n-----------------------------------------\n");
- printf("\n Enter the total number of nodes: ");
- scanf("%d",&n);
- createList(n);
- displayList();
- revdisplaylist();
- //Repeat the following steps until user wants to exit the program
- while(choice != 7)
- {
- //Displaying the menu
- printf("\n DOUBLY LINKED LIST PROGRAM\n");
- printf("-----------------------------------------\n");
- printf("1. Insert node at beginning\n");
- printf("2. Insert node at end\n");
- printf("3. Insert node at a particular position\n");
- printf("4. Delete the first node\n");
- printf("5. Delete the end node\n");
- printf("6. Delete the node at a particular position \n");
- printf("7. Exit\n");
- printf("--------------------------------------------\n");
- //Inputting the choice from the user
- printf(" Enter your choice : ");
- scanf("%d",&choice);
- //Calling functions as per the input of the user
- switch(choice)
- {
- case 1:
- printf("\n Enter data of first node : ");
- scanf("%d",&data);
- insertAtBeginning(data);
- displayList();
- revdisplaylist();
- break;
- case 2:
- printf("\n Enter data of end node : ");
- scanf("%d",&data);
- insertAtEnd(data);
- displayList();
- revdisplaylist();
- break;
- case 3:
- printf("\n Enter the position(0 to Totalnode+1) where you want to insert new node: ");
- scanf("%d",&n);
- printf("\n Enter inserting data value: ");
- scanf("%d",&data);
- insertAtBN(data,n);
- displayList();
- revdisplaylist();
- break;
- case 4:
- DlListDeleteFirstNode();
- displayList();
- revdisplaylist();
- break;
- case 5:
- DlListDeleteendNode();
- displayList();
- revdisplaylist();
- break;
- case 6:
- //Inputting the position from the user
- printf(" Enter the position(1 to Totalnode) to delete a node : ");
- scanf("%d",&insPlc);
- DlListDeleteAnyNode(insPlc);
- displayList();
- revdisplaylist();
- break;
- case 7:
- printf("Stop program.Exit ");
- break;
- default:
- printf("\n Error! Invalid choice...Try again. ");
- }
- }
- return 0;
- }
- //defining the dunction to create the list
- void createList(int n)
- {
- //initialization of variables
- int i, data;
- struct node *newNode;
- //Creating the list
- if(n >= 1)
- {
- head = (struct node *)malloc(sizeof(struct node));
- //Inputting the data into the node
- printf(" Enter data of first node: ");
- scanf("%d",&data);
- head->data = data;
- head->prev = NULL;
- head->next = NULL;
- end = head;
- //Create and link rest of the n-1 nodes
- for(i=2; i<=n; i++)
- {
- //Allocating the memory for the nodes
- newNode = (struct node *)malloc(sizeof(struct node));
- //Inputting the data in the nodes
- printf(" Enter data of next node: ");
- scanf("%d", &data);
- newNode->data = data;
- newNode->prev = end; // Link new node with the previous node
- newNode->next = NULL;
- end->next = newNode; // Link previous node with the new node
- end = newNode; // Make new node as end/previous node
- }
- //Informing link created successfully
- printf("\n........CREATION SUCCESSFUL........\n");
- }
- }
- //Function to display the linked list
- void displayList()
- {
- //initialization and declaration of variables
- struct node * temp;
- int n = 1;
- //If the linked list is empty
- if(head == NULL)
- {
- printf("List is empty.\n");
- }
- else
- {
- //Displaying the elements in the linked list
- temp = head;
- printf("\n Data in the list:\n");
- printf("------------------\n");
- while(temp != NULL)
- {
- printf("Data of %d node = %d\n", n, temp->data);
- n++;
- //Move the current pointer to next node
- temp = temp->next;
- }
- }
- }
- //Function of reverse the display list
- void revdisplaylist()
- {
- node *list = end;
- printf("\n\nThe list printed from back to front is :\n");
- while(list!=head)
- {
- printf("%4d",list->data);
- list=list->prev;
- }
- printf("%4d\n",list->data);
- }
- //function to insert a node at the begining of the list
- void insertAtBeginning(int data)
- {
- //declaring a newe node
- struct node * newNode;
- //if the list is empty
- if(head == NULL)
- {
- printf("\n Error! List is Empty!\n");
- }
- else
- {
- //allocating memory fo the node
- newNode = (struct node *)malloc(sizeof(struct node));
- newNode->data = data;
- newNode->next = head; // Point to next node which is currently head
- newNode->prev = NULL; // Previous node of first node is NULL
- // Link previous address field of head with newnode
- head->prev = newNode;
- // Make the new node as head node
- head = newNode;
- }
- }
- //function to add a node at the end of the list
- void insertAtEnd(int data)
- {
- //declaratrion opf a new node
- struct node * newNode;
- //if the node is empty
- if(end == NULL)
- {
- printf("\n Error! List is empty!\n");
- }
- else
- {
- //allocating memoty for the new node
- newNode = (struct node *)malloc(sizeof(struct node));
- //inserting the node
- newNode->data = data;
- newNode->next = NULL;
- newNode->prev = end;
- end->next = newNode;
- end = newNode;
- }
- }
- //function to insert a node at nth position of a doubly linked list
- void insertAtBN(int data, int position)
- {
- //declaration of variables
- int i;
- struct node * newNode, *temp;
- //if the list is empty
- if(head == NULL)
- {
- printf("\n Error! List is empty!\n");
- }
- else
- {
- temp = head;
- i=1;
- while(i<position-1 && temp!=NULL)
- {
- temp = temp->next;
- i++;
- }
- //when the entered postion is 1
- if(position == 0 || position == 1)
- {
- //calling the dfunction to insert at the begining
- insertAtBeginning(data);
- }
- //the the position is nth
- else if(temp == end)
- {
- //calling the function tp insert at the end
- insertAtEnd(data);
- }
- else if(temp!=NULL)
- {
- //allocating the memory for the new node
- newNode = (struct node *)malloc(sizeof(struct node));
- newNode->data = data;
- newNode->next = temp->next; // Connect new node with n+1th node
- newNode->prev = temp; // Connect new node with n-1th node
- if(temp->next != NULL)
- {
- //Connect n+1th node with new node
- temp->next->prev = newNode;
- }
- // Connect n-1th node with new node
- temp->next = newNode;
- }
- else
- {
- //If the positon is invalid
- printf("\n Error! Invalid position...Try again.\n");
- }
- }
- }
- //defining the function to delete the first node of the list
- void DlListDeleteFirstNode()
- {
- //creating a new node
- struct node * NodeToDel;
- //if the list is empty
- if(head == NULL)
- {
- printf("\n Delete is not possible.List is Empty.\n");
- }
- else
- {
- NodeToDel = head;
- head = head->next; // move the next address of starting node to 2 node
- head->prev = NULL; // set previous address of staring node is NULL
- free(NodeToDel); // delete the first node from memory
- }
- }
- void DlListDeleteendNode()
- {
- struct node * NodeToDel;
- if(end == NULL)
- {
- printf(" Delete is not possible.List is Empty.\n");
- }
- else
- {
- NodeToDel = end;
- end = end->prev; // move the previous address of the end node to 2nd end node
- end->next = NULL; // set the next address of end node to NULL
- free(NodeToDel); // delete the end node
- }
- }
- //Function to delete a node at a particular position of the list
- void DlListDeleteAnyNode(int pos)
- {
- //declaring a new node-type pointer
- struct node *curNode;
- int i;
- curNode = head;
- //deleting the node
- for(i=1; i<pos && curNode!=NULL; i++)
- {
- curNode = curNode->next;
- }
- if(pos == 1)
- {
- DlListDeleteFirstNode();
- }
- else if(curNode == end)
- {
- DlListDeleteendNode();
- }
- else if(curNode != NULL)
- {
- curNode->prev->next= curNode->next;
- curNode->next->prev = curNode->prev;
- free(curNode); //Delete the n node
- }
- else
- {
- printf(" Invalid position!...Try again.\n");
- }
- }
(End of Coding)
OUTPUT:
CREATE A DOUBLY LINKED LIST
--------------------------------------------
Enter
the total number of nodes: 3
Enter
data of first node: 1
Enter
data of next node: 12
Enter
data of next node: 123
........CREATION SUCCESSFUL........
Data in
the list:
----------------------
Data of 1 node = 1
Data of 2 node = 12
Data of 3 node = 123
DOUBLY LINKED LIST PROGRAM
------------------------------------------------------
1. Insert node at beginning
2. Insert node at end
3. Insert node at a particular position
4. Delete the first node
5. Delete the last node
6. Delete the node at a particular position
7. Exit
----------------------------------------------------------
Enter
your choice : 1
Enter
data of first node : 12
Data in
the list:
----------------------
Data of 1 node = 12
Data of 2 node = 1
Data of 3 node = 12
Data of 4 node = 123
DOUBLY LINKED LIST PROGRAM
------------------------------------------------------
1. Insert node at beginning
2. Insert node at end
3. Insert node at a particular position
4. Delete the first node
5. Delete the last node
6. Delete the node at a particular position
7. Exit
----------------------------------------------------------
Enter
your choice : 2
Enter
data of last node : 1234
Data in
the list:
----------------------
Data of 1 node = 12
Data of 2 node = 1
Data of 3 node = 12
Data of 4 node = 123
Data of 5 node = 1234
DOUBLY LINKED LIST PROGRAM
------------------------------------------------------
1. Insert node at beginning
2. Insert node at end
3. Insert node at a particular position
4. Delete the first node
5. Delete the last node
6. Delete the node at a particular position
7. Exit
----------------------------------------------------------
Enter
your choice : 3
Enter
the position(0 to Totalnode+1) where you want to insert new node: 0
Enter
inserting data value: 123
Data in
the list:
----------------------
Data of 1 node = 123
Data of 2 node = 12
Data of 3 node = 1
Data of 4 node = 12
Data of 5 node = 123
Data of 6 node = 1234
DOUBLY LINKED LIST PROGRAM
-------------------------------------------------------
1. Insert node at beginning
2. Insert node at end
3. Insert node at a particular position
4. Delete the first node
5. Delete the last node
6. Delete the node at a particular position
7. Exit
----------------------------------------------------------
Enter
your choice : 4
Data in
the list:
----------------------
Data of 1 node = 12
Data of 2 node = 1
Data of 3 node = 12
Data of 4 node = 123
Data of 5 node = 1234
DOUBLY LINKED LIST PROGRAM
-------------------------------------------------------
1. Insert node at beginning
2. Insert node at end
3. Insert node at a particular position
4. Delete the first node
5. Delete the last node
6. Delete the node at a particular position
7. Exit
-----------------------------------------------------------------
Enter
your choice : 5
Data in
the list:
----------------------
Data of 1 node = 12
Data of 2 node = 1
Data of 3 node = 12
Data of 4 node = 123
DOUBLY LINKED LIST PROGRAM
-------------------------------------------------------
1. Insert node at beginning
2. Insert node at end
3. Insert node at a particular position
4. Delete the first node
5. Delete the last node
6. Delete the node at a particular position
7. Exit
-----------------------------------------------------------
Enter
your choice : 6
Enter
the position(1 to Totalnode) to delete a node : 1
Data in
the list:
----------------------
Data of 1 node = 1
Data of 2 node = 12
Data of 3 node = 123
DOUBLY LINKED LIST PROGRAM
------------------------------------------------------
1. Insert node at beginning
2. Insert node at end
3. Insert node at a particular position
4. Delete the first node
5. Delete the last node
6. Delete the node at a particular position
7. Exit
----------------------------------------------------------
Enter
your choice : 7
Stop program.Exit
0 comments:
Post a Comment