Sei sulla pagina 1di 5

C Program demonstrating Doubly Linked List

Operations:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node *head;
struct node* GetNewNode(int ele)
{
struct node *newNode = (struct node*) malloc
(sizeof(struct node));
newNode -> data = ele;
newNode -> prev = NULL;
newNode -> next = NULL;
return newNode;
}
void InsertFront(int ele)
{
struct node *newNode = GetNewNode(ele);
if(head == NULL)
{
head = newNode;
return;
}
head -> prev = newNode;
newNode -> next = head;
head = newNode;
}
void InsertRear(int ele)
{
struct node *temp = head;
struct node *newNode = GetNewNode(ele);
if(head == NULL)

{
head = newNode;
return;
}
while(temp->next != NULL)
temp = temp->next; // Go To last Node
temp -> next = newNode;
newNode -> prev = temp;
}
void DeleteFront()
{
struct node *temp;
if(head == NULL)
{
printf("\nUnable to delete. List is empty.\n");
return;
}
else
{
temp = head;
head = head -> next; //Move the next pointer of head
node to the node succeding the head node
head -> prev = NULL; //Remove the link to previous node
printf("\nDeleting the node: %d", temp -> data);
free(temp); //Delete the first node from memory
}
}
void DeleteRear()
{
struct node *temp, *temp1;
if(head == NULL)
{
printf("Unable to delete. List is empty.\n");
}

else
{
temp = head;
while(temp -> next != NULL)
temp = temp -> next;
temp1 = temp;
temp = temp -> prev; //Move last pointer to 2nd last
node
temp -> next = NULL; //Remove link to of 2nd last node
with last node
printf("\nDeleting the node: %d", temp1 -> data);
free(temp1); //Delete the last node
}
}
void display()
{
struct node *temp = head;
printf("The linked list is: \n");
while(temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp -> next;
}
printf("\n");
}
//Prints all elements in linked list in reverse traversal order.
void ReversePrint()
{
struct node *temp = head;
if(head == NULL)
{
printf("\nLinked list is empty, exiting");
return; // empty list, exit
}
//Going to last Node

while(temp->next != NULL)
{
temp = temp->next;
}
// Traversing backward using prev pointer
printf("\nReversed linked list is: \n");
while(temp != NULL)
{
printf("%d -> ", temp -> data);
temp = temp -> prev;
}
printf("\n");
}
void Initialize()
{
head = NULL;
}
int main()
{
int ele, choice;
Initialize();
do
{
printf("\nDoubly linked list operations: ");
printf("\n1.Insert from the front");
printf("\n2.Insert from the rear");
printf("\n3.Delete from the front");
printf("\n4.Delete from the rear");
printf("\n5.Display the linked list");
printf("\n6.Display the linked list in reverse order");
printf("\n7.Exit");
printf("\n\nEnter your choice (1 - 7): ");
scanf("%d", &choice);
printf("\n\n");
switch(choice)
{
case 1:

printf("\nEnter the element you wish to insert at


the front: ");
scanf("%d", &ele);
InsertFront(ele);
break;
case 2:
printf("\nEnter the element you wish to insert at
the rear: ");
scanf("%d", &ele);
InsertRear(ele);
break;
case 3:
DeleteFront();
break;
case 4:
DeleteRear();
break;
case 5:
display();
break;
case 6:
ReversePrint();
break;
case 7:
exit(0);
}
}while(choice != 7);
return 0;
}

Potrebbero piacerti anche