SlideShare a Scribd company logo
Stacks and Queues
CSE220 Data Structure
Abstract Data Type
• Abstract Data Type as a design tool
• Concerns only on the important concept or
model
• No concern on implementation details.
• Stack & Queue is an example of ADT
• An array is not ADT.
What is the
difference?
• Stack & Queue vs. Array
– Arrays are data storage structures while stacks and
queues are specialized DS and used as programmer’s
tools.
• Stack – a container that allows push and pop
• Queue - a container that allows enqueue and
dequeue
• No concern on implementation details.
• In an array any item can be accessed, while in
these data structures access is restricted.
• They are more abstract than arrays.
Questions?
• Array is not ADT
• Is Linked list ADT?
• Is Binary-tree ADT?
• Is Hash table ADT?
• What about graph?
Stacks
• Allows access to only the last item inserted.
• An item is inserted or removed from the stack
from one end called the “top” of the stack.
• This mechanism is called Last-In-First-Out
(LIFO).
A Stack Applet example
Stack operations
• Placing a data item on the top is called
“pushing”, while removing an item from the top is
called “popping” it.
• push and pop are the primary stack operations.
• Some of the applications are : microprocessors,
some older calculators etc.
Example of Stack codes
• First example stack ADT and implementation
C:Documents and SettingsboxMy DocumentsCSCSC220ReaderProgramsReaderFilesCh
• push and pop operations are performed in O(1)
time.
Example of Stack codes
• Reversed word
• What is it?
• ABC -> CBA
C:Documents and SettingsboxMy DocumentsCS
Example of Stack codes
• BracketChecker (balancer)
• A syntax checker (compiler) that understands a
language containing any strings with balanced
brackets ‘{‘ ‘[‘ ‘(‘ and ‘)’, ‘]’, ‘}’
– S -> Bl S1 Br
– S1 -> Bl string Br
– Bl -> ‘{‘ | ‘[‘ | ‘(‘
– Br -> ‘)’, | ‘]’, | ‘}’
C:Documents and SettingsboxMy
DocumentsCSCSC220ReaderProgramsReaderFile
sChap04Bracketsbrackets.java
Queues
• 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.
Circular Queue
• When a new item is inserted at the rear, the
pointer to rear moves upwards.
• Similarly, when an item is deleted from the queue
the front arrow moves downwards.
• After a few insert and delete operations the rear
might reach the end of the queue and no more
items can be inserted although the items from
the front of the queue have been deleted and
there is space in the queue.
Circular Queue
• 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 Example
+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
Queue sample code
• C:Documents and SettingsboxMy Documen
Various Queues
• Normal queue (FIFO)
• Circular Queue (Normal Queue)
• Double-ended Queue (Deque)
• Priority Queue
Deque
• 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?
Priority Queues
• 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
Priority Queue
Example
+Queue()
+insert() : void
+remove() : long
+peekMin() : long
+isEmpty() : bool
+isFull() : bool
-maxSize : int
-queueArray [] : long
-nItems : int
PrioityQ
PriorityQApp
Interface1
Priority Queues
• 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
DocumentsCSCSC220ReaderPrograms
ReaderFilesChap04PriorityQpriorityQ.ja
va
Parsing Arithmetic
Expressions
• 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 - * +
Infix to postfix
• 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
DocumentsCSCSC220ReaderProgramsRead
erFilesChap04Postfixpostfix.java
Postfix eval
• 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
DocumentsCSCSC220ReaderProgramsRead
erFilesChap04Postfixpostfix.java
Quick XML Review
• XML – Wave of the future
Another Real world
example
• <?xml version = "1.0"?>
• <!-- An author -->
• <author>
• <name gender = "male">
• <first> Art </first>
• <last> Gittleman </last>
• </name>
• </author>

More Related Content

What's hot

Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
Selvin Josy Bai Somu
 
