Upcoming SlideShare
×

# Lists, queues and stacks 1

927 views
714 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
927
On SlideShare
0
From Embeds
0
Number of Embeds
406
Actions
Shares
0
20
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Lists, queues and stacks 1

1. 1. Lists, Queues, Sta cks CST200 – Week 5: Array based Implementation Instructor: Andreea Molnar
2. 2. Outline • Lists • Queues • Stacks
3. 3. List A 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
4. 4. Stack A 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.
5. 5. 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
6. 6. Stack Assuming that one has an object myStack: myStack.push (25) 25 myStack
7. 7. Stack Assuming that one has an object myStack: myStack.push (25) myStack.push (10) 10 25 myStack
8. 8. Stack Assuming that one has an object myStack: myStack.push (25) myStack.push (10) 10 25 myStack int topValue = myStack.pop () 10 topValue
9. 9. Stack Assuming that one has an object myStack: myStack.push (25) myStack.push (10) int topValue = myStack.pop () 5 25 mySta ck myStack.push (5) 10 topVa lue
10. 10. Stack Assuming that one has an object myStack myStack.push (25) myStack.push (10) 5 25 myStack int topValue = myStack.pop () myStack.push (5) 10 topValue
11. 11. Stack Assuming 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 10 myStack topValue
12. 12. Stack Assuming 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 20 myStack topValue
13. 13. 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.
14. 14. Queue Functionality: • • • enqueue: add an item to the back of the queue dequeue: remove the front item getFront: get the front item
15. 15. Queue Assuming that one has an object myQueue: myQueue.enque (25) 25 myQueue
16. 16. Queue Assuming that one has an object myQueue: myQueue.enque (25) int value = myQueue.deque () 25 25 myQueue value
17. 17. Queue Assuming that one has an object myQueue: myQueue.enque (25) int value = myQueue.deque () 20 myQueue myQueue.enque (20) 25 value
18. 18. Queue Assuming that one has an object myQueue: myQueue.enque (25) int value = myQueue.deque () myQueue.enque (20) 20 5 myQueue myQueue.enque (5) 25 value
19. 19. Queue Assuming that one has an object myQueue: myQueue.enque (25) int value = myQueue.deque () myQueue.enque (20) 20 5 myQueue myQueue.enque (5) value = myQueue.deque () 20 value
20. 20. 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.