C++ Programs
C++ Programs
Assignment - 9
P9. C program to implement a menu-driven program for insertion &
deletion on doubly linked list and circular doubly linked list.
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int value;
struct Node *next,*prev;
};
void print_options()
{
printf("\n1.Insert\n2.Delete\n3.Exit\n");
printf("Enter Your Choice : ");
}
void LinearLinkedList()
{
struct Node *head = NULL;
int val, loc;
int c1, c2;while (c1 != 3)
{
print_options();
scanf("%d", &c1);
switch (c1)
{
case 1:
if (is_empty(head))
{head = HeadInitialization();}
else
{
printf("1.Begining\n2.At Location\n3.End\n");
printf("Enter Your Choice : ");
scanf("%d", &c2);
printf("Enter Value :");
scanf("%d", &val);
switch (c2)
{
case 1:
head = InsertAtBegining(val, head);
break;
case 2:
printf("Enter Location value : ");
scanf("%d", &loc);
if (head->value == loc)
{head = InsertAtBegining(val, head);}
else
{InsertAtLoc(val, loc, head);}
break;
case 3:
InsertAtEnd(val, head);break;
default:
printf("No such option\n");
break;
}
}
break;
case 2:
if (is_empty(head))
{printf("Linked List is Empty\n");}
else
{
printf("1.Begining\n2.At Location\n3.End\n");
printf("Enter Your Choice : ");
scanf("%d", &c2);
switch (c2)
{
case 1:
head = DeleteAtBegining(head);
break;
case 2:
printf("Enter Location value : ");
scanf("%d", &loc);
if (head->value == loc)
{head = DeleteAtBegining(head);}
else {DeleteAtLoc(loc, head);}
break;
case 3:
if (head->next == NULL)
{head = DeleteAtBegining(head);}
else {DeleteAtEnd(head);}
break;
default:
printf("No such option\n");
break;
}
}
break;
case 3:
break;
default:
printf("No such option\n");
break;
}
LinkedListTransversal(head);
}
}
//Circular list functions
struct Node *CHeadInitialization()
{
int val;
struct Node *head = (struct Node *)malloc(sizeof(struct
Node)); printf("Enter head value :");
scanf("%d", &val);
head->value = val;
head->prev = head;
head->next = head;
return head;
}
void CLinkedListTransversal(struct Node *ptr)
{
if (ptr == NULL)
{return;}
struct Node *head = ptr;
while (ptr->next != head)
{
printf("%d ", ptr->value);
ptr = ptr->next;
}
printf("%d ", ptr->value);
}
struct Node *CInsertAtBegining(int value, struct Node *head)
{
struct Node *ptr = (struct Node *)malloc(sizeof(struct
Node)); ptr->value = value;
ptr->next = head;
ptr->prev = head->prev;
head->prev = ptr;
ptr->prev->next = ptr;
return ptr;
}
void CircularLinkedList()
{
struct Node *head = NULL;
int val, loc;
int c1, c2;
while (c1 != 3)
{
print_options();
scanf("%d", &c1);
switch (c1)
{
case 1:
if (is_empty(head))
{head = CHeadInitialization();}
else
{
printf("1.Begining\n2.At Location\n3.End\n");
printf("Enter Your Choice :");
scanf("%d", &c2);
printf("Enter Value :");
scanf("%d", &val);
switch (c2)
{
case 1:
head = CInsertAtBegining(val, head);
break;
case 2:
printf("Enter Location value :");
scanf("%d", &loc);
if (head->value == loc)
{head = CInsertAtBegining(val, head);}
else
{CInsertAtLoc(val, loc, head);}
break;
case 3:
CInsertAtEnd(val, head);
break;
default:
printf("No such option\n");
break;
}
}
break;
case 2:
if (is_empty(head))
{printf("Linked List is Empty\n");}
else
{
printf("1.Begining\n2.At Location\n3.End\n");
printf("Enter Your Choice :");
scanf("%d", &c2);
switch (c2)
{
case 1:
head = CDeleteAtBegining(head);
break;
case 2:
printf("Enter Location value :");
scanf("%d", &loc);
if (head->value == loc)
{head = CDeleteAtBegining(head);}
else
{CDeleteAtLoc(loc, head);}
break;
case 3:
if (head->next == head)
{head = CDeleteAtBegining(head);}
else
{CDeleteAtEnd(head);}
break;
default:
printf("No such option\n");
break;}
}
break;
case 3:
break;
default:
printf("No such option\n");
break;
}
CLinkedListTransversal(head);
}
}
void main()
{
int c;
while (c != 3)
{
printf("\n1.Linear\n2.Circular\n3.Exit\n");
printf("Enter Your Choice : ");
scanf("%d", &c);
switch (c)
{
case 1:
LinearLinkedList();
break;
case 2:
CircularLinkedList();
break;
case 3:
break;
default:
printf("No such option\n");
break;
}
}
}
bit.ly/it-ds-p9
Output :
Ritik
B.Tech. IT 2nd Sem
04616401520