SlideShare a Scribd company logo
1 of 28
Stacks
CS 367 – Introduction to Data Structures
Stack
• A stack is a data structure that stores data
in such a way that the last piece of data
stored, is the first one retrieved
– also called last-in, first-out
• Only access to the stack is the top
element
– consider trays in a cafeteria
• to get the bottom tray out, you must first remove all
of the elements above
Stack
• Push
– the operation to place a new item at the top of
the stack
• Pop
– the operation to remove the next item from
the top of the stack
Stack
A
X
R
C
push(M)
A
X
R
C
M
item = pop()
item = M
A
X
R
C
Implementing a Stack
• At least three different ways to implement
a stack
– array
– vector
– linked list
• Which method to use depends on the
application
– what advantages and disadvantages does
each implementation have?
Implementing Stacks: Array
• Advantages
– best performance
• Disadvantage
– fixed size
• Basic implementation
– initially empty array
– field to record where the next data gets placed into
– if array is full, push() returns false
• otherwise adds it into the correct spot
– if array is empty, pop() returns null
• otherwise removes the next item in the stack
Stack Class (array based)
class StackArray {
private Object[ ] stack;
private int nextIn;
public StackArray(int size) {
stack = new Object[size];
nextIn = 0;
}
public boolean push(Object data);
public Object pop();
public void clear();
public boolean isEmpty();
public boolean isFull();
}
push() Method (array based)
public boolean push(Object data) {
if(nextIn == stack.length) { return false; } // stack is full
// add the element and then increment nextIn
stack[nextIn] = data;
nextIn++;
return true;
}
pop() Method (array based)
public Object pop() {
if(nextIn == 0) { return null; } // stack is empty
// decrement nextIn and return the data
nextIn--;
Object data = stack[nextIn];
return data;
}
Notes on push() and pop()
• Other ways to do this even if using arrays
– may want to keep a size variable that tracks
how many items in the list
– may want to keep a maxSize variable that
stores the maximum number of elements the
stack can hold (size of the array)
• you would have to do this in a language like C++
– could add things in the opposite direction
• keep track of nextOut and decrement it on every
push; increment it on every pop
Remaining Methods (array based)
public void clear() {
nextIn = 0;
}
public boolean isEmpty() {
return nextIn == 0;
}
public boolean isFull() {
return nextIn == stack.length;
}
Additional Notes
• Notice that the array is considered empty if
nextIn equals zero
– doesn’t matter if there is more data stored in
the array – it will never be retrieved
• pop() method will automatically return
• For a truly robust implementation
– should set array elements equal to null if they
are not being used
• why? how?
Implementing a Stack: Vector
• Advantages
– grows to accommodate any amount of data
– second fastest implementation when data size is less
than vector size
• Disadvantage
– slowest method if data size exceeds current vector
size
• have to copy everything over and then add data
– wasted space if anomalous growth
• vectors only grow in size – they don’t shrink
– can grow to an unlimited size
• I thought this was an advantage?
• Basic implementation
– virtually identical to array based version
Stack Class (vector based)
class StackVector {
private Object[ ] stack;
private int nextIn;
public StackVector(int initialSize) {
stack = new Object[initialSize];
nextIn = 0;
}
public void push(Object data);
public Object pop();
public void clear();
public boolean isEmpty();
}
push() Method (vector based)
public void push(Object data) {
// see if we need to grow this stack
if(nextIn == stack.length) {
Object [ ] tmp = new Object[stack.length * 2];
for(int i=0; i<stack.length; i++)
tmp[i] = stack[i];
stack = tmp;
}
// now add the element and increment nextIn
stack[nextIn] = data;
nextIn++;
}
pop() Method (vector based)
public Object pop() {
if(nextIn == 0) { return null; } // stack empty
// decrement nextIn, get the data, and return it
nextIn--;
Object data = stack[nextIn];
return data;
}
Notes on push() and pop()
• Notice that the pop() method is identical to
that for an array based version
• Only difference is in push() method
– doesn’t return a boolean because it cannot fail
• unless we run out of memory 
– first checks if the push will exceed the current
array
• if so, create a new array that’s 2x as big, copy
data, and make that the new stack
• this is the case that’s very slow
Remaining Methods (vector based)
• The clear() and isEmpty() methods are
identical to those in an array based stack
implementation
• There is no need for an isFull() method
– why?
Implementing a Stack: Linked List
• Advantages:
– always constant time to push or pop an element
– can grow to an infinite size
• Disadvantages
– the common case is the slowest of all the
implementations
– can grow to an infinite size
• Basic implementation
– list is initially empty
– push() method adds a new item to the head of the list
– pop() method removes the head of the list
Stack Class (list based)
class StackList {
private LinkedList list;
public StackList() { list = new LinkedList(); }
public void push(Object data) { list.addHead(data); }
public Object pop() { return list.deleteHead(); }
public void clear() { list.clear(); }
public boolean isEmpty() { return list.isEmpty(); }
}
Additional Notes
• It should appear obvious that linked lists
are very well suited for stacks
– addHead() and deleteHead() are basically the
push() and pop() methods
• Our original list implementation did not
have a clear() method
– it’s very simple to do
– how would you do it?
• Again, no need for the isFull() method
– list can grow to an infinite size
Stack Applications
• Stacks are a very common data structure
– compilers
• parsing data between delimiters (brackets)
– operating systems
• program stack
– virtual machines
• manipulating numbers
– pop 2 numbers off stack, do work (such as add)
– push result back on stack and repeat
– artificial intelligence
• finding a path
Reverse Polish Notation
• Way of inputting numbers to a calculator
– (5 + 3) * 6 becomes 5 3 + 6 *
– 5 + 3 * 6 becomes 5 3 6 * +
• We can use a stack to implement this
– consider 5 3 + 6 *
5
3
8
+
8
6
*6
48
– try doing 5 3 6 * +
public int rpn(String equation) {
StackList stack = new StackList();
StringTokenizer tok = new StringTokenizer(equation);
while(tok.hasMoreTokens()) {
String element = tok.nextToken();
if(isOperator(element)) {
char op = element.charAt(0);
if(op == ‘=‘) {
int result = ((Integer)stack.pop()).intValue();
if(!stack.isEmpty() || tok.hasMoreTokens()) { return Integer.MAX_VALUE; } // error
else { return result; }
}
else {
Integer op1 = (Integer)stack.pop()
Integer op2 = (Integer)stack.pop();
if((op1 == null) || (op2 == null)) { return Integer.MAX_VALUE; }
stack.push(doOperation(op, op1, op2));
}
}
else {
Integer operand = new Integer(Integer.parseInt(element));
stack.push(operand);
}
}
return Integer.MAX_VALUE;
}
Finding a Path
• Consider the following graph of flights
PR
X Q
W
Y
Z
S
T
Key
: city (represented as C)
: flight from city C1 to city C2
C1 C2
flight goes from W to S
W S
Example
Finding a Path
• If it exists, we can find a path from any city C1 to
another city C2 using a stack
– place the starting city on the bottom of the stack
• mark it as visited
• pick any arbitrary arrow out of the city
– city cannot be marked as visited
• place that city on the stack
– also mark it as visited
• if that’s the destination, we’re done
• otherwise, pick an arrow out of the city currently at
– next city must not have been visited before
– if there are no legitimate arrows out, pop it off the stack and go
back to the previous city
• repeat this process until the destination is found or all the
cities have been visited
Example
• Want to go from P to Y
– push P on the stack and mark it as visited
– pick R as the next city to visit (random select)
• push it on the stack and mark it as visited
– pick X as the next city to visit (only choice)
• push it on the stack and mark it as visited
– no available arrows out of X – pop it
– no more available arrows from R – pop it
– pick W as next city to visit (only choice left)
• push it on the stack and mark it as visited
– pick Y as next city to visit (random select)
• this is the destination – all done
Psuedo-Code for the Example
public boolean findPath(City origin, City destination) {
StackArray stack = new Stack(numCities);
clearAllCityMarks();
stack.push(origin);
origin.mark();
while(!stack.isEmpty()) {
City next = pickCity();
if(next == destination) { return true; }
if(next != null) { stack.push(next); }
else { stack.pop(); } // no valid arrows out of city
}
return false;
}

