SlideShare a Scribd company logo
1 of 11
Download to read offline
nd

Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
/* Doubly Linked List Acts Like Stack */
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *llink;
int info;
struct node *rlink;
};
typedef struct node NODE;
void ins_first(NODE**, int);
int del_first(NODE**);
void display(NODE*);
int main()
{
NODE *first=NULL;
int choice, item;
for(;;)
{
printf("Operation Resarch On Doubly Linked Listn");
printf("Program Acts Like Stackn");
printf("1.Push OR Insert Frontn");
printf("2.Pop OR Delete Frontn");
printf("3.Displayn");
printf("4.Exitn");
printf("Enter U R Choicen");
scanf("%d", &choice);
printf("nn");
switch(choice)
{
case 1 : printf("Enter The Item To Insertn");
scanf("%d", &item);
printf("nn");
ins_first(&first, item);
break;
case 2 : item=del_first(&first);
if(item!='0')
printf("Deleted Element Is %dnn", item);
break;
case 3 : printf("Contents Of Stack Aren");
display(first);
break;
default : exit(0);
}
}
return 1;
}

P a g e |1
nd

Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
void ins_first(NODE **first, int item)
{
NODE *newn;
newn=(NODE*)malloc(sizeof(NODE));
newn->llink=NULL;
newn->info=item;
if(*first==NULL)
{
newn->rlink=NULL;
*first=newn;
}
else
{
newn->rlink=*first;
newn->rlink->llink=newn; /*Hihgly IMP */
*first=newn;
}
}
int del_first(NODE **first)
{
int item;
NODE *temp;
if(*first==NULL)
{
printf("Stack IS Underflownn");
return('0');
}
temp=*first;
item=temp->info;
*first=temp->rlink; /* *first->llink=NULL Is An Error. */
free(temp);
return item;
}
void display(NODE *first)
{
NODE *temp;
temp=first;
if(first==NULL)
{
printf("Stack Is Emptynn");
}
else
{
while(temp->rlink!=NULL)
{
printf("%dn", temp->info);
temp=temp->rlink;
}
printf("%dnn", temp->info);
}
}

P a g e |2
nd

Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
/* Doubly Linked List */
Insert Last
void ins_last(NODE **first, int item)
{
NODE *newn, *temp;
newn=(NODE*)malloc(sizeof(NODE));
newn->info=item;
newn->rlink=NULL;
if(*first==NULL)
{
newn->llink=NULL;
*first=newn;
}
else
{
temp=*first;
while(temp->rlink!=NULL)
temp=temp->rlink;
newn->llink=temp;
temp->rlink=newn;
}
}

P a g e |3
nd

Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
/* Doubly Linked List */
Delete Last
int del_last(NODE **first)
{
int item;
NODE *temp;
if(*first==NULL)
{
printf("DLL IS Emptynn");
return('0');
}
else
{
temp=*first;
if(temp->rlink==NULL)
{
item=temp->info;
*first=NULL;
free(temp);
return item;
}
else
{
while(temp->rlink!=NULL)
temp=temp->rlink;
item=temp->info;
temp->llink->rlink=NULL;
free(temp);
return item;
}
}
}

P a g e |4
nd

Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
Write A C Module To Find The Frequency Of Given Element x In A DLL.

P a g e |5
P a g e |6

nd

Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
WAP To Search A Node With A Given Data With A DLL, If It Is Found Delete It, Otherwise Display Appropriate
Message.
int del_k(NODE **first, int k)
{
int item;
NODE *temp;
if(*first==NULL)
{
printf("DLL IS Emptynn");
return('0');
}
temp=*first;
st

if(temp->rlink==NULL && temp->info==k) //Element Found In 1 Position No Further Element Present Right To It.
{
item=temp->info;
*first=NULL;
free(temp);
return item;
}
st
if(temp->rlink!=NULL && temp->info==k) //Element Found In 1 Position Further Element Present Right To It.
{
item=temp->info;
*first=temp->rlink;
temp->rlink->llink=NULL;
free(temp);
return item;
}

}

