DOUBLE LINKED LIST
A Menu Driven Program In C
The Basic Declarations
#include<stdio.h>
#include<stdlib.h>
void mainmenu(), create(), display(), insert(), insert_first(),insert_between(),insert_end(), deletion(),
delete_first(),delete_position(),delete_end(),
sort(),reverse(),find();
int count();
struct node
{
struct node *prev;
int info;
struct node *next;
};
typedef struct node NODE;
NODE *start=NULL;
int main()
{
mainmenu();
}
The Mainmenu Function
void mainmenu()
{
int choice,x;
while(1)
{
printf("n----MAIN MENU----n");
printf("1. Create.n");
printf("2. Display.n");
printf("3. Insert.n");
printf("4. Count.n");
printf("5. Delete.n");
printf("6. Sorting.n");
printf("7. Reverse.n");
printf("8. Find.n");
printf("9. Exit.n");
printf("Enter choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4: x=count();
printf("The number of elements are %d",x);
break;
case 5: deletion();
break;
case 6: sort();
break;
case 7: reverse();
break;
case 8: find();
break;
case 9: exit(0);
break;
default: printf("Enter a valid choice.");
}
}
}
Creation Of Nodes
void create()
{
NODE *temp, *traverse;
int iData;
temp=(NODE*)malloc(sizeof(NODE));
temp->prev=NULL;
temp->next=NULL;
printf("Enter an integer value: ");
scanf("%d",&iData);
temp->info=iData;
if(start==NULL)
{
start=temp;
}
else
{
traverse=start;
while(traverse->next!=NULL)
{
traverse=traverse->next;
}
traverse->next=temp;
temp->prev=traverse;
}
}
Display Of The Nodes
void display()
{
NODE *traverse;
if(start==NULL)
{
printf("There are no nodes to be displayed. The list is
empty.n");
}
else
{
traverse=start;
printf("The linked list is: ");
while(traverse!=NULL)
{
printf("%d ",traverse->info);
traverse=traverse->next;
}
}
}
The Insert Menu
void insert()
{
int choice;
while(1)
{
printf("n---Insert Menu---n");
printf("1. Insert at first position.n");
printf("2. Insert in between.n");
printf("3. Insert at the end.n");
printf("4. Go to main menun");
printf("5. Exitn");
printf("Enter choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insert_first();
break;
case 2: insert_between();
break;
case 3: insert_end();
break;
case 4: mainmenu();
break;
case 5: exit(0);
break;
default: printf("Enter a valid choice.");
}
}
}
Insert Node At The First Position
void insert_first()
{
NODE *temp;
if(start==NULL)
{
printf("List is empty. Create as the first node.n");
create();
}
else
{
int iData;
temp=(NODE *)malloc(sizeof(NODE));
temp->prev=NULL;
temp->next=NULL;
printf("Enter an integer value: ");
scanf("%d",&iData);
temp->info=iData;
start->prev=temp;
temp->next=start;
start=temp;
printf("Node inserted at first position.n");
}
display();
}
Insert Node At Any Position
void insert_between()
{
if(start==NULL)
{
printf("List is empty. Create as the first node.n");
create();
}
else
{
int iPosition,iCount,iLoop,iData;
printf("Enter the position where you want to insert: ");
scanf("%d",&iPosition);
iCount=count();
if(iPosition>iCount)
{
printf("There are %d elements.n",iCount);
}
else
{
NODE *temp,*traverse;
traverse=start;
printf("Enter an integer value: ");
scanf("%d",&iData);
temp = (NODE *)malloc(sizeof(NODE));
temp->prev=NULL;
temp->info=iData;
temp->next=NULL;
for(iLoop=0;iLoop<iPosition-2;iLoop++)
{
traverse=traverse->next;
}
temp->prev=traverse;
temp->next=traverse->next;
traverse->next->prev=temp;
traverse->next=temp;
printf("Node inserted at position %d.n",iPosition);
}
}
display();
}
Insert Node At The End
void insert_end()
{
if(start==NULL)
{
printf("List is empty. Create as the first node.n");
create();
}
else
{
create();
printf("Node inserted at the end.n");
}
display();
}
Counting Of Nodes
int count()
{
int iCount=0;
NODE *traverse;
traverse=start;
while(traverse!=NULL)
{
traverse=traverse->next;
iCount++;
}
return iCount;
}
The Delete Menu
void deletion()
{
int choice;
while(1)
{
printf("n---Delete Menu---n");
printf("1. Delete first node.n");
printf("2. Delete last node.n");
printf("3. Delete by position.n");
printf("4. Go to main menun");
printf("5. Exitn");
printf("Enter choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: delete_first();
break;
case 2: delete_end();
break;
case 3: delete_position();
break;
case 4: mainmenu();
break;
case 5: exit(0);
break;
default: printf("Enter a valid choice.");
}
}
}
Delete The First Node
void delete_first()
{
NODE *delete_node;
if(start==NULL)
{
printf("There are no nodes to be deleted. The list is
empty.n");
}
else
{
delete_node=start;
start=start->next;
free(delete_node);
printf("The first node is deleted.n");
}
display();
}
Delete The Last Node
void delete_end()
{
NODE *delete_node, *traverse;
if(start==NULL)
{
printf("There are no nodes to be deleted. The list is
empty.n");
}
else
{
traverse=start;
while(traverse->next->next!=NULL)
{
traverse=traverse->next;
}
delete_node=traverse->next;
traverse->next=NULL;
free(delete_node);
printf("The last node is deleted.n");
}
display();
}
Delete Node At Any Position
void delete_position()
{
if(start==NULL)
{
printf("There are no nodes to be deleted. The list is empty.n");
}
else
{
NODE *traverse, *delete_node;
int iPosition, iLoop, iCount;
printf("Enter the position to be deleted: ");
scanf("%d",&iPosition);
iCount=count();
if(iPosition>iCount)
{
printf("There are %d elements.n",iCount);
}
else
{
traverse=start;
for(iLoop=0;iLoop<iPosition-2;iLoop++)
{
traverse=traverse->next;
}
delete_node=traverse->next;
delete_node->next->prev=traverse;
traverse->next=delete_node->next;
free(delete_node);
printf("Node deleted at position %d.n",iPosition);
}
}
display();
}
Sorting
void sort()
{
static NODE *traverse1;
traverse1=start;
NODE *traverse2;
int iOuter_Loop,iInner_Loop,iCount,iTemp;
iCount=count();
for(iOuter_Loop=0;iOuter_Loop<iCount-1;iOuter_Loop++)
{
traverse2=traverse1->next;
for(iInner_Loop=iOuter_Loop+1;iInner_Loop<iCount;iInner_Loop++)
{
if(traverse1->info>traverse2->info)
{
iTemp=traverse1->info;
traverse1->info=traverse2->info;
traverse2->info=iTemp;
}
traverse2=traverse2->next;
}
traverse1=traverse1->next;
}
printf("Nodes sortedn");
display();
}
Reversal Of The List
void reverse()
{
NODE *previous, *current;
previous=start;
current=previous->next;
previous->prev=current;
previous->next=NULL;
while(current!=NULL)
{
current->prev=current->next;
current->next=previous;
previous=current;
current=current->prev;
}
start=previous;
printf("Nodes reversedn");
display();
}
Searching
void find()
{
int iData;
NODE *traverse;
printf("Enter a number to be searched: ");
scanf("%d",&iData);
traverse=start;
while(traverse!=NULL)
{
if(traverse->info==iData)
{
printf("Element found.");
return;
}
else
{
traverse=traverse->next;
}
}
printf("Element not found.");
}
Presented By:
Sayantan Sur
Thank You

Double linked list

  • 1.
    DOUBLE LINKED LIST AMenu Driven Program In C
  • 2.
    The Basic Declarations #include<stdio.h> #include<stdlib.h> voidmainmenu(), create(), display(), insert(), insert_first(),insert_between(),insert_end(), deletion(), delete_first(),delete_position(),delete_end(), sort(),reverse(),find(); int count(); struct node { struct node *prev; int info; struct node *next; }; typedef struct node NODE; NODE *start=NULL; int main() { mainmenu(); }
  • 3.
    The Mainmenu Function voidmainmenu() { int choice,x; while(1) { printf("n----MAIN MENU----n"); printf("1. Create.n"); printf("2. Display.n"); printf("3. Insert.n"); printf("4. Count.n"); printf("5. Delete.n"); printf("6. Sorting.n"); printf("7. Reverse.n"); printf("8. Find.n"); printf("9. Exit.n"); printf("Enter choice: "); scanf("%d",&choice); switch(choice) { case 1: create(); break; case 2: display(); break; case 3: insert(); break; case 4: x=count(); printf("The number of elements are %d",x); break; case 5: deletion(); break; case 6: sort(); break; case 7: reverse(); break; case 8: find(); break; case 9: exit(0); break; default: printf("Enter a valid choice."); } } }
  • 4.
    Creation Of Nodes voidcreate() { NODE *temp, *traverse; int iData; temp=(NODE*)malloc(sizeof(NODE)); temp->prev=NULL; temp->next=NULL; printf("Enter an integer value: "); scanf("%d",&iData); temp->info=iData; if(start==NULL) { start=temp; } else { traverse=start; while(traverse->next!=NULL) { traverse=traverse->next; } traverse->next=temp; temp->prev=traverse; } }
  • 5.
    Display Of TheNodes void display() { NODE *traverse; if(start==NULL) { printf("There are no nodes to be displayed. The list is empty.n"); } else { traverse=start; printf("The linked list is: "); while(traverse!=NULL) { printf("%d ",traverse->info); traverse=traverse->next; } } }
  • 6.
    The Insert Menu voidinsert() { int choice; while(1) { printf("n---Insert Menu---n"); printf("1. Insert at first position.n"); printf("2. Insert in between.n"); printf("3. Insert at the end.n"); printf("4. Go to main menun"); printf("5. Exitn"); printf("Enter choice: "); scanf("%d",&choice); switch(choice) { case 1: insert_first(); break; case 2: insert_between(); break; case 3: insert_end(); break; case 4: mainmenu(); break; case 5: exit(0); break; default: printf("Enter a valid choice."); } } }
  • 7.
    Insert Node AtThe First Position void insert_first() { NODE *temp; if(start==NULL) { printf("List is empty. Create as the first node.n"); create(); } else { int iData; temp=(NODE *)malloc(sizeof(NODE)); temp->prev=NULL; temp->next=NULL; printf("Enter an integer value: "); scanf("%d",&iData); temp->info=iData; start->prev=temp; temp->next=start; start=temp; printf("Node inserted at first position.n"); } display(); }
  • 8.
    Insert Node AtAny Position void insert_between() { if(start==NULL) { printf("List is empty. Create as the first node.n"); create(); } else { int iPosition,iCount,iLoop,iData; printf("Enter the position where you want to insert: "); scanf("%d",&iPosition); iCount=count(); if(iPosition>iCount) { printf("There are %d elements.n",iCount); } else { NODE *temp,*traverse; traverse=start; printf("Enter an integer value: "); scanf("%d",&iData); temp = (NODE *)malloc(sizeof(NODE)); temp->prev=NULL; temp->info=iData; temp->next=NULL; for(iLoop=0;iLoop<iPosition-2;iLoop++) { traverse=traverse->next; } temp->prev=traverse; temp->next=traverse->next; traverse->next->prev=temp; traverse->next=temp; printf("Node inserted at position %d.n",iPosition); } } display(); }
  • 9.
    Insert Node AtThe End void insert_end() { if(start==NULL) { printf("List is empty. Create as the first node.n"); create(); } else { create(); printf("Node inserted at the end.n"); } display(); }
  • 10.
    Counting Of Nodes intcount() { int iCount=0; NODE *traverse; traverse=start; while(traverse!=NULL) { traverse=traverse->next; iCount++; } return iCount; }
  • 11.
    The Delete Menu voiddeletion() { int choice; while(1) { printf("n---Delete Menu---n"); printf("1. Delete first node.n"); printf("2. Delete last node.n"); printf("3. Delete by position.n"); printf("4. Go to main menun"); printf("5. Exitn"); printf("Enter choice: "); scanf("%d",&choice); switch(choice) { case 1: delete_first(); break; case 2: delete_end(); break; case 3: delete_position(); break; case 4: mainmenu(); break; case 5: exit(0); break; default: printf("Enter a valid choice."); } } }
  • 12.
    Delete The FirstNode void delete_first() { NODE *delete_node; if(start==NULL) { printf("There are no nodes to be deleted. The list is empty.n"); } else { delete_node=start; start=start->next; free(delete_node); printf("The first node is deleted.n"); } display(); }
  • 13.
    Delete The LastNode void delete_end() { NODE *delete_node, *traverse; if(start==NULL) { printf("There are no nodes to be deleted. The list is empty.n"); } else { traverse=start; while(traverse->next->next!=NULL) { traverse=traverse->next; } delete_node=traverse->next; traverse->next=NULL; free(delete_node); printf("The last node is deleted.n"); } display(); }
  • 14.
    Delete Node AtAny Position void delete_position() { if(start==NULL) { printf("There are no nodes to be deleted. The list is empty.n"); } else { NODE *traverse, *delete_node; int iPosition, iLoop, iCount; printf("Enter the position to be deleted: "); scanf("%d",&iPosition); iCount=count(); if(iPosition>iCount) { printf("There are %d elements.n",iCount); } else { traverse=start; for(iLoop=0;iLoop<iPosition-2;iLoop++) { traverse=traverse->next; } delete_node=traverse->next; delete_node->next->prev=traverse; traverse->next=delete_node->next; free(delete_node); printf("Node deleted at position %d.n",iPosition); } } display(); }
  • 15.
    Sorting void sort() { static NODE*traverse1; traverse1=start; NODE *traverse2; int iOuter_Loop,iInner_Loop,iCount,iTemp; iCount=count(); for(iOuter_Loop=0;iOuter_Loop<iCount-1;iOuter_Loop++) { traverse2=traverse1->next; for(iInner_Loop=iOuter_Loop+1;iInner_Loop<iCount;iInner_Loop++) { if(traverse1->info>traverse2->info) { iTemp=traverse1->info; traverse1->info=traverse2->info; traverse2->info=iTemp; } traverse2=traverse2->next; } traverse1=traverse1->next; } printf("Nodes sortedn"); display(); }
  • 16.
    Reversal Of TheList void reverse() { NODE *previous, *current; previous=start; current=previous->next; previous->prev=current; previous->next=NULL; while(current!=NULL) { current->prev=current->next; current->next=previous; previous=current; current=current->prev; } start=previous; printf("Nodes reversedn"); display(); }
  • 17.
    Searching void find() { int iData; NODE*traverse; printf("Enter a number to be searched: "); scanf("%d",&iData); traverse=start; while(traverse!=NULL) { if(traverse->info==iData) { printf("Element found."); return; } else { traverse=traverse->next; } } printf("Element not found."); }
  • 18.