SlideShare una empresa de Scribd logo
1 de 13
Stack & Queue
DATA STRUCTURES
Govt. Madhav Science College Ujjain
Department of Computer ScienceAnd application
PROF. ANIL KUMAR PRAJAPATI
What is a stack?
◦ Stack is linear data structure, working in last in first
out(LIFO) manner. It is an Abstract Data Type
(ADT), commonly used in most programming
languages.
◦ In this type of data structure, we can extract the
date from the end from which we enter the data.
◦ Push operation is used to enter data in stack and
pop operation is used to remove data.
Stack Representation
Top
Data Element
04
Data Element
03
Data Element
02
Data Element
01
Top
Data Element
04
Data Element
03
Data Element
02
Data Element
01
Data Element 05
Push
Data Element 05
Pop
Insertion Deletion
Algorithm for insert data into stack
Step 1. Check stack if full
If Top=Max-1
Print stack is full and exit
Step 2: Increment to by one
Top=Top+1
Step 3: Insert data into stack
Stack[TOP]=data
Step 4: End
1
2
1
3
2
1
4
3
2
1
5
4
3
2
1
Push
2
Push
3
Push
4
Push
5
Push
1
Max
size
Push (Insert data item into Stack)
For inserting data item into stack we use push operation.
Insertion
Top
Data 3
Data 2
Data 1
Data 1, Data 2, Data 3, Data 4, Data 5,
Insertion
Top
Data 4
Data 3
Data 2
Data 1
Insertion
Top
Data 2
Data 1
Insertion
Top
Data 1
Push
Algorithm for insert data into stack
Step 1: Check stack is empty
If TOP=-1
Print Stack is empty and Exit
Step 2: delete an element from stack top
Set Del_element=Stack[Top]
Step 3: decrement stack size by one
Top=Top-1
Step 4: End
5
4
3
2
1
4
3
2
1
3
2
1
2
1
Max
size Pop
5
Pop
4 Pop
3
1
Pop
2
Pop (Deleting data item from Stack)
for deleting data item into stack we use Pop operation.
Deletion
Top
Data 2
Data 1
Data 1, Data 2, Data 3, Data 4, Data 5,
Deletion
Top
Data 1
Deletion
Top
Data 3
Data 2
Data 1
Deletion
Top
Data 4
Data 3
Data 2
Data 1
pop
What is Queue
 A Queue is a linear structure which follows a particular order in
which the operations are performed. The order is First In First
Out (FIFO). In Queue the allocation of data are continuous
manner.
 insertion and deletion is done from different ends in the Queue,
using rear for insertion and front for Dilation.
 Queue having two different formats linear queue and circular
queue.
Types of Queue
broadly Queue are divided into two types such as linear and circular but
linear queue having also two different variations, double ended queue or
Dequeue and insertion restricted and deletion restricted.
10 20 30 40 50
Front
Rear
Linear Queue
10
50 30
20
40
60
Circular Queue
Rear
Front
10 20 30 40 50
10 20 30 40 50
Deletion restricted Queue
Double Ended Queue
Insertion restricted Queue
10 20 30 40 50
Rear
Front
Front
Rear
Front
Rear
Rear
Front
Rear
Front
Insertion Data item into Queue
Rear
Front
Front
Front
Front
Front
Rear
Rear
To insert data element into queue, we use rear pointer all data element inserted only rear end.
Algorithm for Insert an element into Queue
Step 1: Check if the queue is full.
If rear == max-size-1
Print Queue is full and exit
Step 2: Add and element into Queue
SET REAR = REAR + 1
Step 3: data element insert at rear pointer .
Set QUEUE[REAR] = NUM
Step 4: return success.
10
10 20
10 20 30
10 20 30 40
Rear
Rear
10 20 30 40 50
Rear
Front
Deletion Data item From Queue
Algorithm for Delete an element From Queue
Step 1: Check if the queue is Empty.
If FRONT == -1 or FRONT >REAR
Print Queue is Empty and exit
Step 2: Delete an data element from Queue
SET FRONT = FRONT + 1
Step 3: data element insert at rear pointer .
Set Data element = QUEUE[FRONT]
Step 4: return success.
To Delete data element from queue, we use front pointer all data elements delete
only front end.
10 20 30 40 50
Front
Rear
20 30 40 50
30 40 50
40 50
50
Front
Front
Front
Front
Rear
Rear
Rear
Rear
Circular Queue
Circular Queue is a linear data structure in
which the operations are performed based
on FIFO (First In First Out) principle and
the last position is connected back to the
first position to make a circle. Circular
queue also follow contiguous allocation of
data element. In the circular queue, for
insertion and deletion we use rear and front
pointer like linear queue
Thanks and Regards:
Anil Kumar Prajapati

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Expression trees
Expression treesExpression trees
Expression trees
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
Link_List.pptx
Link_List.pptxLink_List.pptx
Link_List.pptx
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
stack & queue
stack & queuestack & queue
stack & queue
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
Stack
StackStack
Stack
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
Linked list
Linked listLinked list
Linked list
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Queues
QueuesQueues
Queues
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Queue
QueueQueue
Queue
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 

Similar a Stack and queue

Similar a Stack and queue (20)

Stack and queue
Stack and queueStack and queue
Stack and queue
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
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
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue
QueueQueue
Queue
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Queue
QueueQueue
Queue
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Queue
QueueQueue
Queue
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
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
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
 
Queue and its operations
Queue and its operationsQueue and its operations
Queue and its operations
 