More Related Content

What's hot (18)

Introduction to matplotlib
Introduction to matplotlibIntroduction to matplotlib
Introduction to matplotlib
 
stack presentation
stack presentationstack presentation
stack presentation
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Stack_Data_Structure.pptx
Stack_Data_Structure.pptxStack_Data_Structure.pptx
Stack_Data_Structure.pptx
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
 
Rana Junaid Rasheed
Rana Junaid RasheedRana Junaid Rasheed
Rana Junaid Rasheed
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
 
Stack
StackStack
Stack
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
Search algorithms master
Search algorithms masterSearch algorithms master
Search algorithms master
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 

Similar to CS 367 - Introduction to Data Structures: Stack Implementation and Applications

Data Structure - Stack.pptx
Data Structure - Stack.pptxData Structure - Stack.pptx
Data Structure - Stack.pptxMarlonMagtibay2
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using Ccpjcollege
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueBalwant Gorad
 
Ds
DsDs
DsAcad
 
Stacks-and-Queues.pdf
Stacks-and-Queues.pdfStacks-and-Queues.pdf
Stacks-and-Queues.pdfTobyWtf
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبياناتRafal Edward
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTSoumen Santra
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxchandankumar364348
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for publiciqbalphy1
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queueRajkiran Nadar
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptxHalid Assen
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxYogesh Pawar
 

