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
“The desire for safety stands against every great and noble enterprise.”– Tacitus
Thought for the Day
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
Introduction
• Stacks, Queues– specialised lists– restricted ways of adding and removing
elements
Stack Queue
Introduction (cont.)
• Deques (Double-Ended Queues)– More general behaviour: add and remove from
either end
Deque
Stacks
• Elements added and removed at one end– the top
• LIFO (Last In, First Out) list
• Add: “push”• Remove: “pop” Stack
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
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
Pushing Elements
– Increment index– Store item
top
stack
0
G ...
0 1 2 3 4 max
e
1
o
2
Popping Elements
– Return element– Decrement index
top
stack
2
G e o ...
0 1 2 3 4 max
1Return ’o’
Array Implementation
• Usual space problems:– Too big: waste space– Too small: run out of space
ArrayStack
data, topIndexdata, topIndexpush, pop, top, isEmpty
The ArrayStack Class
• Class Diagram
Methods as required by the Stack interface
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
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
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
Linked List Implementation
• Very easy– Stack is one of the simplest linked-list
structures• Need a pointer to the top element• Empty stack:
topNode
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
Pushing a New Element• Create new node• Link new node to current top node• Make topNode point to new node
topNode
newNode G
Pushing a New Element (cont.)• Create new node• Link to top element• Make top point to new node
topNode G
newNode e
A Stack with Several Elements
topNode George
The push Method
public void push (T item) { StackNode node = new StackNode(); node.data = item; node.next = topNode; topNode = node; } // push
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
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
Applications of Stacks
• Many problem areas:– Compilers– Iterative versions of recursive programs– Problem-solving– etc.
• Example: Reversing a string
Reversing a String
• Read letters, pushing them onto a stack• Pop the letters off the stack, printing them
LIFO — Last In, First Out