the desire for safety stands against every great and noble enterprise. tacitus thought for the day

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

Upload: aileen-poole

Post on 19-Jan-2018

218 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: The desire for safety stands against every great and noble enterprise.  Tacitus Thought for the Day

“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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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