Professional Documents
Culture Documents
2. Implementation of Stacks
3. Applications of Stack
5. Implementation of Queue
6. Applications of Queue
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 1 / 52
Outcomes
• L.O.2.1 - Depict the following concepts: (a) array list and linked list, including single link
and double links, and multiple links; (b) stack; and (c) queue and circular queue.
• L.O.2.2 - Describe storage structures by using pseudocode for: (a) array list and linked
list, including single link and double links, and multiple links; (b) stack; and (c) queue and
circular queue.
• L.O.2.3 - List necessary methods supplied for list, stack, and queue, and describe them
using pseudocode.
• L.O.2.4 - Implement list, stack, and queue using C/C++.
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 2 / 52
Outcomes
• L.O.2.5 - Use list, stack, and queue for problems in real-life, and choose an appropriate
implementation type (array vs. link).
• L.O.2.6 - Analyze the complexity and develop experiment (program) to evaluate the
eciency of methods supplied for list, stack, and queue.
• L.O.8.4 - Develop recursive implementations for methods supplied for the following
structures: list, tree, heap, searching, and graphs.
• L.O.1.2 - Analyze algorithms and use Big-O notation to characterize the computational
complexity of algorithms composed by using the following control structures: sequence,
branching, and iteration (not recursion).
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 3 / 52
Basic operations of Stacks
Linear List Concepts
General list:
• No restrictions on which operation can be used on the list.
• No restrictions on where data can be inserted/deleted.
Restricted list:
• Only some operations can be used on the list.
• Data can be inserted/deleted only at the ends of the list.
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 4 / 52
Linear list concepts
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 5 / 52
Stack
Denition
A stack of elements of type T is a nite sequence of elements of T, in which all insertions and
deletions are restricted to one end, called the top.
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 6 / 52
Basic operations of Stacks
Basic operations:
• Construct a stack, leaving it empty.
• Push an element: put a new element on to the top of the stack.
• Pop an element: remove the top element from the top of the stack.
• Top an element: retrieve the top element.
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 7 / 52
Basic operations of Stacks
Extended operations:
• Determine whether the stack is empty or not.
• Determine whether the stack is full or not.
• Find the size of the stack.
• Clear the stack to make it empty.
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 8 / 52
Basic operations of Stacks: Push
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 9 / 52
Basic operations of Stacks: Push
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 10 / 52
Basic operations of Stacks: Pop
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 11 / 52
Basic operations of Stacks: Pop
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 12 / 52
Basic operations of Stacks: Top
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 13 / 52
Basic operations of Stacks: Top
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 14 / 52
Implementation of Stacks
Linked-list implementation
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 15 / 52
Linked-list implementation
Stack structure
stack
count <i n t e g e r >
top <node p o i n t e r >
end s t a c k
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 16 / 52
Create an empty Linked Stack
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 17 / 52
Create an empty Linked Stack
stack.count = 0
stack.top = null
return
End createStack
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 18 / 52
Push data into a Linked Stack
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 20 / 52
Push data into a Linked Stack
if then
stack full
success = false
else
allocate (pNew)
pNew -> data = data
pNew -> next = stack.top
stack.top = pNew
stack.count = stack.count + 1
success = true
end
return success
End pushStack
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 21 / 52
Push data into a Linked Stack
• Push is successful when allocation memory for the new node is successful.
• There is no dierence between push data into a stack having elements and push data into
an empty stack (top having NULL value is assigned to pNew->next: that's corresponding
to a list having only one element).
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 22 / 52
Pop Linked Stack
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 24 / 52
Pop Linked Stack
if stack emptythen
success = false
else
dltPtr = stack.top
dataOut = stack.top -> data
stack.top = stack.top -> next
stack.count = stack.count - 1
recycle(dltPtr)
success = true
end
return success
End popStack
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 25 / 52
Pop Linked Stack
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 26 / 52
Stack Top
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 27 / 52
Stack Top
if then
stack empty
success = false
else
dataOut = stack.top -> data
success = true
end
return success
End stackTop
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 28 / 52
Destroy Stack
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 29 / 52
Destroy Stack
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 30 / 52
isEmpty Linked Stack
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 31 / 52
Applications of Stack
Applications of Stack
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 32 / 52
Basic operations of Queues
Queue
Denition
A queue of elements of type T is a nite sequence of elements of T, in which data can only be
inserted at one end called the rear, and deleted from the other end called the front.
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 33 / 52
Basic operations of Queues
Basic operations:
• Construct a queue, leaving it empty.
• Enqueue: put a new element in to the rear of the queue.
• Dequeue: remove the rst element from the front of the queue.
• Queue Front: retrieve the front element.
• Queue Rear: retrieve the rear element.
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 34 / 52
Basic operations of Queues: Enqueue
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 35 / 52
Basic operations of Queues: Dequeue
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 36 / 52
Basic operations of Queues: Queue Front
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 37 / 52
Basic operations of Queues: Queue Rear
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 38 / 52
Implementation of Queue
Linked-list implementation
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 39 / 52
Linked-list implementation
Queue structure
queue
count <i n t e g e r >
f r o n t <node p o i n t e r >
r e a r <node p o i n t e r >
endqueue
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 41 / 52
Create Queue
queue.count= 0
queue.front = null
queue.rear = null
return
End createQueue
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 42 / 52
Enqueue: Insert into an empty queue
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 43 / 52
Enqueue: Insert into a queue with data
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 44 / 52
Enqueue
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 45 / 52
Enqueue
if then
queue full
return false
end
allocate (newPtr)
newPtr -> data = data
newPtr -> next = null
if queue.count = 0 then
queue.front = newPtr // Insert into an empty queue
else
queue.rear -> next = newPtr // Insert into a queue with data
end
queue.rear = newPtr
queue.count = queue.count + 1
return true
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 46 / 52
Dequeue: Delete data in a queue with only one item
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 47 / 52
Dequeue: Delete data in a queue with more than one item
Figure 10: Delete data in a queue with more than one item
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 48 / 52
Dequeue
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 49 / 52
Dequeue
• Polynomial Arithmetic
• Categorizing Data
• Evaluate a Prex Expression
• Radix Sort
• Queue Simulation
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 51 / 52