while(temp!=NULL)
{
if(temp->info==k)
break;
temp=temp->rlink;
}
if(temp!=NULL)
{
item=temp->info;
temp->llink->rlink=temp->rlink;
if(temp->rlink!=NULL) /* If Element Found In Middle & Further Element Present Right To It */
temp->rlink->llink=temp->llink;
free(temp);
return item;
}
else
{
printf("k=%d Not Foundn", k);
return('0');
}
nd

Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
Dec 05
WAP To Perform The Following Operations On Doubly Linked List.
1)To Insert A Node To The Right Of A Given Node.
2)To Delete A Node On The Left Of A Given Node. 12 Marks.
1)To Insert A Node To The Right Of A Given Node.
void ins_x_right_to_y(NODE **first, int x, int y)
{
NODE *newn, *temp;
newn=(NODE*)malloc(sizeof(NODE));
newn->info=x;
if(*first==NULL)
printf("List Is Emptyn");
else
{
temp=*first;
if(temp->info==y && temp->rlink==NULL)
{
newn->rlink=NULL;
newn->llink=temp;
temp->rlink=newn;
}
else
{
while(temp!=NULL)
{
if(temp->info==y)
break;
temp=temp->rlink;
}
if(temp!=NULL && temp->rlink!=NULL)
{
newn->llink=temp->rlink->llink;
newn->rlink=temp->rlink;
temp->rlink->llink=newn;
temp->rlink=newn;
}
else if(temp!=NULL && temp->rlink==NULL)
{
newn->rlink=NULL;
newn->llink=temp;
temp->rlink=newn;
}
else
printf("y=%d Not Found In The Listn", y);
}
}
}

P a g e |7
nd

Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
2)To Delete A Node On The Left Of A Given Node.
int del_x_left_to_y(NODE **first, int y)
{
int item;
NODE *temp, *arc;
if(*first==NULL)
{
printf("DLL IS Emptynn");
return('0');
}
temp=*first;
while(temp!=NULL)
{
if(temp->info==y)
break;
temp=temp->rlink;
}
if(temp!=NULL && temp->llink!=NULL)
{
arc=temp->llink;
item=arc->info;
if(arc->llink!=NULL)
{
arc->llink->rlink=arc->rlink;
temp->llink=arc->llink;
}
else /* 12 13 14 if y=13, To Delete 12 */
{
temp->llink=NULL;
*first=arc->rlink;
}
free(arc);
return item;
}
else if(temp!=NULL && temp->llink==NULL)
{
printf("No Item Present Left To It So Deletion Not Possible");
return('0');
}
else
{
printf("y=%d Not Foundn");
return('0');
}
}

P a g e |8
nd

Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
To Insert A Node On The Left Of A Given Node.
void ins_x_left_to_y(NODE **first, int x, int y)
{
NODE *newn, *temp;
newn=(NODE*)malloc(sizeof(NODE));
newn->info=x;
if(*first==NULL)
printf("List Is Emptyn");
else
{
temp=*first;
if(temp->info==y)
{
newn->llink=NULL;
newn->rlink=*first;
temp->llink=newn;
*first=newn;
}
else
{
while(temp!=NULL)
{
if(temp->info==y)
break;
temp=temp->rlink;
}
if(temp!=NULL)
{
newn->llink=temp->llink;
newn->rlink=temp;
temp->llink->rlink=newn;
temp->llink=newn;
}
else
printf("y=%d Not Found In The Listn", y);
}
}
}

P a g e |9
nd

Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
To Delete A Node On The Right Of A Given Node.
int del_x_right_to_y(NODE **first, int y)
{
int item;
NODE *temp, *arc;
if(*first==NULL)
{
printf("DLL IS Emptynn");
return('0');
}
temp=*first;
while(temp!=NULL)
{
if(temp->info==y)
break;
temp=temp->rlink;
}
if(temp!=NULL && temp->rlink!=NULL)
{
arc=temp->rlink;
item=arc->info;
temp->rlink=arc->rlink;
if(arc->rlink!=NULL) /* 12,6 13, 14 if y=13 To Delete 14 It's Required */
arc->rlink->llink=arc->llink;
free(arc);
return item;
}
else if(temp!=NULL && temp->rlink==NULL)
{
printf("No Item Present Right To It, So Deletion Not Possiblen");
return('0');
}
else
{
printf("y=%d Not Foundn", y);
return('0');
}
}

