the desire for safety stands against every great and noble enterprise. tacitus thought for the day
DESCRIPTION
Introduction Stacks, Queues –specialised lists –restricted ways of adding and removing elements Stack QueueTRANSCRIPT
![Page 1: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/1.jpg)
“The desire for safety stands against every great and noble enterprise.”– Tacitus
Thought for the Day
![Page 2: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/2.jpg)
Chapter 5: Stacks and Queues
• Objectives– To consider stack, queue and deque ADTs– To look at their implementation
• using arrays and linked lists– To look at some of their applications– To study new implementation techniques for
linked lists: doubly-linked lists, circularly-linked lists and list header nodes
![Page 3: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/3.jpg)
Introduction
• Stacks, Queues– specialised lists– restricted ways of adding and removing
elements
Stack Queue
![Page 4: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/4.jpg)
Introduction (cont.)
• Deques (Double-Ended Queues)– More general behaviour: add and remove from
either end
Deque
![Page 5: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/5.jpg)
Stacks
• Elements added and removed at one end– the top
• LIFO (Last In, First Out) list
• Add: “push”• Remove: “pop” Stack
![Page 6: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/6.jpg)
The Stack Interface• We will use a generic interface to specify
the requirements for our stacks
public interface Stack<T> { public void push (T item); // Push the new item onto a stack public T pop (); // Pop item off top of stack public T top (); // Return a copy of top item public boolean isEmpty (); // Return TRUE if no items on stack } // interface Stack
![Page 7: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/7.jpg)
Implementing a Stack:Array-Based Approach
• Very simple– One index to keep track of top item
• Empty stack:
top
stack
-1
...
0 1 2 3 4 max
![Page 8: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/8.jpg)
Pushing Elements
– Increment index– Store item
top
stack
0
G ...
0 1 2 3 4 max
e
1
o
2
![Page 9: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/9.jpg)
Popping Elements
– Return element– Decrement index
top
stack
2
G e o ...
0 1 2 3 4 max
1Return ’o’
![Page 10: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/10.jpg)
Array Implementation
• Usual space problems:– Too big: waste space– Too small: run out of space
![Page 11: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/11.jpg)
ArrayStack
data, topIndexdata, topIndexpush, pop, top, isEmpty
The ArrayStack Class
• Class Diagram
Methods as required by the Stack interface
![Page 12: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/12.jpg)
The ArrayStack Classpublic class ArrayStack<T> implements Stack<T> { private T[] data; // Array of data private int topIndex; // Top element
public ArrayStack (int sz) { data = (T[])new Object[sz]; topIndex = -1; } // Constructor public ArrayStack () { this(100); } // Constructor ... } // class ArrayStack
Very similar toIntegerVector
![Page 13: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/13.jpg)
The push and pop operations• Very simple in Java
– use the decrement and increment operatorspublic void push (T item)// Push the new item onto an ArrayStack { if (topIndex >= data.length-1) throw new …Exception…(); data[++topIndex] = item; } // pushpublic T pop ()// Pop item off top of stack { if (topIndex < 0) throw new …Exception…(); return data[topIndex--]; } // pop
Order is important
![Page 14: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/14.jpg)
top and isEmpty
public T top ()// Return a copy of top item { if (topIndex < 0) throw new …Exception…(); return data[topIndex]; } // top
public boolean isEmpty ()// Return TRUE if no items on stack { return topIndex < 0; } // isEmpty
![Page 15: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/15.jpg)
Linked List Implementation
• Very easy– Stack is one of the simplest linked-list
structures• Need a pointer to the top element• Empty stack:
topNode
![Page 16: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/16.jpg)
ListStack
topNodetopNodepush, pop, top, isEmpty
The ListStack Class• Class Diagram
public class ListStack<T> implements Stack<T> { private class StackNode { public T data; public StackNode next; } // class StackNode private StackNode topNode; // Top StackNode in the stack . . . } // class ListStack
![Page 17: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/17.jpg)
Pushing a New Element• Create new node• Link new node to current top node• Make topNode point to new node
topNode
newNode G
![Page 18: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/18.jpg)
Pushing a New Element (cont.)• Create new node• Link to top element• Make top point to new node
topNode G
newNode e
![Page 19: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/19.jpg)
A Stack with Several Elements
topNode George
![Page 20: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/20.jpg)
The push Method
public void push (T item) { StackNode node = new StackNode(); node.data = item; node.next = topNode; topNode = node; } // push
![Page 21: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/21.jpg)
Popping an Element
• Also very simple– Copy data in top node– Reset top pointer to point to next element
public T pop () { if (topNode == null) // Stack is empty throw new …Exception…(); T tmpData = topNode.data; topNode = topNode.next; return tmpData; } // pop
![Page 22: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/22.jpg)
The top and isEmpty methodspublic T top ()// Return copy of top item { if (topNode == null) throw new …Exception…(); return topNode.data; } // top
public boolean isEmpty ()// Return TRUE if no items on stack { return topNode == null; } // isEmpty
![Page 23: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/23.jpg)
Applications of Stacks
• Many problem areas:– Compilers– Iterative versions of recursive programs– Problem-solving– etc.
• Example: Reversing a string
![Page 24: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/24.jpg)
Reversing a String
• Read letters, pushing them onto a stack• Pop the letters off the stack, printing them
LIFO — Last In, First Out
![Page 25: The desire for safety stands against every great and noble enterprise. Tacitus Thought for the Day](https://reader035.vdocuments.us/reader035/viewer/2022070611/5a4d1bb77f8b9ab0599cf0ce/html5/thumbnails/25.jpg)