02 Stack
02 Stack02 Stack
Queue ppt
Queue pptQueue ppt
Queue ppt
SouravKumar328
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
Dr. Jasmine Beulah Gnanadurai
 
Stack and queue
Stack and queueStack and queue
Stack and queue
CHANDAN KUMAR
 
Chapter 11 ds
Chapter 11 dsChapter 11 ds
Chapter 11 ds
Hanif Durad
 
Queue
QueueQueue
Data Structures 01
Data Structures 01Data Structures 01
Data Structures 01
Budditha Hettige
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
anooppjoseph
 
Queues
QueuesQueues
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
Usha Mahalingam
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
A-Tech and Software Development
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
Muhazzab Chouhadry
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
DurgaDeviCbit
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using C
cpjcollege
 
Sorting
SortingSorting
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queuestech4us
 

What's hot (19)

Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
02 Stack
02 Stack02 Stack
02 Stack
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Chapter 11 ds
Chapter 11 dsChapter 11 ds
Chapter 11 ds
 
Queue
QueueQueue
Queue
 
Data Structures 01
Data Structures 01Data Structures 01
Data Structures 01
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Queues
QueuesQueues
Queues
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using C
 
Sorting
SortingSorting
Sorting
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
 

Similar to Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues

Queues
Queues Queues
Queues
nidhisatija1
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
mayankKatiyar17
 
Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
abinathsabi
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
Michael Heron
 
Data Structures
Data StructuresData Structures
Data Structures
Dr.Umadevi V
 
Stack and queue
Stack and queueStack and queue
Stack and queue
LavanyaJ28
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptx
VandanaBharti21
 
II B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxII B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptx
sabithabanu83
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
Muhazzab Chouhadry
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
Steve Johnson
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
Narayan Sau
 
DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdf
Arumugam90
 
lecture1.ppt
lecture1.pptlecture1.ppt
lecture1.ppt
SanjeevKumarSinha13
 
Queues
QueuesQueues
Data Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and QueuesData Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and Queues
Ferdin Joe John Joseph PhD
 
CPP17 - File IO
CPP17 - File IOCPP17 - File IO
CPP17 - File IO
Michael Heron
 
Stack and queue power point presentation data structure and algorithms Stack-...
Stack and queue power point presentation data structure and algorithms Stack-...Stack and queue power point presentation data structure and algorithms Stack-...
Stack and queue power point presentation data structure and algorithms Stack-...
abhaysingh19149
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queue
Sunipa Bera
 
Data structure and algorithm All in One
Data structure and algorithm All in OneData structure and algorithm All in One
Data structure and algorithm All in One
jehan1987
 

Similar to Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues (20)

Queues
Queues Queues
Queues
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
 
Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
Data Structures
Data StructuresData Structures
Data Structures
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptx
 
II B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxII B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptx
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
 
DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdf
 
lecture1.ppt
lecture1.pptlecture1.ppt
lecture1.ppt
 
Queues
QueuesQueues
Queues
 
Data Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and QueuesData Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and Queues
 
CPP17 - File IO
CPP17 - File IOCPP17 - File IO
CPP17 - File IO
 
Stack and queue power point presentation data structure and algorithms Stack-...
Stack and queue power point presentation data structure and algorithms Stack-...Stack and queue power point presentation data structure and algorithms Stack-...
Stack and queue power point presentation data structure and algorithms Stack-...
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queue
 
Data structures
Data structuresData structures
Data structures
 
Data structure and algorithm All in One
Data structure and algorithm All in OneData structure and algorithm All in One
Data structure and algorithm All in One
 

Recently uploaded

Business update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMIBusiness update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMI
AlejandraGmez176757
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
vcaxypu
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
enxupq
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
vcaxypu
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
axoqas
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
ArpitMalhotra16
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
ewymefz
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
Subhajit Sahu
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
ewymefz
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
ewymefz
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
ukgaet
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
axoqas
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
ewymefz
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
TravisMalana
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
enxupq
 