P a g e | 10
nd

Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
Dec 06/Jan 07
WAP To Interchange The Mth And Nth Elements Of A DLL. 10Marks

P a g e | 11

More Related Content

What's hot

Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structure
Tushar Aneyrao
 

What's hot (20)

linked list
linked listlinked list
linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory Allocation
 
Doubly Linked List || Operations || Algorithms
Doubly Linked List || Operations || AlgorithmsDoubly Linked List || Operations || Algorithms
Doubly Linked List || Operations || Algorithms
 
11 15 (doubly linked list)
11 15 (doubly linked list)11 15 (doubly linked list)
11 15 (doubly linked list)
 
CSE240 Doubly Linked Lists
CSE240 Doubly Linked ListsCSE240 Doubly Linked Lists
CSE240 Doubly Linked Lists
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)
 
Data Structure (Double Linked List)
Data Structure (Double Linked List)Data Structure (Double Linked List)
Data Structure (Double Linked List)
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structure
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
linked list using c
linked list using clinked list using c
linked list using c
 
Linked list without animation
Linked list without animationLinked list without animation
Linked list without animation
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
 
Sorting & Linked Lists
Sorting & Linked ListsSorting & Linked Lists
Sorting & Linked Lists
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 

Viewers also liked (6)

Team 9
Team 9Team 9
Team 9
 
Doubly circular linked list
Doubly circular linked listDoubly circular linked list
Doubly circular linked list
 
05 instruction set design and architecture
05 instruction set design and architecture05 instruction set design and architecture
05 instruction set design and architecture
 
Chapter 8 - Multiplexing 9e
Chapter 8 - Multiplexing 9eChapter 8 - Multiplexing 9e
Chapter 8 - Multiplexing 9e
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 

Similar to Data structure doubly linked list programs

Sorting programs
Sorting programsSorting programs
Sorting programs
Varun Garg
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
mustkeem khan
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
anujmkt
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
ritu1806
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
JUSTSTYLISH3B2MOHALI
 
solution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdfsolution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdf
brijmote
 
BINARY SEARCH TREE OPERATIONS #includestdio.h#includestdlib.pdf
BINARY SEARCH TREE OPERATIONS #includestdio.h#includestdlib.pdfBINARY SEARCH TREE OPERATIONS #includestdio.h#includestdlib.pdf
BINARY SEARCH TREE OPERATIONS #includestdio.h#includestdlib.pdf
ARYAN20071
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 

Similar to Data structure doubly linked list programs (20)

Data structure singly linked list programs VTU Exams
Data structure singly linked list programs VTU ExamsData structure singly linked list programs VTU Exams
Data structure singly linked list programs VTU Exams
 
Data structure circular list
Data structure circular listData structure circular list
Data structure circular list
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
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
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
DS Code (CWH).docx
DS Code (CWH).docxDS Code (CWH).docx
DS Code (CWH).docx
 
week-14x
week-14xweek-14x
week-14x
 
137 Lab-2.2.pdf
137 Lab-2.2.pdf137 Lab-2.2.pdf
137 Lab-2.2.pdf
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
 
Lab-2.2 717822E504.pdf
Lab-2.2 717822E504.pdfLab-2.2 717822E504.pdf
Lab-2.2 717822E504.pdf
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
Linked list imp of list
Linked list imp of listLinked list imp of list
Linked list imp of list
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
solution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdfsolution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdf
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
linkedlist.pptx
linkedlist.pptxlinkedlist.pptx
linkedlist.pptx
 
week-16x
week-16xweek-16x
week-16x
 
BINARY SEARCH TREE OPERATIONS #includestdio.h#includestdlib.pdf
BINARY SEARCH TREE OPERATIONS #includestdio.h#includestdlib.pdfBINARY SEARCH TREE OPERATIONS #includestdio.h#includestdlib.pdf
BINARY SEARCH TREE OPERATIONS #includestdio.h#includestdlib.pdf
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

