Lists, Stacks, and Queues
Lists, Stacks, and Queues
Lists, Stacks, and Queues
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 1 / 50
Abstract Data Types (ADT)
Data type
I a set of objects + a set of operations
I Example: integer
F set of integer numbers
F operations: +, -, x, /
Can this be generalized? e.g. procedures generalize the notion of
an operator
Abstract data type
I what can be stored in the ADT
I what operations can be done on/by the ADT
Encapsulation
I Operations on ADT can only be done by calling appropriate
functions
I no mention of how the set of operations is implemented
I ADT → C++: class
I method → C++: member function
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 2 / 50
Abstract Data Types (Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 3 / 50
Object-Oriented Design
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 4 / 50
More about OOD
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 5 / 50
Client Code Using DateType
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 6 / 50
2 Separate Files Generally Used for Class Type
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 7 / 50
DateType Class Instance Diagrams
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 8 / 50
Implementation of DateType member functions
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 9 / 50
Implementation of DateType member functions
(Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 10 / 50
The List ADT
A1 , A2 , A3 , · · · , AN
N: length of the list
A1 : first element
AN : last element
If N = 0, then empty list
I Ai precedes Ai+1
I Ai follows Ai−1
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 11 / 50
List Operations
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 12 / 50
Implementation of an ADT
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 13 / 50
Array Implementation
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 14 / 50
Pointer Implementation (Linked List)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 15 / 50
Linked Lists
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 16 / 50
A Simple Linked List Class
Declare Node class for the nodes
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 17 / 50
Inserting a New Node
Node* InsertNode(int index, double x)
I Insert a node with data equal to x after the index’th elements.
(i.e., when index = 0, insert the node as the first element; when
index = 1, insert the node after the first element, and so on)
I If the insertion is successful, return the inserted node. Otherwise,
return NULL.
(If index is < 0 or > length of the list, the insertion will fail.)
Steps
I Locate index’th element
I Allocate memory for the new node
I Point the new node to its successor
I Point the new nodes predecessor to the new node
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 18 / 50
Inserting a New Node (Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 19 / 50
Inserting a New Node (Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 20 / 50
Inserting a New Node (Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 21 / 50
Inserting a New Node (Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 22 / 50
Deleting a Node
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 23 / 50
Deleting a Node (Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 24 / 50
Destroying the list
∼List(void)
I Use the destructor to release all the memory used by the list.
I Step through the list and delete each node one by one.
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 25 / 50
Variations of Linked Lists
Circular linked lists
I The last node points to the first node of the list
I How do we know when we have finished traversing the list? (Tip:
check if the pointer of the current node is equal to the head.)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 26 / 50
Doubly-Linked List
remove(X)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 27 / 50
Sentinel Nodes
Dummy head and tail nodes to avoid special cases at ends of list
Doubly-linked list with sentinel nodes
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 28 / 50
Link Inversion
Forward:
··· A → B → C → D ···
⇑ ↑
prev pres
··· ← A B → C → D ···
⇑ ↑
··· ← A ← B C → D ···
⇑ ↑
Backward ...
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 29 / 50
Exclusive-Or Doubly Linked List
Move forward: L
at C, take addr(B), XOR it with C’s link value (i.e., B D), yields
addr(D), and you can continue traversing the list.
Move backward: Similar
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 30 / 50
C++ Standard Template Library (STL)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 31 / 50
Lists Using STL
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50
Iterators
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 33 / 50
Example: The Polynomial ADT
An ADT for single-variable polynomials
N
X
f (x) = ai xi
i=0
Array implementation
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 34 / 50
The Polynomial ADT
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 35 / 50
Stack ADT
Stack is a list where insert and remove take place only at the top
Operations: Constant time
I Push - inserts element on top of stack
I Pop - removes element from top of stack
I Top - returns element at top of stack
I Empty, size
LIFO (Last In First Out)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 36 / 50
Stack Implementation
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 37 / 50
Stack Implementation (cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 38 / 50
Applications of Stack
Balancing symbols
I Compiler checks for program syntax errors
I Every right brace, bracket, and parenthesis must correspond to its
left counterpart
I The sequence [()] is legal, but [(]) is wrong
Postfix Expressions
Infix to Postfix Conversion
Function calls
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 39 / 50
Balancing Symbols
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 40 / 50
Evaluation of Postfix Expressions
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 41 / 50
Infix to Postfix Conversion
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 42 / 50
Infix to Postfix Conversion (Cont’d)
Infix: a + b * c + ( d * e + f ) * g
Postfix: a b c * + d e * f + g * +
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 43 / 50
Applications of Stack: Function Calls
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 44 / 50
Queue ADT
Queue is a list where insert takes place at the back, but remove
takes place at the front
Operations
I Enqueue - inserts element at the back of queue
I Dequeue - removes and returns element from the front of the queue
FIFO (First In First Out)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 45 / 50
Queue Implementation of Array
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 46 / 50
Empty or Full?
Empty queue:
I back = front - 1
Full queue?
I the same!
I Reason: n values to represent n + 1 states
Solutions
I Use a boolean variable to say explicitly whether the queue is empty
or not
I Make the array of size n+1 and only allow n elements to be stored
I Use a counter of the number of elements in the queue
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 47 / 50
Queue Implementation based on Linked List
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 48 / 50
Queue Implementation based on Linked List (Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 49 / 50
Applications of Queue
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 50 / 50