Implement Insert, Delete, Display and Search operations on DLL or SLL.
Doubly Linked Ordered Lists & Single Linked Ordered Lists
/* DLL - Doubly linked ordered lists */
#include
#include
#include
struct dll
{
int data;
struct dll *prev;
struct dll *next;
};
struct dll *head = NULL;
void ins(void);
void delete(void);
void dis(void);
void main()
{
int choice = 1;
while (choice == 1 || choice == 2 || choice == 3)
{
clrscr();
printf("Doubly Linked Ordered Lists :-\n");
printf("------ ------ ------- -----\n\n");
printf("1] INSERT an element to the DLL\n");
printf("2] DELETE an element from the DLL\n");
printf("3] DISPLAY the DLL elements\n");
printf("4] EXIT \n\n");
printf("Enter your choice :- ");
scanf("%d",&choice);
switch (choice)
{
case 1 :
ins();
break;
case 2 :
delete();
break;
case 3 :
dis();
break;
case 4 :
break;
default :
printf("\nOut of range\n\n");
choice = 1;
getch();
}
}
clrscr();
printf("\nProgram finished.....");
getch();
}
/* Function to add an element to the DLL */
void ins(void)
{
struct dll *new_ptr;
struct dll *cur_ptr;
int value;
char proceed;
new_ptr = (struct dll *) malloc(sizeof(struct dll));
if (new_ptr == NULL)
{
printf("Memory Full...Cannot add...");
getch();
return;
}
printf("\nEnter the new element value :- ");
flushall();
scanf("%d",&value);
new_ptr->data = value;
cur_ptr = head;
proceed = 'Y';
while (proceed == 'Y')
{
if (head == NULL)
{
new_ptr->next = NULL;
new_ptr->prev = NULL;
head = new_ptr;
break;
}
if (cur_ptr->data > value)
{
if (cur_ptr == head)
{
new_ptr->next = head;
new_ptr->prev = NULL;
head->prev = new_ptr;
head = new_ptr;
break;
}
else
{
(cur_ptr->prev)->next = new_ptr;
new_ptr->prev = (cur_ptr->prev);
cur_ptr->prev = new_ptr;
new_ptr->next = cur_ptr;
break;
}
}
else if (cur_ptr->next == NULL)
{
cur_ptr->next = new_ptr;
new_ptr->prev = cur_ptr;
new_ptr->next = NULL;
break;
}
else
{
cur_ptr = cur_ptr->next;
}
}
return;
}
/* Function to remove an element from the DLL */
void delete(void)
{
struct dll *old_ptr;
struct dll *cur_ptr;
int value;
char proceed;
if (head == NULL)
{
printf("DLL is empty...Can not delete...\n");
getch();
return;
}
printf("\nEnter the element value to be deleted :- ");
flushall();
scanf("%d",&value);
cur_ptr = head;
proceed = 'Y';
while (proceed == 'Y')
{
if (cur_ptr->data == value)
{
if (cur_ptr == head)
{
head = cur_ptr->next;
head->prev = NULL;
free(cur_ptr);
break;
}
else
{
(cur_ptr->prev)->next = cur_ptr->next;
(cur_ptr->next)->prev =cur_ptr->prev;
free(cur_ptr);
break;
}
}
else if (cur_ptr->next == NULL)
{
printf("\n\nElement does not exist in the DLL...");
getch();
break;
}
else
{
cur_ptr = cur_ptr->next;
}
}
return;
}
/* Function to display all the elements of the DLL */
void dis(void)
{
struct dll *cur_ptr;
printf("\nHead = %d\n\n", head);
if (head == NULL)
{
printf("\nDLL is empty...");
getch();
return;
}
printf("Data Address Prev Link Next Link\n");
printf("---- ------- --------- ---------\n");
for (cur_ptr = head; cur_ptr != NULL; cur_ptr = cur_ptr->next)
{
printf("%d\t%d\t %d\t %d\n", cur_ptr->data, cur_ptr, cur_ptr->prev, cur_ptr->next);
}
getch();
return;
}
Doubly Linked Ordered Lists & Single Linked Ordered Lists
/* DLL - Doubly linked ordered lists */
#include
#include
#include
struct dll
{
int data;
struct dll *prev;
struct dll *next;
};
struct dll *head = NULL;
void ins(void);
void delete(void);
void dis(void);
void main()
{
int choice = 1;
while (choice == 1 || choice == 2 || choice == 3)
{
clrscr();
printf("Doubly Linked Ordered Lists :-\n");
printf("------ ------ ------- -----\n\n");
printf("1] INSERT an element to the DLL\n");
printf("2] DELETE an element from the DLL\n");
printf("3] DISPLAY the DLL elements\n");
printf("4] EXIT \n\n");
printf("Enter your choice :- ");
scanf("%d",&choice);
switch (choice)
{
case 1 :
ins();
break;
case 2 :
delete();
break;
case 3 :
dis();
break;
case 4 :
break;
default :
printf("\nOut of range\n\n");
choice = 1;
getch();
}
}
clrscr();
printf("\nProgram finished.....");
getch();
}
/* Function to add an element to the DLL */
void ins(void)
{
struct dll *new_ptr;
struct dll *cur_ptr;
int value;
char proceed;
new_ptr = (struct dll *) malloc(sizeof(struct dll));
if (new_ptr == NULL)
{
printf("Memory Full...Cannot add...");
getch();
return;
}
printf("\nEnter the new element value :- ");
flushall();
scanf("%d",&value);
new_ptr->data = value;
cur_ptr = head;
proceed = 'Y';
while (proceed == 'Y')
{
if (head == NULL)
{
new_ptr->next = NULL;
new_ptr->prev = NULL;
head = new_ptr;
break;
}
if (cur_ptr->data > value)
{
if (cur_ptr == head)
{
new_ptr->next = head;
new_ptr->prev = NULL;
head->prev = new_ptr;
head = new_ptr;
break;
}
else
{
(cur_ptr->prev)->next = new_ptr;
new_ptr->prev = (cur_ptr->prev);
cur_ptr->prev = new_ptr;
new_ptr->next = cur_ptr;
break;
}
}
else if (cur_ptr->next == NULL)
{
cur_ptr->next = new_ptr;
new_ptr->prev = cur_ptr;
new_ptr->next = NULL;
break;
}
else
{
cur_ptr = cur_ptr->next;
}
}
return;
}
/* Function to remove an element from the DLL */
void delete(void)
{
struct dll *old_ptr;
struct dll *cur_ptr;
int value;
char proceed;
if (head == NULL)
{
printf("DLL is empty...Can not delete...\n");
getch();
return;
}
printf("\nEnter the element value to be deleted :- ");
flushall();
scanf("%d",&value);
cur_ptr = head;
proceed = 'Y';
while (proceed == 'Y')
{
if (cur_ptr->data == value)
{
if (cur_ptr == head)
{
head = cur_ptr->next;
head->prev = NULL;
free(cur_ptr);
break;
}
else
{
(cur_ptr->prev)->next = cur_ptr->next;
(cur_ptr->next)->prev =cur_ptr->prev;
free(cur_ptr);
break;
}
}
else if (cur_ptr->next == NULL)
{
printf("\n\nElement does not exist in the DLL...");
getch();
break;
}
else
{
cur_ptr = cur_ptr->next;
}
}
return;
}
/* Function to display all the elements of the DLL */
void dis(void)
{
struct dll *cur_ptr;
printf("\nHead = %d\n\n", head);
if (head == NULL)
{
printf("\nDLL is empty...");
getch();
return;
}
printf("Data Address Prev Link Next Link\n");
printf("---- ------- --------- ---------\n");
for (cur_ptr = head; cur_ptr != NULL; cur_ptr = cur_ptr->next)
{
printf("%d\t%d\t %d\t %d\n", cur_ptr->data, cur_ptr, cur_ptr->prev, cur_ptr->next);
}
getch();
return;
}