Recently uploaded (20)

Business update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMIBusiness update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMI
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
 

Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues

  • 1. Stacks and Queues CSE220 Data Structure
  • 2. Abstract Data Type • Abstract Data Type as a design tool • Concerns only on the important concept or model • No concern on implementation details. • Stack & Queue is an example of ADT • An array is not ADT.
  • 3. What is the difference? • Stack & Queue vs. Array – Arrays are data storage structures while stacks and queues are specialized DS and used as programmer’s tools. • Stack – a container that allows push and pop • Queue - a container that allows enqueue and dequeue • No concern on implementation details. • In an array any item can be accessed, while in these data structures access is restricted. • They are more abstract than arrays.
  • 4. Questions? • Array is not ADT • Is Linked list ADT? • Is Binary-tree ADT? • Is Hash table ADT? • What about graph?
  • 5. Stacks • Allows access to only the last item inserted. • An item is inserted or removed from the stack from one end called the “top” of the stack. • This mechanism is called Last-In-First-Out (LIFO). A Stack Applet example
  • 6. Stack operations • Placing a data item on the top is called “pushing”, while removing an item from the top is called “popping” it. • push and pop are the primary stack operations. • Some of the applications are : microprocessors, some older calculators etc.
  • 7. Example of Stack codes • First example stack ADT and implementation C:Documents and SettingsboxMy DocumentsCSCSC220ReaderProgramsReaderFilesCh • push and pop operations are performed in O(1) time.
  • 8. Example of Stack codes • Reversed word • What is it? • ABC -> CBA C:Documents and SettingsboxMy DocumentsCS
  • 9. Example of Stack codes • BracketChecker (balancer) • A syntax checker (compiler) that understands a language containing any strings with balanced brackets ‘{‘ ‘[‘ ‘(‘ and ‘)’, ‘]’, ‘}’ – S -> Bl S1 Br – S1 -> Bl string Br – Bl -> ‘{‘ | ‘[‘ | ‘(‘ – Br -> ‘)’, | ‘]’, | ‘}’ C:Documents and SettingsboxMy DocumentsCSCSC220ReaderProgramsReaderFile sChap04Bracketsbrackets.java
  • 10. Queues • 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.
  • 11. Circular Queue • When a new item is inserted at the rear, the pointer to rear moves upwards. • Similarly, when an item is deleted from the queue the front arrow moves downwards. • After a few insert and delete operations the rear might reach the end of the queue and no more items can be inserted although the items from the front of the queue have been deleted and there is space in the queue.
  • 12. Circular Queue • 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.
  • 13. Queue Example +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
  • 14. Queue sample code • C:Documents and SettingsboxMy Documen
  • 15. Various Queues • Normal queue (FIFO) • Circular Queue (Normal Queue) • Double-ended Queue (Deque) • Priority Queue
  • 16. Deque • 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?
  • 17. Priority Queues • 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
  • 18. Priority Queue Example +Queue() +insert() : void +remove() : long +peekMin() : long +isEmpty() : bool +isFull() : bool -maxSize : int -queueArray [] : long -nItems : int PrioityQ PriorityQApp Interface1
  • 19. Priority Queues • 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 DocumentsCSCSC220ReaderPrograms ReaderFilesChap04PriorityQpriorityQ.ja va
  • 20. Parsing Arithmetic Expressions • 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 - * +
  • 21. Infix to postfix • 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 DocumentsCSCSC220ReaderProgramsRead erFilesChap04Postfixpostfix.java
  • 22. Postfix eval • 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 DocumentsCSCSC220ReaderProgramsRead erFilesChap04Postfixpostfix.java
  • 23. Quick XML Review • XML – Wave of the future
  • 24. Another Real world example • <?xml version = "1.0"?> • <!-- An author --> • <author> • <name gender = "male"> • <first> Art </first> • <last> Gittleman </last> • </name> • </author>