Data structure doubly linked list programs

  • 1. nd Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore. /* Doubly Linked List Acts Like Stack */ #include<stdio.h> #include<stdlib.h> struct node { struct node *llink; int info; struct node *rlink; }; typedef struct node NODE; void ins_first(NODE**, int); int del_first(NODE**); void display(NODE*); int main() { NODE *first=NULL; int choice, item; for(;;) { printf("Operation Resarch On Doubly Linked Listn"); printf("Program Acts Like Stackn"); printf("1.Push OR Insert Frontn"); printf("2.Pop OR Delete Frontn"); printf("3.Displayn"); printf("4.Exitn"); printf("Enter U R Choicen"); scanf("%d", &choice); printf("nn"); switch(choice) { case 1 : printf("Enter The Item To Insertn"); scanf("%d", &item); printf("nn"); ins_first(&first, item); break; case 2 : item=del_first(&first); if(item!='0') printf("Deleted Element Is %dnn", item); break; case 3 : printf("Contents Of Stack Aren"); display(first); break; default : exit(0); } } return 1; } P a g e |1
  • 2. nd Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore. void ins_first(NODE **first, int item) { NODE *newn; newn=(NODE*)malloc(sizeof(NODE)); newn->llink=NULL; newn->info=item; if(*first==NULL) { newn->rlink=NULL; *first=newn; } else { newn->rlink=*first; newn->rlink->llink=newn; /*Hihgly IMP */ *first=newn; } } int del_first(NODE **first) { int item; NODE *temp; if(*first==NULL) { printf("Stack IS Underflownn"); return('0'); } temp=*first; item=temp->info; *first=temp->rlink; /* *first->llink=NULL Is An Error. */ free(temp); return item; } void display(NODE *first) { NODE *temp; temp=first; if(first==NULL) { printf("Stack Is Emptynn"); } else { while(temp->rlink!=NULL) { printf("%dn", temp->info); temp=temp->rlink; } printf("%dnn", temp->info); } } P a g e |2
  • 3. nd Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore. /* Doubly Linked List */ Insert Last void ins_last(NODE **first, int item) { NODE *newn, *temp; newn=(NODE*)malloc(sizeof(NODE)); newn->info=item; newn->rlink=NULL; if(*first==NULL) { newn->llink=NULL; *first=newn; } else { temp=*first; while(temp->rlink!=NULL) temp=temp->rlink; newn->llink=temp; temp->rlink=newn; } } P a g e |3
  • 4. nd Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore. /* Doubly Linked List */ Delete Last int del_last(NODE **first) { int item; NODE *temp; if(*first==NULL) { printf("DLL IS Emptynn"); return('0'); } else { temp=*first; if(temp->rlink==NULL) { item=temp->info; *first=NULL; free(temp); return item; } else { while(temp->rlink!=NULL) temp=temp->rlink; item=temp->info; temp->llink->rlink=NULL; free(temp); return item; } } } P a g e |4
  • 5. nd Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore. Write A C Module To Find The Frequency Of Given Element x In A DLL. P a g e |5
  • 6. P a g e |6 nd Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore. WAP To Search A Node With A Given Data With A DLL, If It Is Found Delete It, Otherwise Display Appropriate Message. int del_k(NODE **first, int k) { int item; NODE *temp; if(*first==NULL) { printf("DLL IS Emptynn"); return('0'); } temp=*first; st if(temp->rlink==NULL && temp->info==k) //Element Found In 1 Position No Further Element Present Right To It. { item=temp->info; *first=NULL; free(temp); return item; } st if(temp->rlink!=NULL && temp->info==k) //Element Found In 1 Position Further Element Present Right To It. { item=temp->info; *first=temp->rlink; temp->rlink->llink=NULL; free(temp); return item; } } while(temp!=NULL) { if(temp->info==k) break; temp=temp->rlink; } if(temp!=NULL) { item=temp->info; temp->llink->rlink=temp->rlink; if(temp->rlink!=NULL) /* If Element Found In Middle & Further Element Present Right To It */ temp->rlink->llink=temp->llink; free(temp); return item; } else { printf("k=%d Not Foundn", k); return('0'); }
  • 7. nd Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore. Dec 05 WAP To Perform The Following Operations On Doubly Linked List. 1)To Insert A Node To The Right Of A Given Node. 2)To Delete A Node On The Left Of A Given Node. 12 Marks. 1)To Insert A Node To The Right Of A Given Node. void ins_x_right_to_y(NODE **first, int x, int y) { NODE *newn, *temp; newn=(NODE*)malloc(sizeof(NODE)); newn->info=x; if(*first==NULL) printf("List Is Emptyn"); else { temp=*first; if(temp->info==y && temp->rlink==NULL) { newn->rlink=NULL; newn->llink=temp; temp->rlink=newn; } else { while(temp!=NULL) { if(temp->info==y) break; temp=temp->rlink; } if(temp!=NULL && temp->rlink!=NULL) { newn->llink=temp->rlink->llink; newn->rlink=temp->rlink; temp->rlink->llink=newn; temp->rlink=newn; } else if(temp!=NULL && temp->rlink==NULL) { newn->rlink=NULL; newn->llink=temp; temp->rlink=newn; } else printf("y=%d Not Found In The Listn", y); } } } P a g e |7
  • 8. nd Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore. 2)To Delete A Node On The Left Of A Given Node. int del_x_left_to_y(NODE **first, int y) { int item; NODE *temp, *arc; if(*first==NULL) { printf("DLL IS Emptynn"); return('0'); } temp=*first; while(temp!=NULL) { if(temp->info==y) break; temp=temp->rlink; } if(temp!=NULL && temp->llink!=NULL) { arc=temp->llink; item=arc->info; if(arc->llink!=NULL) { arc->llink->rlink=arc->rlink; temp->llink=arc->llink; } else /* 12 13 14 if y=13, To Delete 12 */ { temp->llink=NULL; *first=arc->rlink; } free(arc); return item; } else if(temp!=NULL && temp->llink==NULL) { printf("No Item Present Left To It So Deletion Not Possible"); return('0'); } else { printf("y=%d Not Foundn"); return('0'); } } P a g e |8
  • 9. nd Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore. To Insert A Node On The Left Of A Given Node. void ins_x_left_to_y(NODE **first, int x, int y) { NODE *newn, *temp; newn=(NODE*)malloc(sizeof(NODE)); newn->info=x; if(*first==NULL) printf("List Is Emptyn"); else { temp=*first; if(temp->info==y) { newn->llink=NULL; newn->rlink=*first; temp->llink=newn; *first=newn; } else { while(temp!=NULL) { if(temp->info==y) break; temp=temp->rlink; } if(temp!=NULL) { newn->llink=temp->llink; newn->rlink=temp; temp->llink->rlink=newn; temp->llink=newn; } else printf("y=%d Not Found In The Listn", y); } } } P a g e |9
  • 10. nd Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore. To Delete A Node On The Right Of A Given Node. int del_x_right_to_y(NODE **first, int y) { int item; NODE *temp, *arc; if(*first==NULL) { printf("DLL IS Emptynn"); return('0'); } temp=*first; while(temp!=NULL) { if(temp->info==y) break; temp=temp->rlink; } if(temp!=NULL && temp->rlink!=NULL) { arc=temp->rlink; item=arc->info; temp->rlink=arc->rlink; if(arc->rlink!=NULL) /* 12,6 13, 14 if y=13 To Delete 14 It's Required */ arc->rlink->llink=arc->llink; free(arc); return item; } else if(temp!=NULL && temp->rlink==NULL) { printf("No Item Present Right To It, So Deletion Not Possiblen"); return('0'); } else { printf("y=%d Not Foundn", y); return('0'); } } P a g e | 10
  • 11. nd Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore. Dec 06/Jan 07 WAP To Interchange The Mth And Nth Elements Of A DLL. 10Marks P a g e | 11