Similar to CS 367 - Introduction to Data Structures: Stack Implementation and Applications (20)

Data Structure - Stack.pptx
Data Structure - Stack.pptxData Structure - Stack.pptx
Data Structure - Stack.pptx
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using C
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Stacks
StacksStacks
Stacks
 
Ds
DsDs
Ds
 
Stacks-and-Queues.pdf
Stacks-and-Queues.pdfStacks-and-Queues.pdf
Stacks-and-Queues.pdf
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبيانات
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
Chapter 4 stack
Chapter 4 stackChapter 4 stack
Chapter 4 stack
 
Lecture9_StackQueue.ppt
Lecture9_StackQueue.pptLecture9_StackQueue.ppt
Lecture9_StackQueue.ppt
 
StackQueue.ppt
StackQueue.pptStackQueue.ppt
StackQueue.ppt
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptx
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 

Recently uploaded

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 

Recently uploaded (20)

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 

CS 367 - Introduction to Data Structures: Stack Implementation and Applications

  • 1. Stacks CS 367 – Introduction to Data Structures
  • 2. Stack • A stack is a data structure that stores data in such a way that the last piece of data stored, is the first one retrieved – also called last-in, first-out • Only access to the stack is the top element – consider trays in a cafeteria • to get the bottom tray out, you must first remove all of the elements above
  • 3. Stack • Push – the operation to place a new item at the top of the stack • Pop – the operation to remove the next item from the top of the stack
  • 5. Implementing a Stack • At least three different ways to implement a stack – array – vector – linked list • Which method to use depends on the application – what advantages and disadvantages does each implementation have?
  • 6. Implementing Stacks: Array • Advantages – best performance • Disadvantage – fixed size • Basic implementation – initially empty array – field to record where the next data gets placed into – if array is full, push() returns false • otherwise adds it into the correct spot – if array is empty, pop() returns null • otherwise removes the next item in the stack
  • 7. Stack Class (array based) class StackArray { private Object[ ] stack; private int nextIn; public StackArray(int size) { stack = new Object[size]; nextIn = 0; } public boolean push(Object data); public Object pop(); public void clear(); public boolean isEmpty(); public boolean isFull(); }
  • 8. push() Method (array based) public boolean push(Object data) { if(nextIn == stack.length) { return false; } // stack is full // add the element and then increment nextIn stack[nextIn] = data; nextIn++; return true; }
  • 9. pop() Method (array based) public Object pop() { if(nextIn == 0) { return null; } // stack is empty // decrement nextIn and return the data nextIn--; Object data = stack[nextIn]; return data; }
  • 10. Notes on push() and pop() • Other ways to do this even if using arrays – may want to keep a size variable that tracks how many items in the list – may want to keep a maxSize variable that stores the maximum number of elements the stack can hold (size of the array) • you would have to do this in a language like C++ – could add things in the opposite direction • keep track of nextOut and decrement it on every push; increment it on every pop
  • 11. Remaining Methods (array based) public void clear() { nextIn = 0; } public boolean isEmpty() { return nextIn == 0; } public boolean isFull() { return nextIn == stack.length; }
  • 12. Additional Notes • Notice that the array is considered empty if nextIn equals zero – doesn’t matter if there is more data stored in the array – it will never be retrieved • pop() method will automatically return • For a truly robust implementation – should set array elements equal to null if they are not being used • why? how?
  • 13. Implementing a Stack: Vector • Advantages – grows to accommodate any amount of data – second fastest implementation when data size is less than vector size • Disadvantage – slowest method if data size exceeds current vector size • have to copy everything over and then add data – wasted space if anomalous growth • vectors only grow in size – they don’t shrink – can grow to an unlimited size • I thought this was an advantage? • Basic implementation – virtually identical to array based version
  • 14. Stack Class (vector based) class StackVector { private Object[ ] stack; private int nextIn; public StackVector(int initialSize) { stack = new Object[initialSize]; nextIn = 0; } public void push(Object data); public Object pop(); public void clear(); public boolean isEmpty(); }
  • 15. push() Method (vector based) public void push(Object data) { // see if we need to grow this stack if(nextIn == stack.length) { Object [ ] tmp = new Object[stack.length * 2]; for(int i=0; i<stack.length; i++) tmp[i] = stack[i]; stack = tmp; } // now add the element and increment nextIn stack[nextIn] = data; nextIn++; }
  • 16. pop() Method (vector based) public Object pop() { if(nextIn == 0) { return null; } // stack empty // decrement nextIn, get the data, and return it nextIn--; Object data = stack[nextIn]; return data; }
  • 17. Notes on push() and pop() • Notice that the pop() method is identical to that for an array based version • Only difference is in push() method – doesn’t return a boolean because it cannot fail • unless we run out of memory  – first checks if the push will exceed the current array • if so, create a new array that’s 2x as big, copy data, and make that the new stack • this is the case that’s very slow
  • 18. Remaining Methods (vector based) • The clear() and isEmpty() methods are identical to those in an array based stack implementation • There is no need for an isFull() method – why?
  • 19. Implementing a Stack: Linked List • Advantages: – always constant time to push or pop an element – can grow to an infinite size • Disadvantages – the common case is the slowest of all the implementations – can grow to an infinite size • Basic implementation – list is initially empty – push() method adds a new item to the head of the list – pop() method removes the head of the list
  • 20. Stack Class (list based) class StackList { private LinkedList list; public StackList() { list = new LinkedList(); } public void push(Object data) { list.addHead(data); } public Object pop() { return list.deleteHead(); } public void clear() { list.clear(); } public boolean isEmpty() { return list.isEmpty(); } }
  • 21. Additional Notes • It should appear obvious that linked lists are very well suited for stacks – addHead() and deleteHead() are basically the push() and pop() methods • Our original list implementation did not have a clear() method – it’s very simple to do – how would you do it? • Again, no need for the isFull() method – list can grow to an infinite size
  • 22. Stack Applications • Stacks are a very common data structure – compilers • parsing data between delimiters (brackets) – operating systems • program stack – virtual machines • manipulating numbers – pop 2 numbers off stack, do work (such as add) – push result back on stack and repeat – artificial intelligence • finding a path
  • 23. Reverse Polish Notation • Way of inputting numbers to a calculator – (5 + 3) * 6 becomes 5 3 + 6 * – 5 + 3 * 6 becomes 5 3 6 * + • We can use a stack to implement this – consider 5 3 + 6 * 5 3 8 + 8 6 *6 48 – try doing 5 3 6 * +
  • 24. public int rpn(String equation) { StackList stack = new StackList(); StringTokenizer tok = new StringTokenizer(equation); while(tok.hasMoreTokens()) { String element = tok.nextToken(); if(isOperator(element)) { char op = element.charAt(0); if(op == ‘=‘) { int result = ((Integer)stack.pop()).intValue(); if(!stack.isEmpty() || tok.hasMoreTokens()) { return Integer.MAX_VALUE; } // error else { return result; } } else { Integer op1 = (Integer)stack.pop() Integer op2 = (Integer)stack.pop(); if((op1 == null) || (op2 == null)) { return Integer.MAX_VALUE; } stack.push(doOperation(op, op1, op2)); } } else { Integer operand = new Integer(Integer.parseInt(element)); stack.push(operand); } } return Integer.MAX_VALUE; }
  • 25. Finding a Path • Consider the following graph of flights PR X Q W Y Z S T Key : city (represented as C) : flight from city C1 to city C2 C1 C2 flight goes from W to S W S Example
  • 26. Finding a Path • If it exists, we can find a path from any city C1 to another city C2 using a stack – place the starting city on the bottom of the stack • mark it as visited • pick any arbitrary arrow out of the city – city cannot be marked as visited • place that city on the stack – also mark it as visited • if that’s the destination, we’re done • otherwise, pick an arrow out of the city currently at – next city must not have been visited before – if there are no legitimate arrows out, pop it off the stack and go back to the previous city • repeat this process until the destination is found or all the cities have been visited
  • 27. Example • Want to go from P to Y – push P on the stack and mark it as visited – pick R as the next city to visit (random select) • push it on the stack and mark it as visited – pick X as the next city to visit (only choice) • push it on the stack and mark it as visited – no available arrows out of X – pop it – no more available arrows from R – pop it – pick W as next city to visit (only choice left) • push it on the stack and mark it as visited – pick Y as next city to visit (random select) • this is the destination – all done
  • 28. Psuedo-Code for the Example public boolean findPath(City origin, City destination) { StackArray stack = new Stack(numCities); clearAllCityMarks(); stack.push(origin); origin.mark(); while(!stack.isEmpty()) { City next = pickCity(); if(next == destination) { return true; } if(next != null) { stack.push(next); } else { stack.pop(); } // no valid arrows out of city } return false; }