Más de Anil Kumar Prajapati

Más de Anil Kumar Prajapati (7)

Artificial Intelligence(Machine learning & deep Learning ).pptx
Artificial Intelligence(Machine learning & deep Learning ).pptxArtificial Intelligence(Machine learning & deep Learning ).pptx
Artificial Intelligence(Machine learning & deep Learning ).pptx
 
Intellectual Property Rights.pptx
Intellectual Property Rights.pptxIntellectual Property Rights.pptx
Intellectual Property Rights.pptx
 
Naac criteria
Naac criteriaNaac criteria
Naac criteria
 
Digital logic Gates of Computer Science
Digital logic Gates of Computer ScienceDigital logic Gates of Computer Science
Digital logic Gates of Computer Science
 
Naac criteria
Naac criteriaNaac criteria
Naac criteria
 
Computer Number system
Computer Number systemComputer Number system
Computer Number system
 
Data structure power point presentation
Data structure power point presentation Data structure power point presentation
Data structure power point presentation
 

Último

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
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
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.docxRamakrishna Reddy Bijjam
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
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.pptxMaritesTamaniVerdade
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 

Último (20)

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 ...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).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
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
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
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

Stack and queue

  • 1. Stack & Queue DATA STRUCTURES Govt. Madhav Science College Ujjain Department of Computer ScienceAnd application PROF. ANIL KUMAR PRAJAPATI
  • 2. What is a stack? ◦ Stack is linear data structure, working in last in first out(LIFO) manner. It is an Abstract Data Type (ADT), commonly used in most programming languages. ◦ In this type of data structure, we can extract the date from the end from which we enter the data. ◦ Push operation is used to enter data in stack and pop operation is used to remove data.
  • 3. Stack Representation Top Data Element 04 Data Element 03 Data Element 02 Data Element 01 Top Data Element 04 Data Element 03 Data Element 02 Data Element 01 Data Element 05 Push Data Element 05 Pop Insertion Deletion
  • 4. Algorithm for insert data into stack Step 1. Check stack if full If Top=Max-1 Print stack is full and exit Step 2: Increment to by one Top=Top+1 Step 3: Insert data into stack Stack[TOP]=data Step 4: End 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 Push 2 Push 3 Push 4 Push 5 Push 1 Max size
  • 5. Push (Insert data item into Stack) For inserting data item into stack we use push operation. Insertion Top Data 3 Data 2 Data 1 Data 1, Data 2, Data 3, Data 4, Data 5, Insertion Top Data 4 Data 3 Data 2 Data 1 Insertion Top Data 2 Data 1 Insertion Top Data 1 Push
  • 6. Algorithm for insert data into stack Step 1: Check stack is empty If TOP=-1 Print Stack is empty and Exit Step 2: delete an element from stack top Set Del_element=Stack[Top] Step 3: decrement stack size by one Top=Top-1 Step 4: End 5 4 3 2 1 4 3 2 1 3 2 1 2 1 Max size Pop 5 Pop 4 Pop 3 1 Pop 2
  • 7. Pop (Deleting data item from Stack) for deleting data item into stack we use Pop operation. Deletion Top Data 2 Data 1 Data 1, Data 2, Data 3, Data 4, Data 5, Deletion Top Data 1 Deletion Top Data 3 Data 2 Data 1 Deletion Top Data 4 Data 3 Data 2 Data 1 pop
  • 8. What is Queue  A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In Queue the allocation of data are continuous manner.  insertion and deletion is done from different ends in the Queue, using rear for insertion and front for Dilation.  Queue having two different formats linear queue and circular queue.
  • 9. Types of Queue broadly Queue are divided into two types such as linear and circular but linear queue having also two different variations, double ended queue or Dequeue and insertion restricted and deletion restricted. 10 20 30 40 50 Front Rear Linear Queue 10 50 30 20 40 60 Circular Queue Rear Front 10 20 30 40 50 10 20 30 40 50 Deletion restricted Queue Double Ended Queue Insertion restricted Queue 10 20 30 40 50 Rear Front Front Rear Front Rear Rear Front Rear Front
  • 10. Insertion Data item into Queue Rear Front Front Front Front Front Rear Rear To insert data element into queue, we use rear pointer all data element inserted only rear end. Algorithm for Insert an element into Queue Step 1: Check if the queue is full. If rear == max-size-1 Print Queue is full and exit Step 2: Add and element into Queue SET REAR = REAR + 1 Step 3: data element insert at rear pointer . Set QUEUE[REAR] = NUM Step 4: return success. 10 10 20 10 20 30 10 20 30 40 Rear Rear 10 20 30 40 50 Rear Front
  • 11. Deletion Data item From Queue Algorithm for Delete an element From Queue Step 1: Check if the queue is Empty. If FRONT == -1 or FRONT >REAR Print Queue is Empty and exit Step 2: Delete an data element from Queue SET FRONT = FRONT + 1 Step 3: data element insert at rear pointer . Set Data element = QUEUE[FRONT] Step 4: return success. To Delete data element from queue, we use front pointer all data elements delete only front end. 10 20 30 40 50 Front Rear 20 30 40 50 30 40 50 40 50 50 Front Front Front Front Rear Rear Rear Rear
  • 12. Circular Queue Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. Circular queue also follow contiguous allocation of data element. In the circular queue, for insertion and deletion we use rear and front pointer like linear queue
  • 13. Thanks and Regards: Anil Kumar Prajapati