## Stacks, Queues and Linked Lists

Stacks, Queues and Linked Lists • Stacks • Queues • Linked Lists – Singly Linked Lists – Doubly Linked Lists • Double-Ended Queues • Inspectable Cont...
Author: Holly Kelly

• Double-Ended Queues • Inspectable Containers

School of Computer Science, Carleton University COMP 2002/2402 Introduction to Data Structures and Data Types

Version 03.s 4-1

Albert Chan http://www.scs.carleton.ca/~achan

Stack • A stack is a data structure which has the following properties: – It can hold objects which are pushed onto it. When an object is pushed onto a stack, it is put on the top of the stack. – If another object is pushed onto the stack, the newly pushed object will now be on the top of the stack, and the old top object will be “pushed beneath” it. It will be on the top again if all the objects that are pushed after have been removed. – The object on the top of the stack can be popped out from the stack. That is, the stack has a last-in-first-out (LIFO) structure. – No object in the stack other than the one on the top can be accessed from outside. School of Computer Science, Carleton University COMP 2002/2402 Introduction to Data Structures and Data Types

Version 03.s 4-2

Albert Chan http://www.scs.carleton.ca/~achan

Stack ADT • push (o): Insert object o at the top of the stack. – Input: Object – Output: None

• pop (): Remove from the stack and return the top object on the stack; an error occurs if the stack is empty. – Input: None – Output: Object

• top (): Return the top object on the stack, without removing it; an error occurs if the stack is empty. – Input: None – Output: Object School of Computer Science, Carleton University COMP 2002/2402 Introduction to Data Structures and Data Types

Version 03.s 4-3

Albert Chan http://www.scs.carleton.ca/~achan

Stack ADT • isEmpty (): Return a boolean indicating if the stack is empty. – Input: None – Output: Boolean

• size (): Return the number of objects in the stack. – Input: None – Output: Integer

School of Computer Science, Carleton University COMP 2002/2402 Introduction to Data Structures and Data Types

Version 03.s 4-4

Albert Chan http://www.scs.carleton.ca/~achan

De-coupling of the ADT • The 5 methods we saw in the previous slides can be divided into 2 groups: those belong only to Stack (push, pop, top) and those do not (isEmpty, size). • isEmpty and size are methods that apply to all data containers. • So we can divide the ADT into hierarchy: DataContainer DataContainer Stack Stack School of Computer Science, Carleton University COMP 2002/2402 Introduction to Data Structures and Data Types

Version 03.s 4-5

Albert Chan http://www.scs.carleton.ca/~achan

Stack Interface public interface DataContainer { // return the # of elements in stack public int size (); // test if stack is empty public boolean isEmpty (); } public interface Stack extends DataContainer { // insert onto stack public void push (Object element); // return and remove the top element public Object pop () throws StackEmptyException; // return the top element public Object top () throws StackEmptyException; } School of Computer Science, Carleton University COMP 2002/2402 Introduction to Data Structures and Data Types

Version 03.s 4-6

Albert Chan http://www.scs.carleton.ca/~achan

Stack Example Operation push (5) push (3) pop () push (7) pop () top () pop () pop () isEmpty () push (9) push (7) push (3) push (5) size () pop ()

School of Computer Science, Carleton University COMP 2002/2402 Introduction to Data Structures and Data Types

Version 03.s 4-7

Output 3 7 5 5 Error true 4 5

S (5) (5,3) (5) (5,7) (5) (5) () () () (9) (9,7) (9,7,3) (9,7,3,5) (9,7,3,5) (9,7,3)

Albert Chan http://www.scs.carleton.ca/~achan

Array-Based Implementation

TOP

...

S 0

1

2

School of Computer Science, Carleton University COMP 2002/2402 Introduction to Data Structures and Data Types

t

Version 03.s 4-8

N-1

Albert Chan http://www.scs.carleton.ca/~achan

Analysis • Advantage: – Simple – Efficient

• Problem: – Size Limitation • Stack overflow • Array size may not be utilized

School of Computer Science, Carleton University COMP 2002/2402 Introduction to Data Structures and Data Types

Method size isEmpty top push pop

Version 03.s 4-9

Time O (1) O (1) O (1) O (1) O (1)

Albert Chan http://www.scs.carleton.ca/~achan

Casting of Return Types • The returned object are always instances of the class “Object”. • For that object to be useful, type casting has to be performed. • Example: To reverse an integer array a[] public static Integer[] reverse (Integer[] a) { ArrayStack S = new ArrayStack (a.length); Integer[] b = new Integer [a.length]; for (int i=0; i