Presentation is loading. Please wait.

Presentation is loading. Please wait.

“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.

Similar presentations


Presentation on theme: "“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day."— Presentation transcript:

1 “The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day

2 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

3 Introduction Stacks, Queues –specialised lists –restricted ways of adding and removing elements Stack Queue

4 Introduction (cont.) Deques (Double-Ended Queues) –More general behaviour: add and remove from either end Deque

5 Stacks Elements added and removed at one end –the top LIFO (Last In, First Out) list Add: “push” Remove: “pop” Stack

6 The Stack Interface We will use a generic interface to specify the requirements for our stacks public interface Stack { 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

7 Implementing a Stack: Array-Based Approach Very simple –One index to keep track of top item Empty stack: top stack... 0 1 2 3 4 max

8 Pushing Elements –Increment index –Store item top stack 0 G... 0 1 2 3 4 max e 1 o 2

9 Popping Elements –Return element –Decrement index top stack 2 Geo... 0 1 2 3 4 max 1 Return ’o’

10 Array Implementation Usual space problems: –Too big: waste space –Too small: run out of space

11 ArrayStack data, topIndex push, pop, top, isEmpty The ArrayStack Class Class Diagram Methods as required by the Stack interface

12 The ArrayStack Class public class ArrayStack implements Stack { 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 to IntegerVector

13 The push and pop operations Very simple in Java –use the decrement and increment operators public void push (T item) // Push the new item onto an ArrayStack { if (topIndex >= data.length-1) throw new …Exception…(); data[++topIndex] = item; } // push public T pop () // Pop item off top of stack { if (topIndex < 0) throw new …Exception…(); return data[topIndex--]; } // pop Order is important

14 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

15 Linked List Implementation Very easy –Stack is one of the simplest linked-list structures Need a pointer to the top element Empty stack: topNode

16 ListStacktopNode push, pop, top, isEmpty The ListStack Class Class Diagram public class ListStack implements Stack { private class StackNode { public T data; public StackNode next; } // class StackNode private StackNode topNode; // Top StackNode in the stack... } // class ListStack

17 Pushing a New Element Create new node Link new node to current top node Make topNode point to new node topNode newNode G

18 Pushing a New Element (cont.) Create new node Link to top element Make top point to new node topNode G newNode e

19 A Stack with Several Elements topNode George

20 The push Method public void push (T item) { StackNode node = new StackNode(); node.data = item; node.next = topNode; topNode = node; } // push

21 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

22 The top and isEmpty methods public 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

23 Applications of Stacks Many problem areas: –Compilers –Iterative versions of recursive programs –Problem-solving –etc. Example: Reversing a string

24 Reversing a String Read letters, pushing them onto a stack Pop the letters off the stack, printing them LIFO — Last In, First Out

25


Download ppt "“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day."

Similar presentations


Ads by Google