SlideShare a Scribd company logo
1 of 18
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

More Related Content

What's hot (20)

week-16x
week-16xweek-16x
week-16x
 
Avl tree
Avl treeAvl tree
Avl tree
 
Function basics
Function basicsFunction basics
Function basics
 
C++ programs
C++ programsC++ programs
C++ programs
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Implementing string
Implementing stringImplementing string
Implementing string
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
week-18x
week-18xweek-18x
week-18x
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
week-17x
week-17xweek-17x
week-17x
 
งานนำเสนอ อาจารย์ลาวัลย์
งานนำเสนอ อาจารย์ลาวัลย์งานนำเสนอ อาจารย์ลาวัลย์
งานนำเสนอ อาจารย์ลาวัลย์
 
4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
 
C questions
C questionsC questions
C questions
 
Arrays
ArraysArrays
Arrays
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 

Viewers also liked

Doubly linked list
Doubly linked listDoubly linked list
Doubly linked listFahd Allebdi
 
Circular linked list
Circular linked listCircular linked list
Circular linked listdchuynh
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked listsstudent
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJulie Iskander
 
Dynamic data structures
Dynamic data structuresDynamic data structures
Dynamic data structures9020303098
 
Doubly circular linked list
Doubly circular linked listDoubly circular linked list
Doubly circular linked listRoshan Chaudhary
 
Eventually-Consistent Data Structures
Eventually-Consistent Data StructuresEventually-Consistent Data Structures
Eventually-Consistent Data StructuresSean Cribbs
 
Purely functional data structures demystified
Purely functional data structures demystifiedPurely functional data structures demystified
Purely functional data structures demystifiedMohit Thatte
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked ListNinad Mankar
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6Teksify
 
Data structure circular list
Data structure circular listData structure circular list
Data structure circular listiCreateWorld
 
10 generics a-4_in_1
10 generics a-4_in_110 generics a-4_in_1
10 generics a-4_in_1arasforever
 

Viewers also liked (20)

Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Data Structure (Circular Linked List)
Data Structure (Circular Linked List)Data Structure (Circular Linked List)
Data Structure (Circular Linked List)
 
Linked lists
Linked listsLinked lists
Linked lists
 
Linked list
Linked listLinked list
Linked list
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked lists
 
Data structures1
Data structures1Data structures1
Data structures1
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Dynamic data structures
Dynamic data structuresDynamic data structures
Dynamic data structures
 
Doubly circular linked list
Doubly circular linked listDoubly circular linked list
Doubly circular linked list
 
Eventually-Consistent Data Structures
Eventually-Consistent Data StructuresEventually-Consistent Data Structures
Eventually-Consistent Data Structures
 
Purely functional data structures demystified
Purely functional data structures demystifiedPurely functional data structures demystified
Purely functional data structures demystified
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Doubly Link List
Doubly Link ListDoubly Link List
Doubly Link List
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6
 
Data structure circular list
Data structure circular listData structure circular list
Data structure circular list
 
10 generics a-4_in_1
10 generics a-4_in_110 generics a-4_in_1
10 generics a-4_in_1
 
It6601 mobile computing unit 4 questions
It6601 mobile computing unit 4 questionsIt6601 mobile computing unit 4 questions
It6601 mobile computing unit 4 questions
 
circular linked list
circular linked listcircular linked list
circular linked list
 

Similar to Double linked list

Similar to Double linked list (20)

C basics
C basicsC basics
C basics
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 
Array menu
Array menuArray menu
Array menu
 
One dimensional operation of Array in C- language
One dimensional operation of Array in C- language One dimensional operation of Array in C- language
One dimensional operation of Array in C- language
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
week-21x
week-21xweek-21x
week-21x
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Pnno
PnnoPnno
Pnno
 
C program
C programC program
C program
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
 

More from Sayantan Sur

Image Encryption and Compression
Image Encryption and Compression Image Encryption and Compression
Image Encryption and Compression Sayantan Sur
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked ListSayantan Sur
 
Decision Support System(DSS)
Decision Support System(DSS)Decision Support System(DSS)
Decision Support System(DSS)Sayantan Sur
 
International Terrorism
International Terrorism International Terrorism
International Terrorism Sayantan Sur
 

More from Sayantan Sur (9)

Image Encryption and Compression
Image Encryption and Compression Image Encryption and Compression
Image Encryption and Compression
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
 
Stack using Array
Stack using ArrayStack using Array
Stack using Array
 
Decision Support System(DSS)
Decision Support System(DSS)Decision Support System(DSS)
Decision Support System(DSS)
 
Network Security
Network SecurityNetwork Security
Network Security
 
Visual Studio IDE
Visual Studio IDEVisual Studio IDE
Visual Studio IDE
 
Ethical Hacking
Ethical HackingEthical Hacking
Ethical Hacking
 
Phising
PhisingPhising
Phising
 
International Terrorism
International Terrorism International Terrorism
International Terrorism
 

Recently uploaded

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 

Recently uploaded (20)

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 

Double linked list

  • 1. DOUBLE LINKED LIST A Menu Driven Program In C
  • 2. 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(); }
  • 3. 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."); } } }
  • 4. 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; } }
  • 5. 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; } } }
  • 6. 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."); } } }
  • 7. 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(); }
  • 8. 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(); }
  • 9. 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(); }
  • 10. Counting Of Nodes int count() { int iCount=0; NODE *traverse; traverse=start; while(traverse!=NULL) { traverse=traverse->next; iCount++; } return iCount; }
  • 11. 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."); } } }
  • 12. 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(); }
  • 13. 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(); }
  • 14. 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(); }
  • 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 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(); }
  • 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."); }