Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure...

88
Stacks and Linked Lists

Transcript of Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure...

Page 1: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Stacks and Linked Lists

Page 2: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Abstract Data Types (ADTs)

• An ADT is an abstraction of a data structure that specifies– Data stored– Operations on the data– Error conditions associated with operations

Page 3: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Abstract Data Types (ADTs)

• An ADT is an abstraction of a data structure that specifies– Data stored– Operations on the data– Error conditions associated with operations

• Example: Registering for classes– The data stored are the courses in your schedule– The operations supported are

• Register(course)• Unregister(course)• ForceRequest(course)

– Error conditions:• Registering for multiple classes meeting at the same time

Page 4: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Stacks

Page 5: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Stacks• Stacks store arbitrary objects

(Pez in this case)

Page 6: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Stacks• Stacks store arbitrary objects

(Pez in this case)• Operations

– push(e): inserts an element to the top of the stack

Page 7: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Stacks• Stacks store arbitrary objects

(Pez in this case)• Operations

– push(e): inserts an element to the top of the stack

Page 8: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Stacks• Stacks store arbitrary objects

(Pez in this case)• Operations

– push(e): inserts an element to the top of the stack

– pop(): removes and returns the top element of the stack

Page 9: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Stacks• Stacks store arbitrary objects

(Pez in this case)• Operations

– push(e): inserts an element to the top of the stack

– pop(): removes and returns the top element of the stack

Page 10: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Stacks• Stacks store arbitrary objects

(Pez in this case)• Operations

– push(e): inserts an element to the top of the stack

– pop(): removes and returns the top element of the stack

Page 11: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Stacks• Stacks store arbitrary objects

(Pez in this case)• Operations

– push(e): inserts an element to the top of the stack

– pop(): removes and returns the top element of the stack

– top(): returns a reference to the top element of the stack, but doesn’t remove it

Page 12: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Stacks• Stacks store arbitrary objects

(Pez in this case)• Operations

– push(e): inserts an element to the top of the stack

– pop(): removes and returns the top element of the stack

– top(): returns a reference to the top element of the stack, but doesn’t remove it

• Optional operations– size(): returns the number of

elements in the stack– empty(): returns a bool indicating if

the stack contains any objects

Page 13: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Stack Exceptions• Attempting to execute an operation of ADT may

cause an error condition called an exception• Exceptions are said to be “thrown” by an

operation that cannot be executed• In the Stack ADT, pop and top cannot be

performed if the stack is empty• Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Page 14: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Exercise: Stacks• Describe the output and final structure of the stack after

the following operations:– Push(8)– Push(3)– Pop()– Push(2)– Push(5)– Pop()– Pop()– Push(9)– Push(1)

Page 15: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Applications of Stacks

• Direct applications– Page-visited history in a Web browser– Undo sequence in a text editor– Saving local variables when one function calls another, and

this one calls another, and so on.

• Indirect applications– Auxiliary data structure for algorithms– Component of other data structures

Page 16: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

C++ Run-time Stack• The C++ run-time system keeps

track of the chain of active functions with a stack

• When a function is called, the run-time system pushes on the stack a frame containing– Local variables and return value– Program counter, keeping track

of the statement being executed • When a function returns, its

frame is popped from the stack and control is passed to the method on top of the stack

main() {int i;

i = 5;foo(i);

}

foo(int j) {int k;k = j+1;bar(k);

}

bar(int m) {…

}

bar PC = 1 m = 6

foo PC = 3 j = 5 k = 6

main PC = 2 i = 5

Page 17: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Array-based Stack

• A simple way of implementing the Stack ADT uses an array

• We add elements from left to right

• A variable keeps track of the index of the top element

S

0 1 2 t

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() thenthrow EmptyStackException

else t t 1return S[t + 1]

Page 18: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Array-based Stack (cont.)

• The array storing the stack elements may become full

• A push operation will then throw a FullStackException– Limitation of the

array-based implementation

– Not intrinsic to the Stack ADT

S

0 1 2 t

Algorithm push(e)if t = S.length 1 thenthrow FullStackException

else t t + 1S[t] e

Page 19: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Performance and Limitations (array-based implementation of stack ADT)

• Performance– Let n be the number of elements in the stack– The space used is O(n)

