SlideShare ist ein Scribd-Unternehmen logo
1 von 5
STACKS, QUEUES AND LINKED LIST
Stack
In computer science, a stack is a Last in, First out (LIFO) data structure. It simply means that an
element that is inserted at the end will be deleted first. To Manage a stack all the insertion and
deletion takes place from one posi
One of the common uses of stack is in function call.
Operations on the Stack
There are two fundamental operations
Push
Pop
Push means to insert an element
Pop means to delete an element
Queue
In computer science, a Queue is a First in, First out (FIFO) data structure. It simply means that an
element that is inserted at the beginning will be deleted first. To Manage a queue all the insertion and
Every element is inserted from the rear position and deleted from the front position in the queue.
Linked List
A linked list is a data structure consisting of a group of nodes which together represent a sequence.
Under the simplest form, each node is composed of a data and a reference (in other words, a link) to
the next node in the sequence; more complex variants add additional links. This structure allows for
efficient insertion or removal of elements from any position in thesequence.
Here in the figure is an example of a linked list whose nodes contain two fields: an integer value and
a link to the next node. The last node is linked to a terminator used to signify the end of thelist.
Linked lists are among the simplest and most common data structures. They can be used to
implement several other common abstract data types, stacks, queues etc though it is not uncommon to
implement the other data structures directly without using a list as the basis of implementation.
The principal benefit of a linked list over an array is that the list elements can easily be inserted or
removed without reallocation or reorganization of the entire structure because the data items need not
be stored contiguously in memory or on disk. Linked lists allow insertion and removal of nodes at
any point in the list, and can do so with a constant number of operations if the link previous to the
link being added or removed is maintained during list traversal.
Linked list are dynamic structure where memory allocation takes place at run time.
Operation on a linked list
There are three basic operations on a linked list
Insertion
Deletion
Traversal
Inserting a node or element into Linked list :
Inserting an element into linked list contains 3 types .
1. Insertion at beginning of the Linked list
2. Insertion after/before any element of the linked list
3. Insertion at the end of the linked list
Deleting a node from the Linked list.
A node can be deleted in 3 ways similar to Insertion.
1. Deleting a Node from the beginning of the Linked List
40
41
2. Deleting a node before/after an element from the Linked list.
3. Deleting a node from the end of the Linked List .
Implementation of stacks using a linked list
The stack which is implemented using linked list is called linked stack or dynamic stack
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node * next;
};
class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void stackpush();
void stackpop();
void displaystack();
};
void stack::stackpush()
{
node *ptr;
ptr=new node;
cout<<"Enter the element to be pushed"<<endl;
cin>>ptr->data;
if(top==NULL)
ptr->next=NULL;
else
ptr->next=top;
top=ptr;
}
void stack ::stackpop()
{
node *ptr;
ptr=new node;
ptr=top;
cout<<"The popped element is "<<ptr->data;
top=top->next;
delete ptr;
}
void stack :: displaystack()
{
node *ptr;
ptr=new node;
ptr=top;
cout<<"The stack is "<<endl;
while(ptr!=NULL)
{
cout<<ptr->data<<endl;
ptr=ptr->next;
}
}
void main()
{
clrscr();
char ans;
stack s1;
do
{
s1.stackpush();
cout<<"wish to continue "<<endl;
cin>>ans;
}while(ans=='y');
s1.displaystack();
cout<<"Press any key to pop an
element"<<endl;
getch();
s1.stackpop();
getch();
}
42
Implementation of queues using a linked list
The queue which is implemented using linked list is called linked queue or dynamic queue
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node * next;
};
class queue
{
node *front,*rear;
public:
queue()
{
rear=front=NULL;
}
void insqueue();
void delqueue();
void dispqueue();
};
void queue::insqueue()
{
node *ptr;
ptr=new node;
cout<<"Enter the element to be insert"<<endl;
cin>>ptr->data;
ptr->next=NULL;
if(rear==NULL)
front=rear=ptr;
else
{
rear->next=ptr;
rear=ptr;
}
}
void queue ::delqueue()
{
node *ptr;
ptr=new node;
ptr=front;
cout<<"The deleted element is "<<ptr->data;
if(front==rear)
front=rear=NULL;
else
front=front->next;
delete ptr;
}
void queue :: dispqueue()
{
node *ptr;
ptr=new node;
ptr=front;
cout<<"The queue is "<<endl;
while(ptr!=NULL)
{
cout<<ptr->data<<endl;
ptr=ptr->next;
}
}
void main()
{
clrscr();
char ans;
queue q1;
do
{
q1.insqueue();
cout<<"wish to continue "<<endl;
cin>>ans;
}while(ans=='y');
q1.dispqueue();
cout<<"Press any key to delete an element
...."<<endl;
getch();
q1.delqueue();
getch();
}
Some Questions based on Board Examination Linked stack & Linked Queue
allocated Queue of Customers implemented with the help of the following structure:
struct Customer
{ int CNo;
char CName[20];
Customer *Link;
};
Ans: struct Customer
{
int CNo;
char CName[20];
Customer *Link;
} *Front, *Rear, *ptr;
void DELETE()
{
if(Front = = NULL)
n Queue Underflow
else
{
ptr = Front;
Front = Front Link;
delete ptr;
}
}
allocated Stack of Books implemented with the help of the following structure.
struct Book
{ int BNo;
char BName[20];
Book *Next;
};
Ans: struct Book
{
int BNo;
char BName[20];
Book *Next;
}*Front, *Rear, *ptr;
void POP()
{
if(Front = = NULL)
n Stack Underflow
else
{
ptr = Front;
Front = Front Link;
delete ptr; }
}
43
=
44
Q 3. Evaluate the postfix notaion of expression.
4, 10 , 5 , + , * , 15 , 3 ,/ , -
Q. 4.
Sno. Symbol Stack
0 [
1 4 [4
2 10 [4,10
3 5 [4,10,5
4 + [4
[4,15
5 * [
[60
6 15 [60,15
7 3 [60,15,3
8 / [60
[60,5
9 - [
[55
10 ] 55 Ans

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (19)

Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Linked List
Linked ListLinked List
Linked List
 
linked list
linked listlinked list
linked list
 
Singly link list
Singly link listSingly link list
Singly link list
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Linked list
Linked listLinked list
Linked list
 
Link list
Link listLink list
Link list
 
Linked lists
Linked listsLinked lists
Linked lists
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 
Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6
 
Linked list
Linked listLinked list
Linked list
 
Link List
Link ListLink List
Link List
 
Linked list
Linked listLinked list
Linked list
 
Doubly Link List
Doubly Link ListDoubly Link List
Doubly Link List
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 

Ähnlich wie STACKS, QUEUES AND LINKED LISTS

Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arorakulachihansraj
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queuestech4us
 
Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Getachew Ganfur
 
Data Structure
Data Structure Data Structure
Data Structure Ibrahim MH
 
chapter three ppt.pptx
chapter three ppt.pptxchapter three ppt.pptx
chapter three ppt.pptxselemonGamo
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queueRajkiran Nadar
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueRai University
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queueRai University
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queuePulkitmodi1998
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.pptSeethaDinesh
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queueRai University
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 

Ähnlich wie STACKS, QUEUES AND LINKED LISTS (20)

Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
 
Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9
 
Rana Junaid Rasheed
Rana Junaid RasheedRana Junaid Rasheed
Rana Junaid Rasheed
 
Data Structure
Data Structure Data Structure
Data Structure
 
chapter three ppt.pptx
chapter three ppt.pptxchapter three ppt.pptx
chapter three ppt.pptx
 
Assignment
AssignmentAssignment
Assignment
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 
12888239 (2).ppt
12888239 (2).ppt12888239 (2).ppt
12888239 (2).ppt
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
DSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdfDSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdf
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 
Linked list
Linked list Linked list
Linked list
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 

Mehr von Dev Chauhan

GTU induction program report
GTU induction program report GTU induction program report
GTU induction program report Dev Chauhan
 
2 States Book Review
2 States Book Review2 States Book Review
2 States Book ReviewDev Chauhan
 
NETWORKING AND COMMUNICATION TECHNOLOGIES
NETWORKING AND COMMUNICATION TECHNOLOGIESNETWORKING AND COMMUNICATION TECHNOLOGIES
NETWORKING AND COMMUNICATION TECHNOLOGIESDev Chauhan
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDev Chauhan
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDev Chauhan
 
BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12Dev Chauhan
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 
Communication and Network Concepts
Communication and Network ConceptsCommunication and Network Concepts
Communication and Network ConceptsDev Chauhan
 
What is bullying
What is bullyingWhat is bullying
What is bullyingDev Chauhan
 
Properties Of Water
Properties Of WaterProperties Of Water
Properties Of WaterDev Chauhan
 
बहुव्रीहि समास
बहुव्रीहि समासबहुव्रीहि समास
बहुव्रीहि समासDev Chauhan
 
अव्ययीभाव समास
अव्ययीभाव समासअव्ययीभाव समास
अव्ययीभाव समासDev Chauhan
 
तत्पुरुष (विभक्ति, उपपद ऽ नञ्)
तत्पुरुष (विभक्ति, उपपद ऽ नञ्)तत्पुरुष (विभक्ति, उपपद ऽ नञ्)
तत्पुरुष (विभक्ति, उपपद ऽ नञ्)Dev Chauhan
 
कर्मधारय, द्विगु समास
कर्मधारय, द्विगु समासकर्मधारय, द्विगु समास
कर्मधारय, द्विगु समासDev Chauhan
 
द्वन्द्व समास
द्वन्द्व समासद्वन्द्व समास
द्वन्द्व समासDev Chauhan
 
Class 10 Farewell Presentation Topic:- Nostalgia
Class 10 Farewell Presentation Topic:- NostalgiaClass 10 Farewell Presentation Topic:- Nostalgia
Class 10 Farewell Presentation Topic:- NostalgiaDev Chauhan
 

Mehr von Dev Chauhan (17)

GTU induction program report
GTU induction program report GTU induction program report
GTU induction program report
 
2 States Book Review
2 States Book Review2 States Book Review
2 States Book Review
 
NETWORKING AND COMMUNICATION TECHNOLOGIES
NETWORKING AND COMMUNICATION TECHNOLOGIESNETWORKING AND COMMUNICATION TECHNOLOGIES
NETWORKING AND COMMUNICATION TECHNOLOGIES
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
 
BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12
 
BOOLEAN ALGEBRA
BOOLEAN ALGEBRABOOLEAN ALGEBRA
BOOLEAN ALGEBRA
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
Communication and Network Concepts
Communication and Network ConceptsCommunication and Network Concepts
Communication and Network Concepts
 
What is bullying
What is bullyingWhat is bullying
What is bullying
 
Properties Of Water
Properties Of WaterProperties Of Water
Properties Of Water
 
बहुव्रीहि समास
बहुव्रीहि समासबहुव्रीहि समास
बहुव्रीहि समास
 
अव्ययीभाव समास
अव्ययीभाव समासअव्ययीभाव समास
अव्ययीभाव समास
 
तत्पुरुष (विभक्ति, उपपद ऽ नञ्)
तत्पुरुष (विभक्ति, उपपद ऽ नञ्)तत्पुरुष (विभक्ति, उपपद ऽ नञ्)
तत्पुरुष (विभक्ति, उपपद ऽ नञ्)
 
कर्मधारय, द्विगु समास
कर्मधारय, द्विगु समासकर्मधारय, द्विगु समास
कर्मधारय, द्विगु समास
 
द्वन्द्व समास
द्वन्द्व समासद्वन्द्व समास
द्वन्द्व समास
 
Class 10 Farewell Presentation Topic:- Nostalgia
Class 10 Farewell Presentation Topic:- NostalgiaClass 10 Farewell Presentation Topic:- Nostalgia
Class 10 Farewell Presentation Topic:- Nostalgia
 

Kürzlich hochgeladen

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
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
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 

Kürzlich hochgeladen (20)

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
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
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 

STACKS, QUEUES AND LINKED LISTS

  • 1. STACKS, QUEUES AND LINKED LIST Stack In computer science, a stack is a Last in, First out (LIFO) data structure. It simply means that an element that is inserted at the end will be deleted first. To Manage a stack all the insertion and deletion takes place from one posi One of the common uses of stack is in function call. Operations on the Stack There are two fundamental operations Push Pop Push means to insert an element Pop means to delete an element Queue In computer science, a Queue is a First in, First out (FIFO) data structure. It simply means that an element that is inserted at the beginning will be deleted first. To Manage a queue all the insertion and Every element is inserted from the rear position and deleted from the front position in the queue. Linked List A linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a data and a reference (in other words, a link) to the next node in the sequence; more complex variants add additional links. This structure allows for efficient insertion or removal of elements from any position in thesequence. Here in the figure is an example of a linked list whose nodes contain two fields: an integer value and a link to the next node. The last node is linked to a terminator used to signify the end of thelist. Linked lists are among the simplest and most common data structures. They can be used to implement several other common abstract data types, stacks, queues etc though it is not uncommon to implement the other data structures directly without using a list as the basis of implementation. The principal benefit of a linked list over an array is that the list elements can easily be inserted or removed without reallocation or reorganization of the entire structure because the data items need not be stored contiguously in memory or on disk. Linked lists allow insertion and removal of nodes at any point in the list, and can do so with a constant number of operations if the link previous to the link being added or removed is maintained during list traversal. Linked list are dynamic structure where memory allocation takes place at run time. Operation on a linked list There are three basic operations on a linked list Insertion Deletion Traversal Inserting a node or element into Linked list : Inserting an element into linked list contains 3 types . 1. Insertion at beginning of the Linked list 2. Insertion after/before any element of the linked list 3. Insertion at the end of the linked list Deleting a node from the Linked list. A node can be deleted in 3 ways similar to Insertion. 1. Deleting a Node from the beginning of the Linked List 40
  • 2. 41 2. Deleting a node before/after an element from the Linked list. 3. Deleting a node from the end of the Linked List . Implementation of stacks using a linked list The stack which is implemented using linked list is called linked stack or dynamic stack #include<iostream.h> #include<conio.h> struct node { int data; node * next; }; class stack { node *top; public: stack() { top=NULL; } void stackpush(); void stackpop(); void displaystack(); }; void stack::stackpush() { node *ptr; ptr=new node; cout<<"Enter the element to be pushed"<<endl; cin>>ptr->data; if(top==NULL) ptr->next=NULL; else ptr->next=top; top=ptr; } void stack ::stackpop() { node *ptr; ptr=new node; ptr=top; cout<<"The popped element is "<<ptr->data; top=top->next; delete ptr; } void stack :: displaystack() { node *ptr; ptr=new node; ptr=top; cout<<"The stack is "<<endl; while(ptr!=NULL) { cout<<ptr->data<<endl; ptr=ptr->next; } } void main() { clrscr(); char ans; stack s1; do { s1.stackpush(); cout<<"wish to continue "<<endl; cin>>ans; }while(ans=='y'); s1.displaystack(); cout<<"Press any key to pop an element"<<endl; getch(); s1.stackpop(); getch(); }
  • 3. 42 Implementation of queues using a linked list The queue which is implemented using linked list is called linked queue or dynamic queue #include<iostream.h> #include<conio.h> struct node { int data; node * next; }; class queue { node *front,*rear; public: queue() { rear=front=NULL; } void insqueue(); void delqueue(); void dispqueue(); }; void queue::insqueue() { node *ptr; ptr=new node; cout<<"Enter the element to be insert"<<endl; cin>>ptr->data; ptr->next=NULL; if(rear==NULL) front=rear=ptr; else { rear->next=ptr; rear=ptr; } } void queue ::delqueue() { node *ptr; ptr=new node; ptr=front; cout<<"The deleted element is "<<ptr->data; if(front==rear) front=rear=NULL; else front=front->next; delete ptr; } void queue :: dispqueue() { node *ptr; ptr=new node; ptr=front; cout<<"The queue is "<<endl; while(ptr!=NULL) { cout<<ptr->data<<endl; ptr=ptr->next; } } void main() { clrscr(); char ans; queue q1; do { q1.insqueue(); cout<<"wish to continue "<<endl; cin>>ans; }while(ans=='y'); q1.dispqueue(); cout<<"Press any key to delete an element ...."<<endl; getch(); q1.delqueue(); getch(); }
  • 4. Some Questions based on Board Examination Linked stack & Linked Queue allocated Queue of Customers implemented with the help of the following structure: struct Customer { int CNo; char CName[20]; Customer *Link; }; Ans: struct Customer { int CNo; char CName[20]; Customer *Link; } *Front, *Rear, *ptr; void DELETE() { if(Front = = NULL) n Queue Underflow else { ptr = Front; Front = Front Link; delete ptr; } } allocated Stack of Books implemented with the help of the following structure. struct Book { int BNo; char BName[20]; Book *Next; }; Ans: struct Book { int BNo; char BName[20]; Book *Next; }*Front, *Rear, *ptr; void POP() { if(Front = = NULL) n Stack Underflow else { ptr = Front; Front = Front Link; delete ptr; } } 43
  • 5. = 44 Q 3. Evaluate the postfix notaion of expression. 4, 10 , 5 , + , * , 15 , 3 ,/ , - Q. 4. Sno. Symbol Stack 0 [ 1 4 [4 2 10 [4,10 3 5 [4,10,5 4 + [4 [4,15 5 * [ [60 6 15 [60,15 7 3 [60,15,3 8 / [60 [60,5 9 - [ [55 10 ] 55 Ans