0% found this document useful (0 votes)
12 views12 pages

C++ Programs

The document is a C program that implements a menu-driven interface for inserting and deleting nodes in both doubly linked lists and circular doubly linked lists. It includes functions for initializing lists, inserting and deleting nodes at various positions, and traversing the lists. The program allows users to choose between linear and circular linked lists and provides options for managing the lists interactively.

Uploaded by

Atul Kushwaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views12 pages

C++ Programs

The document is a C program that implements a menu-driven interface for inserting and deleting nodes in both doubly linked lists and circular doubly linked lists. It includes functions for initializing lists, inserting and deleting nodes at various positions, and traversing the lists. The program allows users to choose between linear and circular linked lists and provides options for managing the lists interactively.

Uploaded by

Atul Kushwaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Data Structures

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 : ");
}

int is_empty(struct Node *ptr)


{
if (ptr == NULL)
{return 1;}
return 0;
}

//Linear list functions


struct Node *HeadInitialization()
{
int val;
struct Node *head = (struct Node *)malloc(sizeof(struct Node));
printf("Enter head value :");
scanf("%d", &val);
head->value = val;head->prev = NULL;
head->next = NULL;
return head;
}

void LinkedListTransversal(struct Node *ptr)


{
while (ptr != NULL)
{
printf("%d ", ptr->value);
ptr = ptr->next;
}
}

struct Node *InsertAtBegining(int value, struct Node *head)


{
struct Node *ptr = (struct Node *)malloc(sizeof(struct
Node)); ptr->value = value;
ptr->next = head;
ptr->prev = NULL;
head->prev = ptr;
return ptr;
}

void InsertAtLoc(int value, int loc, struct Node *ptr)


{
while (ptr != NULL)
{
if (ptr->value == loc)
{
struct Node *newPtr = (struct Node *)malloc(sizeof(struct
Node));
newPtr->value = value;
newPtr->next = ptr;
newPtr->prev = ptr->prev;
ptr->prev->next = newPtr;
ptr->prev = newPtr;
return;
}
ptr = ptr->next;
}
printf("No location found !\n");
}

void InsertAtEnd(int value, struct Node *ptr)


{
while (ptr->next != NULL)
{ptr = ptr->next;}
struct Node *newPtr = (struct Node *)malloc(sizeof(struct
Node)); newPtr->value = value;
newPtr->next = NULL;
newPtr->prev = ptr;
ptr->next = newPtr;
}

struct Node *DeleteAtBegining(struct Node *head)


{
if (head->next == NULL)
{
free(head);
return NULL;
}
struct Node *temp = head;
head->next->prev = NULL;
free(head);
return temp->next;
}

void DeleteAtEnd(struct Node *ptr)


{
while (ptr->next->next != NULL)
{ptr = ptr->next;}
free(ptr->next);ptr->next = NULL;
}

void DeleteAtLoc(int loc, struct Node *ptr)


{
if (ptr->next == NULL)
{
printf("No location found !\n");
return;
}
while (ptr->next->next != NULL)
{
ptr = ptr->next;
if (ptr->value == loc)
{
ptr->prev->next = ptr->next;
ptr->next->prev = ptr->prev;
free(ptr);
return;
}
}
if (ptr->next->value == loc)
{
free(ptr->next);
ptr->next = NULL;
return;
}
printf("No location found !\n");
}

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 CInsertAtLoc(int value, int loc, struct Node *ptr)


{
struct Node *head = ptr;
ptr = ptr->next;
while (ptr != head)
{
if (ptr->value == loc)
{
struct Node *newPtr = (struct Node *)malloc(sizeof(struct
Node));
newPtr->value = value;
newPtr->next = ptr;
newPtr->prev = ptr->prev;
ptr->prev->next = newPtr;
ptr->prev = newPtr;return;
}
ptr = ptr->next;
}
printf("No location found !\n");
}

void CInsertAtEnd(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->next = ptr;
head->prev = ptr;
}

struct Node *CDeleteAtBegining(struct Node *head)


{
if (head->next == head)
{
free(head);
return NULL;
}
struct Node *temp = head;
head->next->prev = head->prev;
head->prev->next = head->next;
free(head);
return temp->next;
}
void CDeleteAtEnd(struct Node *head)
{
struct Node *temp = head->prev;
head->prev = head->prev->prev;
head->prev->next = head;
free(temp);
}
void CDeleteAtLoc(int loc, struct Node *ptr)
{
struct Node *head = ptr;if (ptr->next == head)
{
printf("No location found !\n");
return;
}
while (ptr->next->next != head)
{
ptr = ptr->next;
if (ptr->value == loc)
{
ptr->prev->next = ptr->next;
ptr->next->prev = ptr->prev;
free(ptr);
return;
}
}
if (ptr->next->value == loc)
{
CDeleteAtEnd(head);
return;
}
printf("No location found !\n");
}

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;
}
}
}

Memory used by console : 624 KB


Memory used by program : 372 KB
Total memory used : 996 KB
*Also some memory is used by cmd (6.5 MB)

bit.ly/it-ds-p9

View it online -->

Output :
Ritik
B.Tech. IT 2nd Sem
04616401520

You might also like