STACKS &
QUEUES
Presented by :-
MANJU RANI
DATA
STRUCTURE
LINEAR DATA
STRUCTURE
ARRAY QUEUE STACK
NON LINEAR
DATA
STRUCTURE
What is Linear Data Structure
In linear data structure, data is arranged
in linear sequence.
 Data items can be traversed in a single
run.
 In linear data structure elements are
accessed or placed in contiguous(together
in sequence) memory location.
WHAT Is
 A stack is called a last-in-first-out (LIFO)
collection. This means that the last thing
we added (pushed) is the first thing that
gets pulled (popped) off.
 A stack is a sequence of items that are
accessible at only one end of the sequence.
EXAMPLES OF STACK:
Operations that can be performed
on STACK:
 PUSH.
 POP.
PUSH : It is used to insert items into the stack.
POP: It is used to delete items from stack.
TOP: It represents the current location of data
in stack.
ALGORITHM OF INSERTION IN
STACK: (PUSH)
1. Insertion(a,top,item,max)
2. If top=max then
print ‘STACK OVERFLOW’
exit
else
3. top=top+1
end if
4. a[top]=item
5. Exit
ALGORITHM OF DELETION IN
STACK: (POP)
1. Deletion(a,top,item)
2. If top=0 then
print ‘STACK UNDERFLOW’
exit
else
3. item=a[top]
end if
4. top=top-1
5. Exit
ALGORITHM OF DISPLAY IN
STACK:
1.Display(top,i,a[i])
2.If top=0 then
Print ‘STACK EMPTY’
Exit
Else
3.For i=top to 0
Print a[i]
End for
4.exit
APPLICATIONS OF STACKS ARE:
I. Reversing Strings:
• A simple application of stack is reversing strings.
To reverse a string , the characters of string are
pushed onto the stack one by one as the string
is read from left to right.
• Once all the characters
of string are pushed onto stack, they are
popped one by one. Since the character last
pushed in comes out first, subsequent pop
operation results in the reversal of the string.
For example:
To reverse the string ‘REVERSE’ the string is
read from left to right and its characters are
pushed . LIKE:
onto a stack.
II. Checking the validity of an expression
containing nested parenthesis:
• Stacks are also used to check whether a given
arithmetic expressions containing nested
parenthesis is properly parenthesized.
• The program for checking the validity of an
expression verifies that for each left parenthesis
braces or bracket ,there is a corresponding
closing symbol and symbols are appropriately
nested.
For example:
VALID INPUTS INVALID INPUTS
{ }
( { [ ] } )
{ [ ] ( ) }
[ { ( { } [ ] ( {
})}]
{ ( }
( [ ( ( ) ] )
{ } [ ] )
[ { ) } ( ] } ]
III. Evaluating arithmetic expressions:
INFIX notation:
The general way of writing arithmetic
expressions is known as infix notation.
e.g, (a+b)
PREFIX notation:
e.g, +AB
POSTFIX notation:
e.g: AB+
Conversion of INFIX to POSTFIX conversion:
Example: 2+(4-1)*3 step1
2+41-*3 step2
2+41-3* step3
241-3*+ step4
CURRENT
SYMBOL
ACTION
PERFORMED
STACK STATUS POSTFIX
EXPRESSION
( PUSH C C 2
2 2
+ PUSH + (+ 2
( PUSH ( (+( 24
4 24
- PUSH - (+(- 241
1 POP 241-
) (+ 241-
* PUSH * (+* 241-
3 241-3
POP * 241-3*
POP + 241-3*+
)
CONVERSION OF INFIX INTO POSTFIX
2+(4-1)*3 into 241-3*+
 Queue is an ADT data structure similar to stack,
except that the first item to be inserted is the first
one to be removed.
 This mechanism is called First-In-First-Out (FIFO).
 Placing an item in a queue is called “insertion or
enqueue”, which is done at the end of the queue
called “rear”.
 Removing an item from a queue is called “deletion
or dequeue”, which is done at the other end of the
queue called “front”.
 Some of the applications are : printer queue,
keystroke queue, etc.
1.To insert an element in
queue
2.Delete an element from
queue
Placing an item in a queue is called
“insertion or enqueue”, which is done
at the end of the queue called “rear”.
Front
Rear
Removing an item from a queue is
called “deletion or dequeue”, which
is done at the other end of the
queue called “front”.
Front
Rear
1.If (rear = maxsize-1 )
print (“queue overflow”) and return
2.Else
rear = rear + 1
Queue [rear] = item
1.If (front =rear)
print “queue empty” and return
2. Else
Front = front + 1
item = queue [front];
Return item
 Real life examples
Waiting in line
Waiting on hold for tech support
 Applications related to Computer Science
Round robin scheduling
Job scheduling (FIFO Scheduling)
Key board buffer
1.Queue is empty
FRONT=REAR
2.Queue is full
REAR=N
3.Queue contains element >=1
FRONT<REAR
NO. OF ELEMENT=REAR-FRONT+1
1.Using an array
2.Using linked list
 To solve this problem, queues implement
wrapping around. Such queues are called
Circular Queues.
 Both the front and the rear pointers wrap
around to the beginning of the array.
 It is also called as “Ring buffer”.
 Items can inserted and deleted from a
queue in O(1) time.
+Queue()
+insert() : void
+remove() : long
+peekFront() : long
+isEmpty() : bool
+isFull() : bool
+size() : int
-maxSize : int
-queueArray [] : long
-front : int
-rear : int
-nItems : int
Queue
QueueApp
Interface1
 C:Documents and SettingsboxMy
DocumentsCSCSC220ReaderProgramsRe
aderFilesChap04Queuequeue.java
 Normal queue (FIFO)
 Circular Queue (Normal Queue)
 Double-ended Queue (Deque)
 Priority Queue
 It is a double-ended queue.
 Items can be inserted and deleted from
either ends.
 More versatile data structure than stack or
queue.
 E.g. policy-based application (e.g. low
priority go to the end, high go to the front)
 In a case where you want to sort the queue
once in a while, What sorting algorithm will
you use?
 More specialized data structure.
 Similar to Queue, having front and rear.
 Items are removed from the front.
 Items are ordered by key value so that the
item with the lowest key (or highest) is
always at the front.
 Items are inserted in proper position to
maintain the order.
 Let’s discuss complexity
+Queue()
+insert() : void
+remove() : long
+peekMin() : long
+isEmpty() : bool
+isFull() : bool
-maxSize : int
-queueArray [] : long
-nItems : int
PrioityQ
PriorityQApp
Interface1
 Used in multitasking operating system.
 They are generally represented using “heap”
data structure.
 Insertion runs in O(n) time, deletion in O(1)
time.
 C:Documents and SettingsboxMy
DocumentsCSCSC220ReaderProgramsRe
aderFilesChap04PriorityQpriorityQ.java
 2 + 3
 2 + 4 * 5
 ((2 + 4) * 7) + 3* (9 – 5))
 Infix vs postfix
 Why do we want to do this
transformation?
• 2 3 +
• 2 4 5 * +
• 2 4 + 7 * 3 9 5 - * +
 Read ch from input until empty
◦ If ch is arg , output = output + arg
◦ If ch is “(“, push ‘(‘;
◦ If ch is op and higher than top push ch
◦ If ch is “)” or end of input,
 output = output + pop() until empty or top is “(“
◦ Read next input
 C:Documents and SettingsboxMy
DocumentsCSCSC220ReaderPrograms
ReaderFilesChap04Postfixpostfix.java
 5 + 2 * 3 -> 5 2 3 * +
 Algorithm
◦ While input is not empty
◦ If ch is number , push (ch)
◦ Else
 Pop (a)
 Pop(b)
 Eval (ch, a, b)
 C:Documents and SettingsboxMy
DocumentsCSCSC220ReaderProgramsR
eaderFilesChap04Postfixpostfix.java
 XML – Wave of the future
 <?xml version = "1.0"?>
 <!-- An author -->
 <author>
 <name gender = "male">
 <first> Art </first>
 <last> Gittleman </last>
 </name>
 </author>
stack & queue

More Related Content

PDF
PPTX
queue & its applications
PPT
Queue data structure
PPTX
Queue in Data Structure
PPTX
Stacks IN DATA STRUCTURES
PPTX
PPTX
linked list in data structure
PPTX
Circular link list.ppt
queue & its applications
Queue data structure
Queue in Data Structure
Stacks IN DATA STRUCTURES
linked list in data structure
Circular link list.ppt

What's hot (20)

PPT
Binary Search
PPTX
Quick sort-Data Structure
PPTX
Binary Search Tree in Data Structure
PPTX
sorting and its types
PPTX
Threaded Binary Tree
PPTX
Insertion sort
PPTX
Sparse matrix and its representation data structure
PPT
Data structures using c
PPTX
Stack & Queue using Linked List in Data Structure
PPTX
Linked List - Insertion & Deletion
PPTX
Presentation on queue
PPTX
Linked list
PPTX
Insertion Sorting
PPTX
Python Exception Handling
PPT
PPTX
Data structure & its types
PPTX
Binary Tree Traversal
PPTX
Data structure and its types
PPT
Binary search tree(bst)
PPTX
Tree traversal techniques
Binary Search
Quick sort-Data Structure
Binary Search Tree in Data Structure
sorting and its types
Threaded Binary Tree
Insertion sort
Sparse matrix and its representation data structure
Data structures using c
Stack & Queue using Linked List in Data Structure
Linked List - Insertion & Deletion
Presentation on queue
Linked list
Insertion Sorting
Python Exception Handling
Data structure & its types
Binary Tree Traversal
Data structure and its types
Binary search tree(bst)
Tree traversal techniques
Ad

Viewers also liked (20)

PPT
Stacks & Queues By Ms. Niti Arora
PPT
Stack and queue
PPT
Queue and stacks
PPT
Stack & queue
PPSX
PPTX
STACKS IN DATASTRUCTURE
PPT
Stack & queues
PPTX
Data Structure -List Stack Queue
PPTX
Stack and Queue
PPTX
Stack and queue
PPTX
Deque and its applications
PPT
Array Presentation (EngineerBaBu.com)
PPT
Queue Data Structure
PDF
Queue as data_structure
PPT
Notes DATA STRUCTURE - queue
PPT
DATA STRUCTURES
DOCX
DataStructures notes
PDF
Stack and Queue (brief)
PPTX
B tree &amp;
PPT
stack and queue array implementation in java.
Stacks & Queues By Ms. Niti Arora
Stack and queue
Queue and stacks
Stack & queue
STACKS IN DATASTRUCTURE
Stack & queues
Data Structure -List Stack Queue
Stack and Queue
Stack and queue
Deque and its applications
Array Presentation (EngineerBaBu.com)
Queue Data Structure
Queue as data_structure
Notes DATA STRUCTURE - queue
DATA STRUCTURES
DataStructures notes
Stack and Queue (brief)
B tree &amp;
stack and queue array implementation in java.
Ad

Similar to stack & queue (20)

PPT
Data Structures by Maneesh Boddu
PPT
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
PPT
linked list in c++
PPTX
Stack and Queue.pptx
PPTX
Queues in C++
PPTX
DS UNIT 2 PPT.pptx stack queue representations
PPTX
queue.pptx
PPTX
Stack & Queue
PPTX
UNIT II LINEAR DATA STRUCTURES – STACKS.pptx
PPTX
UNIT II LINEAR DATA STRUCTURES – STACKS.pptx
PPTX
DSA_Ques ewoifhjerofhefhehfreofheek.pptx
PPTX
Data Structures and Agorithm: DS 09 Queue.pptx
PPTX
PPTX
Stack and queue
PPTX
Data Structures - Lecture 2 - Unit 2.pptx
PPT
Stack and queue
PPTX
Bca ii dfs u-2 linklist,stack,queue
PPTX
Bsc cs ii dfs u-2 linklist,stack,queue
PPT
Queues in C++ detailed explanation and examples .ppt
PDF
CHAPTER 4 - DATA STRUCTURES QUEUES CHAPTER
Data Structures by Maneesh Boddu
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
linked list in c++
Stack and Queue.pptx
Queues in C++
DS UNIT 2 PPT.pptx stack queue representations
queue.pptx
Stack & Queue
UNIT II LINEAR DATA STRUCTURES – STACKS.pptx
UNIT II LINEAR DATA STRUCTURES – STACKS.pptx
DSA_Ques ewoifhjerofhefhehfreofheek.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
Stack and queue
Data Structures - Lecture 2 - Unit 2.pptx
Stack and queue
Bca ii dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
Queues in C++ detailed explanation and examples .ppt
CHAPTER 4 - DATA STRUCTURES QUEUES CHAPTER

Recently uploaded (20)

PDF
IS1343_2012...........................pdf
PDF
Diabetes Mellitus , types , clinical picture, investigation and managment
PPTX
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
PPT
hsl powerpoint resource goyloveh feb 07.ppt
PPTX
IT infrastructure and emerging technologies
DOCX
THEORY AND PRACTICE ASSIGNMENT SEMESTER MAY 2025.docx
PPTX
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
PDF
African Communication Research: A review
PDF
Physical pharmaceutics two in b pharmacy
PDF
anganwadi services for the b.sc nursing and GNM
PPTX
climate change of delhi impacts on climate and there effects
PPTX
PAIN PATHWAY & MANAGEMENT OF ACUTE AND CHRONIC PAIN SPEAKER: Dr. Rajasekhar ...
PPTX
Neurological complocations of systemic disease
PPTX
Power Point PR B.Inggris 12 Ed. 2019.pptx
PPTX
MMW-CHAPTER-1-final.pptx major Elementary Education
PDF
FAMILY PLANNING (preventative and social medicine pdf)
PPTX
UCSP Section A - Human Cultural Variations,Social Differences,social ChangeCo...
PDF
Chevening Scholarship Application and Interview Preparation Guide
PPSX
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
PPTX
Theoretical for class.pptxgshdhddhdhdhgd
IS1343_2012...........................pdf
Diabetes Mellitus , types , clinical picture, investigation and managment
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
hsl powerpoint resource goyloveh feb 07.ppt
IT infrastructure and emerging technologies
THEORY AND PRACTICE ASSIGNMENT SEMESTER MAY 2025.docx
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
African Communication Research: A review
Physical pharmaceutics two in b pharmacy
anganwadi services for the b.sc nursing and GNM
climate change of delhi impacts on climate and there effects
PAIN PATHWAY & MANAGEMENT OF ACUTE AND CHRONIC PAIN SPEAKER: Dr. Rajasekhar ...
Neurological complocations of systemic disease
Power Point PR B.Inggris 12 Ed. 2019.pptx
MMW-CHAPTER-1-final.pptx major Elementary Education
FAMILY PLANNING (preventative and social medicine pdf)
UCSP Section A - Human Cultural Variations,Social Differences,social ChangeCo...
Chevening Scholarship Application and Interview Preparation Guide
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
Theoretical for class.pptxgshdhddhdhdhgd

stack & queue

  • 2. DATA STRUCTURE LINEAR DATA STRUCTURE ARRAY QUEUE STACK NON LINEAR DATA STRUCTURE
  • 3. What is Linear Data Structure In linear data structure, data is arranged in linear sequence.  Data items can be traversed in a single run.  In linear data structure elements are accessed or placed in contiguous(together in sequence) memory location.
  • 4. WHAT Is  A stack is called a last-in-first-out (LIFO) collection. This means that the last thing we added (pushed) is the first thing that gets pulled (popped) off.  A stack is a sequence of items that are accessible at only one end of the sequence.
  • 6. Operations that can be performed on STACK:  PUSH.  POP.
  • 7. PUSH : It is used to insert items into the stack. POP: It is used to delete items from stack. TOP: It represents the current location of data in stack.
  • 8. ALGORITHM OF INSERTION IN STACK: (PUSH) 1. Insertion(a,top,item,max) 2. If top=max then print ‘STACK OVERFLOW’ exit else 3. top=top+1 end if 4. a[top]=item 5. Exit
  • 9. ALGORITHM OF DELETION IN STACK: (POP) 1. Deletion(a,top,item) 2. If top=0 then print ‘STACK UNDERFLOW’ exit else 3. item=a[top] end if 4. top=top-1 5. Exit
  • 10. ALGORITHM OF DISPLAY IN STACK: 1.Display(top,i,a[i]) 2.If top=0 then Print ‘STACK EMPTY’ Exit Else 3.For i=top to 0 Print a[i] End for 4.exit
  • 11. APPLICATIONS OF STACKS ARE: I. Reversing Strings: • A simple application of stack is reversing strings. To reverse a string , the characters of string are pushed onto the stack one by one as the string is read from left to right. • Once all the characters of string are pushed onto stack, they are popped one by one. Since the character last pushed in comes out first, subsequent pop operation results in the reversal of the string.
  • 12. For example: To reverse the string ‘REVERSE’ the string is read from left to right and its characters are pushed . LIKE: onto a stack.
  • 13. II. Checking the validity of an expression containing nested parenthesis: • Stacks are also used to check whether a given arithmetic expressions containing nested parenthesis is properly parenthesized. • The program for checking the validity of an expression verifies that for each left parenthesis braces or bracket ,there is a corresponding closing symbol and symbols are appropriately nested.
  • 14. For example: VALID INPUTS INVALID INPUTS { } ( { [ ] } ) { [ ] ( ) } [ { ( { } [ ] ( { })}] { ( } ( [ ( ( ) ] ) { } [ ] ) [ { ) } ( ] } ]
  • 15. III. Evaluating arithmetic expressions: INFIX notation: The general way of writing arithmetic expressions is known as infix notation. e.g, (a+b) PREFIX notation: e.g, +AB POSTFIX notation: e.g: AB+
  • 16. Conversion of INFIX to POSTFIX conversion: Example: 2+(4-1)*3 step1 2+41-*3 step2 2+41-3* step3 241-3*+ step4
  • 17. CURRENT SYMBOL ACTION PERFORMED STACK STATUS POSTFIX EXPRESSION ( PUSH C C 2 2 2 + PUSH + (+ 2 ( PUSH ( (+( 24 4 24 - PUSH - (+(- 241 1 POP 241- ) (+ 241- * PUSH * (+* 241- 3 241-3 POP * 241-3* POP + 241-3*+ ) CONVERSION OF INFIX INTO POSTFIX 2+(4-1)*3 into 241-3*+
  • 18.  Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed.  This mechanism is called First-In-First-Out (FIFO).  Placing an item in a queue is called “insertion or enqueue”, which is done at the end of the queue called “rear”.  Removing an item from a queue is called “deletion or dequeue”, which is done at the other end of the queue called “front”.  Some of the applications are : printer queue, keystroke queue, etc.
  • 19. 1.To insert an element in queue 2.Delete an element from queue
  • 20. Placing an item in a queue is called “insertion or enqueue”, which is done at the end of the queue called “rear”. Front Rear
  • 21. Removing an item from a queue is called “deletion or dequeue”, which is done at the other end of the queue called “front”. Front Rear
  • 22. 1.If (rear = maxsize-1 ) print (“queue overflow”) and return 2.Else rear = rear + 1 Queue [rear] = item
  • 23. 1.If (front =rear) print “queue empty” and return 2. Else Front = front + 1 item = queue [front]; Return item
  • 24.  Real life examples Waiting in line Waiting on hold for tech support  Applications related to Computer Science Round robin scheduling Job scheduling (FIFO Scheduling) Key board buffer
  • 25. 1.Queue is empty FRONT=REAR 2.Queue is full REAR=N 3.Queue contains element >=1 FRONT<REAR NO. OF ELEMENT=REAR-FRONT+1
  • 27.  To solve this problem, queues implement wrapping around. Such queues are called Circular Queues.  Both the front and the rear pointers wrap around to the beginning of the array.  It is also called as “Ring buffer”.  Items can inserted and deleted from a queue in O(1) time.
  • 28. +Queue() +insert() : void +remove() : long +peekFront() : long +isEmpty() : bool +isFull() : bool +size() : int -maxSize : int -queueArray [] : long -front : int -rear : int -nItems : int Queue QueueApp Interface1
  • 29.  C:Documents and SettingsboxMy DocumentsCSCSC220ReaderProgramsRe aderFilesChap04Queuequeue.java
  • 30.  Normal queue (FIFO)  Circular Queue (Normal Queue)  Double-ended Queue (Deque)  Priority Queue
  • 31.  It is a double-ended queue.  Items can be inserted and deleted from either ends.  More versatile data structure than stack or queue.  E.g. policy-based application (e.g. low priority go to the end, high go to the front)  In a case where you want to sort the queue once in a while, What sorting algorithm will you use?
  • 32.  More specialized data structure.  Similar to Queue, having front and rear.  Items are removed from the front.  Items are ordered by key value so that the item with the lowest key (or highest) is always at the front.  Items are inserted in proper position to maintain the order.  Let’s discuss complexity
  • 33. +Queue() +insert() : void +remove() : long +peekMin() : long +isEmpty() : bool +isFull() : bool -maxSize : int -queueArray [] : long -nItems : int PrioityQ PriorityQApp Interface1
  • 34.  Used in multitasking operating system.  They are generally represented using “heap” data structure.  Insertion runs in O(n) time, deletion in O(1) time.  C:Documents and SettingsboxMy DocumentsCSCSC220ReaderProgramsRe aderFilesChap04PriorityQpriorityQ.java
  • 35.  2 + 3  2 + 4 * 5  ((2 + 4) * 7) + 3* (9 – 5))  Infix vs postfix  Why do we want to do this transformation? • 2 3 + • 2 4 5 * + • 2 4 + 7 * 3 9 5 - * +
  • 36.  Read ch from input until empty ◦ If ch is arg , output = output + arg ◦ If ch is “(“, push ‘(‘; ◦ If ch is op and higher than top push ch ◦ If ch is “)” or end of input,  output = output + pop() until empty or top is “(“ ◦ Read next input  C:Documents and SettingsboxMy DocumentsCSCSC220ReaderPrograms ReaderFilesChap04Postfixpostfix.java
  • 37.  5 + 2 * 3 -> 5 2 3 * +  Algorithm ◦ While input is not empty ◦ If ch is number , push (ch) ◦ Else  Pop (a)  Pop(b)  Eval (ch, a, b)  C:Documents and SettingsboxMy DocumentsCSCSC220ReaderProgramsR eaderFilesChap04Postfixpostfix.java
  • 38.  XML – Wave of the future
  • 39.  <?xml version = "1.0"?>  <!-- An author -->  <author>  <name gender = "male">  <first> Art </first>  <last> Gittleman </last>  </name>  </author>