– Each operation runs in time O(1)

• Limitations– The maximum size of the stack must be defined a

priori , and cannot be changed– Trying to push a new element into a full stack causes

an implementation-specific exception

Page 20: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Growable Array-based Stack

• In a push operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one

• How large should the new array be?– incremental strategy:

increase the size by a constant c

– doubling strategy: double the size

Algorithm push(o)if t = S.length 1

thenA new array of

size …for i 0 to t do A[i] S[i]

S At t + 1S[t] o

Page 21: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Comparison

• We compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n push operations

• Assume that we start with an empty stack represented by an array of size 1

• We call amortized time of a push operation the average time taken by a push over the series of operations, i.e., T(n)/n

Page 22: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Incremental Strategy Analysis

• We replace the array k = n/c times• The total time T(n) of a series of n push

operations is proportional to• n + c + 2c + 3c + 4c + … + kc =

• n + c(1 + 2 + 3 + … + k) =• n + ck(k + 1)/2

• Since c is a constant, T(n) is O(n + k2) = O(n2)• The amortized time of a push operation is O(n)

Page 23: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Doubling Strategy Analysis

• We replace the array k = log2 n times• The total time T(n) of a series of n push

operations is proportional to• n + 1 + 2 + 4 + 8 + …+ 2k =

• n 2k + 1 1 = 3n 1• T(n) is O(n)• The amortized time of a push operation is O(1)

Page 24: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Stack Interface in C++

• Requires the definition of class EmptyStackException

• Most similar STL construct is vector

template <class Type>class Stack {public: int size(); bool isEmpty(); Type& top()

throw(EmptyStackException); void push(Type e); Type pop()

throw(EmptyStackException);};

Page 25: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

