SlideShare a Scribd company logo
Stack: Linked List
Implementation
 Push and pop at the head of the list
 New nodes should be inserted at the front of the list, so that they
become the top of the stack
 Nodes are removed from the front (top) of the list
 Straight-forward linked list implementation
 push and pop can be implemented fairly easily, e.g. assuming that
head is a reference to the node at the front of the list
public void push(int x){
// Make a new node whose next reference is
// the existing list
Node newNode = new Node(x, top);
top = newNode; // top points to new node
}
List Stack Example
Java Code
Stack st = new Stack();
st.push(6);
top
6
List Stack Example
Java Code
Stack st = new Stack();
st.push(6);
st.push(1);
top
6
1
List Stack Example
Java Code
Stack st = new Stack();
st.push(6);
st.push(1);
st.push(7);
top
6
1
7
List Stack Example
Java Code
Stack st = new Stack();
st.push(6);
st.push(1);
st.push(7);
st.push(8);
top
6
1
7
8
List Stack Example
Java Code
Stack st = new Stack();
st.push(6);
st.push(1);
st.push(7);
st.push(8);
st.pop();
top
6
1
7
8
List Stack Example
Java Code
Stack st = new Stack();
st.push(6);
st.push(1);
st.push(7);
st.push(8);
st.pop();
top
6
1
7
Stack: ADT List
Implementation
 Push() and pop() either at the beginning or at the end
of ADT List
 at the beginning:
public void push(Object newItem) {
list.add(1, newItem);
} // end push
public Object pop() {
Object temp = list.get(1);
list.remove(1);
return temp;
} // end pop
Stack: ADT List
Implementation
 Push() and pop() either at the beginning or at the end
of ADT List
 at the end:
public void push(Object newItem) {
list.add(list.size()+1, newItem);
} // end push
public Object pop() {
Object temp = list.get(list.size());
list.remove(list.size());
return temp;
} // end pop
Stack: ADT List
Implementation
 Push() and pop() either at the beginning or at the end
of ADT List
 Efficiency depends on implementation of ADT List (not
guaranteed)
 On other hand: it was very fast to implement (code is
easy, unlikely that errors were introduced when
coding).
Applications of Stacks
 Call stack (recursion).
 Searching networks, traversing trees
(keeping a track where we are).
Examples:
 Checking balanced expressions
 Recognizing palindromes
 Evaluating algebraic expressions
Simple Applications of the ADT Stack:
Checking for Balanced Braces
 A stack can be used to verify whether a program
contains balanced braces
 An example of balanced braces
abc{defg{ijk}{l{mn}}op}qr
 An example of unbalanced braces
