lists, queues and stacks 1
TRANSCRIPT
![Page 1: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/1.jpg)
Lists, Queues, Stacks
CST200 – Week 5: Array based Implementation
Instructor: Andreea Molnar
![Page 2: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/2.jpg)
Outline
•Lists
•Queues
•Stacks
![Page 3: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/3.jpg)
ListA sequence of items that has at least the functionality below:
•Accessing an item at a given position
•Adding an item at a given position
•Removing an item
•Determining the number of items
![Page 4: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/4.jpg)
StackA sequence of items in which:
•An item can be inserted only to the top
•Only the top item can be accessed/removed
In a stack items are inserted and removed according to the last-in first-out (LIFO) principle.
![Page 5: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/5.jpg)
Stack
Functionality:
• push: insert an item to the top of the stack
• pop: remove the item at the top of the stack
• peek: get the item at the top of the stack
• isEmpty: assess if the stack is empty
• isFull: assess if the stack is full
![Page 6: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/6.jpg)
Stack
Assuming that one has an object myStack:
myStack.push (25) 25 myStack
![Page 7: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/7.jpg)
Stack
Assuming that one has an object myStack:
myStack.push (25)
myStack.push (10)10
25
myStack
![Page 8: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/8.jpg)
StackAssuming that one has an object myStack:
myStack.push (25)
myStack.push (10)
int topValue = myStack.pop ()
10
25myStack
10 topValue
![Page 9: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/9.jpg)
StackAssuming that one has an object myStack:
myStack.push (25)
myStack.push (10)
int topValue = myStack.pop ()
myStack.push (5)
5
25myStack
10 topValue
![Page 10: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/10.jpg)
StackAssuming that one has an object myStack
myStack.push (25)
myStack.push (10)
int topValue = myStack.pop ()
myStack.push (5)
5
25myStack
10 topValue
![Page 11: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/11.jpg)
StackAssuming that one has an object myStack:
myStack.push (25)
myStack.push (10)
int topValue = myStack.pop ()
myStack.push (5)
myStack.push (20)
20
5
25
myStack
10 topValue
![Page 12: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/12.jpg)
StackAssuming that one has an object myStack:
myStack.push (25)
myStack.push (10)
int topValue = myStack.pop ()
myStack.push (5)
myStack.push (20)
topValue = myStack.pop ()
20
5
25
myStack
20 topValue
![Page 13: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/13.jpg)
Queue
A sequence of items in which the first element inserted is the first one to be removed.
In a queue elements are inserted and removed based on the the first-in first-out (FIFO) principle.
![Page 14: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/14.jpg)
Queue
Functionality:
• enqueue: add an item to the back of the queue
• dequeue: remove the front item
• getFront: get the front item
![Page 15: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/15.jpg)
Queue
Assuming that one has an object myQueue:
myQueue.enque (25) 25 myQueue
![Page 16: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/16.jpg)
Queue
Assuming that one has an object myQueue:
myQueue.enque (25)
int value = myQueue.deque () 25 myQueue
25 value
![Page 17: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/17.jpg)
Queue
Assuming that one has an object myQueue:
myQueue.enque (25)
int value = myQueue.deque ()
myQueue.enque (20)
20 myQueue
25 value
![Page 18: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/18.jpg)
QueueAssuming that one has an object myQueue:
myQueue.enque (25)
int value = myQueue.deque ()
myQueue.enque (20)
myQueue.enque (5)
20 5 myQueue
25 value
![Page 19: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/19.jpg)
Queue
Assuming that one has an object myQueue:
myQueue.enque (25)
int value = myQueue.deque ()
myQueue.enque (20)
myQueue.enque (5)
value = myQueue.deque ()
20 5 myQueue
20 value
![Page 20: Lists, queues and stacks 1](https://reader036.vdocuments.mx/reader036/viewer/2022082702/55588f03d8b42a5d328b5521/html5/thumbnails/20.jpg)
Summary
• A list is sequence of items that allows to access, add and remove an arbitrary element and determine the numbers of items (length/size of the list).
• A stack is a data structure in which an item can be added, accessed or removed only from the top.
• A queue is a data structure in which the first element added is the first one to be accessed/removed.