template <class Type>class ArrayStack{private: int capacity; // stack capacity Type *S; // stack array int t; // top of stack

public: ArrayStack(int c) : capacity(c) { S = new Type [ capacity ]; t = -1; }

bool isEmpty() { return t < 0; }

Type pop() throw(EmptyStackException) { if ( isEmpty ( ) ) throw EmptyStackException(“Popping from empty stack”); return S [ t-- ]; }//… (other functions omitted)

Array-based Stack in C++

Page 26: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Singly Linked List

• A singly linked list is a structure consisting of a sequence of nodes

• A singly linked list stores a pointer to the first node (head) and last (tail)

• Each node stores– element– link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Page 27: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Singly Linked List Node in C++

next

elem node

template <class Type>class SLinkedListNode {public: Type elem;

SLinkedListNode<Type> *next;

};

Leonard Sheldon Howard Raj

Page 28: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Singly Linked List

• A singly linked list is a structure consisting of a sequence of nodes

• Operations– insertFront(e): inserts an element on the front of

the list– removeFront(): returns and removes the element

at the front of the list– insertBack(e): inserts an element on the back of

the list– removeBack(): returns and removes the element

at the end of the list

Page 29: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Inserting at the Front

1. Allocate a new node2. Have new node point to old head3. Update head to point to new node

Leonard Sheldon Howard Raj

head tail

Page 30: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Inserting at the Front

1. Allocate a new node2. Have new node point to old head3. Update head to point to new node

Leonard Sheldon Howard Raj

head

Penny

tail

Page 31: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Inserting at the Front

1. Allocate a new node2. Have new node point to old head3. Update head to point to new node

Leonard Sheldon Howard Raj

head

Penny

tail

Page 32: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Inserting at the Front

1. Allocate a new node2. Have new node point to old head3. Update head to point to new node

Leonard Sheldon Howard Raj

head

Penny

tail

Page 33: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Inserting at the Front

1. Allocate a new node2. Have new node point to old head3. Update head to point to new node

head tail

Page 34: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Inserting at the Front

1. Allocate a new node2. Have new node point to old head3. Update head to point to new node

Raj

head tail

Page 35: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Inserting at the Front

1. Allocate a new node2. Have new node point to old head3. Update head to point to new node4. If tail is NULL, update tail to point to the head

node

Raj

head tail

Page 36: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing at the Front

1. Update head to point to next node in the list2. Return elem of previous head and delete the

node

Leonard Sheldon Howard Raj

head tail

Page 37: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing at the Front

1. Update head to point to next node in the list2. Return elem of previous head and delete the

node

Leonard Sheldon Howard Raj

head tail

Page 38: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing at the Front

1. Update head to point to next node in the list2. Return elem of previous head and delete the

node

Leonard Sheldon Howard Raj

head tail

Page 39: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing at the Front

1. Update head to point to next node in the list2. Return elem of previous head and delete the

node

Leonard Sheldon Howard Raj

head tail

Page 40: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing at the Front

1. Update head to point to next node in the list2. Return elem of previous head and delete the

node

Sheldon Howard Raj

head tail

Page 41: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing at the Front

1. Update head to point to next node in the list2. Return elem of previous head and delete the

node

Sheldon

head tail

Page 42: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing at the Front

1. Update head to point to next node in the list2. Return elem of previous head and delete the

node

Sheldon

head tail

Page 43: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing at the Front

1. Update head to point to next node in the list2. Return elem of previous head and delete the

node

Sheldon

head tail

Page 44: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing at the Front

1. Update head to point to next node in the list2. Return elem of previous head and delete the

node

Sheldon

head tail

Page 45: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing at the Front

1. Update head to point to next node in the list2. Return elem of previous head and delete the

node3. If head is NULL, update tail to NULL

head tail

Page 46: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Inserting at the Back

1. Allocate a new node2. If tail is NULL, update head and tail to point to

the new node; otherwise1. Have the old tail point to the new node2. Update tail to point to new node

Leonard Sheldon Howard

head tail

Page 47: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Inserting at the Back

1. Allocate a new node2. If tail is NULL, update head and tail to point to

the new node; otherwise1. Have the old tail point to the new node2. Update tail to point to new node

Leonard Sheldon Howard

head tail

Raj

Page 48: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Inserting at the Back

1. Allocate a new node2. If tail is NULL, update head and tail to point to

the new node; otherwise1. Have the old tail point to the new node2. Update tail to point to new node

Leonard Sheldon Howard

head tail

Raj

Page 49: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Inserting at the Back

1. Allocate a new node2. If tail is NULL, update head and tail to point to

the new node; otherwise1. Have the old tail point to the new node2. Update tail to point to new node

Leonard Sheldon Howard

head tail

Raj

Page 50: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing at the Back

• No efficient way of doing so (O(n))

• Typically would not use a singly linked-list if this operation is commonly used

Leonard Sheldon Howard Raj

head tail

Page 51: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Stack with a Singly Linked List

• We can implement a stack with a singly linked list• The top element of the stack is the first node of the list• The space used is O(n) and each operation of the Stack

ADT takes O(1) time

t

nodes

elements

top

Page 52: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Stack Summary

• Stack Operation Complexity for Different ImplementationsArray

Fixed-SizeArrayExpandable (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size(), isEmpty() O(1) O(1) O(1)

Page 53: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Queues

Page 54: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Queues• Queues store arbitrary objects• Insertions are at the end of the

queue and removals are at the front of the queue

• Main queue operations:– enqueue(e): inserts an element

at the end of the queue– dequeue(): removes and returns

the element at the front of the queue

• Auxiliary queue operations:– front(): returns the element at

the front without removing it– size(): returns the number of

elements stored– isEmpty(): returns a boolean

value indicating if there are no elements in the queue

• Exceptions– Attempting to execute dequeue

or front on an empty queue throws an EmptyQueueException

Page 55: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Exercise: Queues• Describe the output and final structure of the queue

after the following operations:– enqueue(8)– enqueue(3)– dequeue()– enqueue(2)– enqueue(5)– dequeue()– dequeue()– enqueue(9)– enqueue(1)

Page 56: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Applications of Queues

• Direct applications– Waiting lines– Access to shared resources (e.g., printer)– User input in a game

• Indirect applications– Auxiliary data structure for algorithms– Component of other data structures

Page 57: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Array-based Queue• Use an array of size N in a circular fashion• Two variables keep track of the front and rear

– f index of the front element– r index immediately past the rear element

• Array location r is kept empty

Q

0 1 2 rf

normal configuration

Q

0 1 2 fr

wrapped-around configuration

Page 58: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Queue Operations

• We use the modulo operator (remainder of division)

Algorithm size()return (N - f + r) mod N

Algorithm isEmpty()return (f = r)

Q

0 1 2 rf

Q

0 1 2 fr

Page 59: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Queue Operations (cont.)Algorithm enqueue(o)if size() = N 1 thenthrow FullQueueException

else Q[r] or (r + 1) mod N

• Operation enqueue throws an exception if the array is full

• This exception is implementation-dependent

Q

0 1 2 rf

Q

0 1 2 fr

Page 60: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Queue Operations (cont.)

• Operation dequeue throws an exception if the queue is empty

• This exception is specified in the queue ADT

Algorithm dequeue()if isEmpty() thenthrow EmptyQueueException

elseo Q[f]f (f + 1) mod Nreturn o

Q

0 1 2 rf

Q

0 1 2 fr

Page 61: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Performance and Limitations - array-based implementation of queue ADT

• Performance– Let n be the number of elements in the queue– The space used is O(n)

– Each operation runs in time O(1)

• Limitations– The maximum size of the queue must be defined a

priori , and cannot be changed– Trying to enqueue a new element into a full queue

causes an implementation-specific exception

Page 62: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Growable Array-based Queue

• In an enqueue operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one

• Similar to what we did for an array-based stack• The enqueue operation has amortized running

time – O(n) with the incremental strategy – O(1) with the doubling strategy

Page 63: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Exercise

• Describe how to implement a queue using a singly-linked list– Queue operations: enqueue(x), dequeue(),

size(), isEmpty()– For each operation, give the running time

Page 64: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Queue with a Singly Linked List• We can implement a queue with a singly linked list

– The front element is stored at the head of the list– The rear element is stored at the tail of the list

• The space used is O(n) and each operation of the Queue ADT takes O(1) time

• NOTE: we do not have the limitation of the array based implementation on the size of the stack b/c the size of the linked list is not fixed, I.e., the queue is NEVER full.

Leonard Sheldon Howard Raj

head tail

Page 65: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Informal C++ Queue Interface

• Informal C++ interface for our Queue ADT

• Requires the definition of class EmptyQueueException

• No corresponding built-in STL class

template <class Type>class Queue {public: int size(); bool isEmpty(); Type& front()

throw(EmptyQueueException); void enqueue(Type e); Type dequeue()

throw(EmptyQueueException);};

Page 66: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Queue Summary

• Queue Operation Complexity for Different Implementations

Array Fixed-Size

ArrayExpandable (doubling strategy)

ListSingly-Linked

dequeue() O(1) O(1) O(1)

enqueue(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

front() O(1) O(1) O(1)

Size(), isEmpty() O(1) O(1) O(1)

Page 67: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Double-Ended Queues• The Double-Ended Queue, or

Deque, ADT stores arbitrary objects. (Pronounced ‘deck’)

• Richer than stack or queue ADTs. Supports insertions and deletions at both the front and the end.

• Main deque operations:– insertFirst(object o): inserts

element o at the beginning of the deque

– insertLast(object o): inserts element o at the end of the deque

– removeFirst(): removes and returns the element at the front of the deque

– removeLast(): removes and returns the element at the end of the deque

• Auxiliary deque operations:– first(): returns the element at the

front without removing it– last(): returns the element at the

front without removing it– size(): returns the number of

elements stored– isEmpty(): returns a Boolean value

indicating whether no elements are stored

• Exceptions– Attempting to execute

removeFirst,removeLast, front, or last on an empty deque throws an EmptyDequeException

Page 68: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Doubly Linked List

• A doubly linked list is a structure consisting of a sequence of nodes

• A doubly linked list stores a pointer to a special head/tail node

• Each node stores– element– link to the prev, next node

next

elemnode

head tail

prev

Leonard Sheldon Howard Raj

Page 69: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Doubly Linked List

• A doubly linked list is a structure consisting of a sequence of nodes

• A doubly linked list stores a pointer to a special head/tail node

• Each node stores– element– link to the prev, next node

next

elemnode

prev

head tail

Page 70: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Doubly Linked List Node in C++

template <class Type>class DLinkedListNode {public: Type elem;

DLinkedListNode<Type> *prev, *next;

};

next

elemnode

prev

head tail

Leonard Sheldon Howard Raj

Page 71: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Doubly Linked List

• A doubly linked list is a structure consisting of a sequence of nodes

• Operations– insertFront(e): inserts an element on the front of the list– removeFront(): returns and removes the element at the

front of the list– insertBack(e): inserts an element on the back of the list– removeBack(): returns and removes the element at the end

of the list• Private operations

– add(n, e): inserts the element after the node n– remove(n): returns and removes the element stored in the

node n

Page 72: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Adding a Node

1. Allocate a new node2. Have new node point to the previous and next

nodes3. Update the previous and next nodes to point to

the new node

head tail

Leonard Sheldon Howard Raj

Page 73: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Adding a Node

1. Allocate a new node2. Have new node point to the previous and next

nodes3. Update the previous and next nodes to point to

the new node

head tail

Leonard Sheldon Howard Raj

Bernadette

Page 74: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Adding a Node

1. Allocate a new node2. Have new node point to the previous and next

nodes3. Update the previous and next nodes to point to

the new node

head tail

Leonard Sheldon Howard Raj

Bernadette

Page 75: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Adding a Node

1. Allocate a new node2. Have new node point to the previous and next

nodes3. Update the previous and next nodes to point to

the new node

head tail

Leonard Sheldon Howard Raj

Bernadette

Page 76: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Adding a Node

1. Allocate a new node2. Have new node point to the previous and next

nodes3. Update the previous and next nodes to point to

the new node

head tail

Leonard Sheldon Howard RajBernadette

Page 77: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Adding a Node

1. Allocate a new node2. Have new node point to the previous and next

nodes3. Update the previous and next nodes to point to

the new node

head tail

Sheldon

Page 78: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Adding a Node

1. Allocate a new node2. Have new node point to the previous and next

nodes3. Update the previous and next nodes to point to

the new node

head tail

Sheldon

Page 79: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Adding a Node

1. Allocate a new node2. Have new node point to the previous and next

nodes3. Update the previous and next nodes to point to

the new node

head tail

Sheldon

Page 80: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Adding a Node

1. Allocate a new node2. Have new node point to the previous and next

nodes3. Update the previous and next nodes to point to

the new node

head tail

Sheldon

Page 81: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing a Node

1. Have the prev node’s next point to the next of the current node

2. Have the next node’s prev point to the prev of the current node

3. Delete the current node

head tail

Leonard Sheldon Howard Raj

Page 82: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing a Node

1. Have the prev node’s next point to the next of the current node

2. Have the next node’s prev point to the prev of the current node

3. Delete the current node

head tail

Leonard Sheldon Howard Raj

Page 83: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing a Node

1. Have the prev node’s next point to the next of the current node

2. Have the next node’s prev point to the prev of the current node

3. Delete the current node

head tail

Leonard Sheldon Howard Raj

Page 84: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing a Node

1. Have the prev node’s next point to the next of the current node

2. Have the next node’s prev point to the prev of the current node

3. Delete the current node

head tail

Leonard Sheldon Howard Raj

Page 85: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Removing a Node

1. Have the prev node’s next point to the next of the current node

2. Have the next node’s prev point to the prev of the current node

3. Delete the current node

head tail

Leonard Sheldon Raj

Page 86: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Deque with a Doubly Linked List

• We can implement a deque with a doubly linked list– The front element is pointed to by head– The rear element is pointed to by tail

• The space used is O(n) and each operation of the Deque ADT takes O(1) time

head tail

Leonard Sheldon Howard Raj

Page 87: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Performance and Limitations - doubly linked list implementation of deque ADT

• Performance– Let n be the number of elements in the deque– The space used is O(n)

– Each operation runs in time O(1)

• Limitations– NOTE: we do not have the limitation of the array

based implementation on the size of the deque b/c the size of the linked list is not fixed, I.e., the deque is NEVER full.

Page 88: Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.

Deque Summary

• Deque Operation Complexity for Different Implementations

Array Fixed-Size

ArrayExpandable (doubling strategy)

ListSingly-Linked

List Doubly-Linked

removeFirst(), removeLast()

O(1) O(1) O(1) – removeFirst, O(n) – removeLast

O(1)

insertFirst(o), InsertLast(o)

O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1) O(1)

first(), last O(1) O(1) O(1) O(1)

size(), isEmpty()

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