abc{def}}{ghij{kl}m
abc{def}{ghij{kl}m
Checking for Balanced Braces
 Requirements for balanced braces
 Each time you encounter a “}”, it matches an already
encountered “{”
 When you reach the end of the string, you have matched
each “{”
Checking for Balanced Braces
Figure 7-3
Traces of the algorithm that checks for balanced braces
Evaluating Postfix
Expressions
 A postfix (reverse Polish logic) calculator
 Requires you to enter postfix expressions
 Example: 2 3 4 + *
 When an operand is entered, the calculator
 Pushes it onto a stack
 When an operator is entered, the calculator
 Applies it to the top two operands of the stack
 Pops the operands from the stack
 Pushes the result of the operation on the stack
Evaluating Postfix
Expressions
Figure 7-8
The action of a postfix calculator when evaluating the expression 2 * (3 + 4)
Evaluating Postfix Expressions
 Pseudo code:
int evaluate(String expression)
{
Stack stack=new Stack(); // creaty empty stack
while (true) {
String c=expression.getNextItem();
if (c==ENDOFLINE)
return stack.pop();
if (c is operand)
stack.push(c);
else { // operation
int operand2=stack.pop();
int operand1=stack.pop();
stack.push(execute(c,operand1,operand2));
}
}
}
Queues
 A queue is a data structure that only allows items to
be inserted at the end and removed from the front
 “Queue” is the British word for a line (or line-up)
 Queues are FIFO (First In First Out) data structures
– “fair” data structures
Using a Queue
What Can You Use a Queue
For?
 Processing inputs and outputs to screen (console)
 Server requests
 Instant messaging servers queue up incoming
messages
 Database requests
 Print queues
 One printer for dozens of computers
 Operating systems use queues to schedule CPU
jobs
 Simulations
Queue Operations
 A queue should implement (at least) these
operations:
 enqueue – insert an item at the back of the queue
 dequeue – remove an item from the front
 peek – return the item at the front of the queue without
removing it
 Like stacks it is assumed that these operations will
be implemented efficiently
 That is, in constant time
Queue: Array Implementation
 First consider using an array as the underlying
structure for a queue, one plan would be to
 Make the back of the queue the current size of the
queue (i.e., the number of elements stored)
 Make the front of the queue index 0
 Inserting an item can be performed in constant time
 But removing an item would require shifting all elements
in the queue to the left which is too slow!
 Therefore we need to find another way
An Array-Based
Implementation
Figure 8-8
a) A naive array-based implementation of a queue; b) rightward drift can cause the
queue to appear full

More Related Content

Similar to stack.ppt

01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.ppt
soniya555961
 
05-stack_queue.ppt
05-stack_queue.ppt05-stack_queue.ppt
05-stack_queue.ppt
Sarojkumari55
 
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
gomathi chlm
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
Senthil Kumar
 
Lec2
Lec2Lec2
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
Muhazzab Chouhadry
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problem
ecomputernotes
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5ecomputernotes
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
Ssankett Negi
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
iqbalphy1
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Sriram Raj
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Zidny Nafan
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
BG Java EE Course
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptx
line24arts
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
iloveyoucarlo0923
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
MouDhara1
 

Similar to stack.ppt (20)

01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.ppt
 
Lec2
Lec2Lec2
Lec2
 
05-stack_queue.ppt
05-stack_queue.ppt05-stack_queue.ppt
05-stack_queue.ppt
 
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
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
Lec2
Lec2Lec2
Lec2
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problem
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptx
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 

Recently uploaded

Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Soumen Santra
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
ChristineTorrepenida1
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Basic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparelBasic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparel
top1002
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
veerababupersonal22
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 

Recently uploaded (20)

Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Basic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparelBasic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparel
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 

stack.ppt

  • 1. Stack: Linked List Implementation  Push and pop at the head of the list  New nodes should be inserted at the front of the list, so that they become the top of the stack  Nodes are removed from the front (top) of the list  Straight-forward linked list implementation  push and pop can be implemented fairly easily, e.g. assuming that head is a reference to the node at the front of the list public void push(int x){ // Make a new node whose next reference is // the existing list Node newNode = new Node(x, top); top = newNode; // top points to new node }
  • 2. List Stack Example Java Code Stack st = new Stack(); st.push(6); top 6
  • 3. List Stack Example Java Code Stack st = new Stack(); st.push(6); st.push(1); top 6 1
  • 4. List Stack Example Java Code Stack st = new Stack(); st.push(6); st.push(1); st.push(7); top 6 1 7
  • 5. List Stack Example Java Code Stack st = new Stack(); st.push(6); st.push(1); st.push(7); st.push(8); top 6 1 7 8
  • 6. List Stack Example Java Code Stack st = new Stack(); st.push(6); st.push(1); st.push(7); st.push(8); st.pop(); top 6 1 7 8
  • 7. List Stack Example Java Code Stack st = new Stack(); st.push(6); st.push(1); st.push(7); st.push(8); st.pop(); top 6 1 7
  • 8. Stack: ADT List Implementation  Push() and pop() either at the beginning or at the end of ADT List  at the beginning: public void push(Object newItem) { list.add(1, newItem); } // end push public Object pop() { Object temp = list.get(1); list.remove(1); return temp; } // end pop
  • 9. Stack: ADT List Implementation  Push() and pop() either at the beginning or at the end of ADT List  at the end: public void push(Object newItem) { list.add(list.size()+1, newItem); } // end push public Object pop() { Object temp = list.get(list.size()); list.remove(list.size()); return temp; } // end pop
  • 10. Stack: ADT List Implementation  Push() and pop() either at the beginning or at the end of ADT List  Efficiency depends on implementation of ADT List (not guaranteed)  On other hand: it was very fast to implement (code is easy, unlikely that errors were introduced when coding).
  • 11. Applications of Stacks  Call stack (recursion).  Searching networks, traversing trees (keeping a track where we are). Examples:  Checking balanced expressions  Recognizing palindromes  Evaluating algebraic expressions
  • 12. Simple Applications of the ADT Stack: Checking for Balanced Braces  A stack can be used to verify whether a program contains balanced braces  An example of balanced braces abc{defg{ijk}{l{mn}}op}qr  An example of unbalanced braces abc{def}}{ghij{kl}m abc{def}{ghij{kl}m
  • 13. Checking for Balanced Braces  Requirements for balanced braces  Each time you encounter a “}”, it matches an already encountered “{”  When you reach the end of the string, you have matched each “{”
  • 14. Checking for Balanced Braces Figure 7-3 Traces of the algorithm that checks for balanced braces
  • 15. Evaluating Postfix Expressions  A postfix (reverse Polish logic) calculator  Requires you to enter postfix expressions  Example: 2 3 4 + *  When an operand is entered, the calculator  Pushes it onto a stack  When an operator is entered, the calculator  Applies it to the top two operands of the stack  Pops the operands from the stack  Pushes the result of the operation on the stack
  • 16. Evaluating Postfix Expressions Figure 7-8 The action of a postfix calculator when evaluating the expression 2 * (3 + 4)
  • 17. Evaluating Postfix Expressions  Pseudo code: int evaluate(String expression) { Stack stack=new Stack(); // creaty empty stack while (true) { String c=expression.getNextItem(); if (c==ENDOFLINE) return stack.pop(); if (c is operand) stack.push(c); else { // operation int operand2=stack.pop(); int operand1=stack.pop(); stack.push(execute(c,operand1,operand2)); } } }
  • 18. Queues  A queue is a data structure that only allows items to be inserted at the end and removed from the front  “Queue” is the British word for a line (or line-up)  Queues are FIFO (First In First Out) data structures – “fair” data structures
  • 20. What Can You Use a Queue For?  Processing inputs and outputs to screen (console)  Server requests  Instant messaging servers queue up incoming messages  Database requests  Print queues  One printer for dozens of computers  Operating systems use queues to schedule CPU jobs  Simulations
  • 21. Queue Operations  A queue should implement (at least) these operations:  enqueue – insert an item at the back of the queue  dequeue – remove an item from the front  peek – return the item at the front of the queue without removing it  Like stacks it is assumed that these operations will be implemented efficiently  That is, in constant time
  • 22. Queue: Array Implementation  First consider using an array as the underlying structure for a queue, one plan would be to  Make the back of the queue the current size of the queue (i.e., the number of elements stored)  Make the front of the queue index 0  Inserting an item can be performed in constant time  But removing an item would require shifting all elements in the queue to the left which is too slow!  Therefore we need to find another way
  • 23. An Array-Based Implementation Figure 8-8 a) A naive array-based implementation of a queue; b) rightward drift can cause the queue to appear full