SlideShare a Scribd company logo
1 of 24
Stacks and Queues
CSC220 Data Structure
Winter 2004-5
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

Learning R and Teaching R
Learning R and Teaching RLearning R and Teaching R
Learning R and Teaching RAjay Ohri
 
TMPA-2017: Static Checking of Array Objects in JavaScript
TMPA-2017: Static Checking of Array Objects in JavaScriptTMPA-2017: Static Checking of Array Objects in JavaScript
TMPA-2017: Static Checking of Array Objects in JavaScriptIosif Itkin
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queuesAbbott
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queuesBuxoo Abdullah
 
Session 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing BasicsSession 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing BasicsPawanMM
 
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 Lecturergomathi chlm
 
All python data_analyst_r_course
All python data_analyst_r_courseAll python data_analyst_r_course
All python data_analyst_r_courseKamal A
 
Gems in the python standard library
Gems in the python standard libraryGems in the python standard library
Gems in the python standard libraryjasonscheirer
 
Session 20 - Collections - Maps
Session 20 - Collections - MapsSession 20 - Collections - Maps
Session 20 - Collections - MapsPawanMM
 
The TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake Bolewski
The TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake BolewskiThe TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake Bolewski
The TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake BolewskiPyData
 

What's hot (20)

Introduction to HDF5 Data and Programming Models
Introduction to HDF5 Data and Programming ModelsIntroduction to HDF5 Data and Programming Models
Introduction to HDF5 Data and Programming Models
 
Learning R and Teaching R
Learning R and Teaching RLearning R and Teaching R
Learning R and Teaching R
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
TMPA-2017: Static Checking of Array Objects in JavaScript
TMPA-2017: Static Checking of Array Objects in JavaScriptTMPA-2017: Static Checking of Array Objects in JavaScript
TMPA-2017: Static Checking of Array Objects in JavaScript
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
ORAM
ORAMORAM
ORAM
 
Session 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing BasicsSession 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing Basics
 
Queues
QueuesQueues
Queues
 
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
 
All python data_analyst_r_course
All python data_analyst_r_courseAll python data_analyst_r_course
All python data_analyst_r_course
 
Gems in the python standard library
Gems in the python standard libraryGems in the python standard library
Gems in the python standard library
 
Chapter 11 ds
Chapter 11 dsChapter 11 ds
Chapter 11 ds
 
Session 20 - Collections - Maps
Session 20 - Collections - MapsSession 20 - Collections - Maps
Session 20 - Collections - Maps
 
Queue
QueueQueue
Queue
 
The TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake Bolewski
The TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake BolewskiThe TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake Bolewski
The TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake Bolewski
 

Similar to linked list in c++

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 computerabinathsabi
 
Stack and queue
Stack and queueStack and queue
Stack and queueLavanyaJ28
 
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.pptxsabithabanu83
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptxVandanaBharti21
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming LanguageSteve Johnson
 
DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfArumugam90
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using javaNarayan Sau
 
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 QueuesFerdin Joe John Joseph PhD
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.pptDevliNeeraj
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.pptAshok280385
 

Similar to linked list in c++ (20)

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
 
stack & queue
stack & queuestack & queue
stack & queue
 
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
 
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
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptx
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
lecture1.ppt
lecture1.pptlecture1.ppt
lecture1.ppt
 
DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdf
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
 
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
 
Data structures
Data structuresData structures
Data structures
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt
 

Recently uploaded

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
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
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
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
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
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
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 

Recently uploaded (20)

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
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
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
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
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).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
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 

linked list in c++

  • 1. Stacks and Queues CSC220 Data Structure Winter 2004-5
  • 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>