Download presentation
Presentation is loading. Please wait.
Published byAnna Ackerly Modified over 9 years ago
1
Today’s Agenda Stacks Queues Priority Queues CS2336: Computer Science II
2
Data structures Arrays, linked lists, … are best for data that represents real objects. Stacks and Queues are used to complete a task and are soon after discarded A major difference is Stacks and Queues only allow a single item to be added or removed at a time. Stack: LIFO Queue: FIFO CS2336: Computer Science II
3
Stack Implement Stack using linkedlists (see the demo) Each operation takes constant time Also we need extra space Objects (16*2 bytes overhead) Two references(item and next) 8 bytes Total ~40 bytes For N items we may have 40N bytes So there is better implementation available Specially that stacks are use in the loop some algorithms it is important to use the faster and better implementation Use array to store items CS2336: Computer Science II
4
Stacks Stacks are simpler than queues We show them as an Array of String[] It holds all of the values that are push to the stack We monitor The size of the stack The size of the array The top of stack It is last in first out When it is empty then its value is “-1” CS2336: Computer Science II
5
Stacks: array implementation A “push” method to put an item into stack Check if stack is not full if “true” then put the input item to the top of the array Increment the array size by one A “pop” method to remove the last information out of stack Check if the stack is not empty If “true” then we make the top of stack unavailable (“-1”) Just like memory, when you delete an item it is still there but it is not available, we simulate that Decrement the top of stack A “peek” method to see what is at the top of stack, but not remove it Also “PuchMany”, “PopAll”, “isEmpty, size… CS2336: Computer Science II
6
Stack example top10 CS2336: Computer Science II s.Push (10); top15 10 s.Push (15); top10 s.Remove(); Stack s = new Stack(10)
7
Stack: resizing-array implementation Problem: requiring the user to provide a size for the array! How to grow array? First try: increase and decrease size of array by 1 for any push and pop Expensive Need to create a new array size one bigger and copy all items Inserting first N items take proportional to 1+2+…+n ~ O(n 2 ) How do the resizing but ensure that resizing happens infrequently? CS2336: Computer Science II
8
Stack: resizing-array implementation Alternative: When array is full, create a new array of twice the size, and copy items. Repeated doubling technique Inserting first N item is (worst case O(n) ) One array access per push therefore N array accesses for N items ~ O(n) K array accesses to double the size (2+4+8+…+N) ~ O(n) Ignoring the cost to create new array! CS2336: Computer Science II
9
Stack: resizing-array implementation How to shrink array? First Try: Push : double size of the array when it is full Pop : halve size of the array when array is one-half full TOO Expensive in worst case Consider push-pop-push-pop.. Sequence when array if full. Efficient solution: Push : double size of the array when it is full Pop : halve size of the array when array is one-quarter full (between 25% and 100% full) We don’t call resize more often CS2336: Computer Science II
10
Stack: Resizing-array vs. linked list Tradeoffs: Which one is better? Linked List Implementation Every operation takes constant time in worst case Uses extra time and space to deal with the links Slower Resizing-array Implementation Every operation takes constant time in average. Less space Faster In internet switch that ratio of packets/sec is high you don’t want to have some operations that they get suddenly very slow! So you can use linked list If you only care about the total amount of time then you can use resizing- array CS2336: Computer Science II
11
Queue Queues are complex than Stacks We show them as a linkedlist first and then array of String[] and then resizing-array We monitor: The size of queue The front It is first in first out The end (rear) The number of items CS2336: Computer Science II
12
Queue: array implementation An “insert” method Check if queue is not full If “true” we add that input to the end of the queue Increment the end of the queue pointer Increment the size of the queue Increment the number of items A “remove” method Check if the queue is not empty If “true” we remove an item from the front of the queue Increment the front pointer Decrement number of items A “peek” method to see what is at the front of queue, but not remove it CS2336: Computer Science II
13
Queue example 10 rear front CS2336: Computer Science II Insert(10) 1510 rearfront Insert(15) 111510 rearfront Insert(11) 1115 rearfront remove() Peek() 11 rear front remove()
14
Queue: resizing-array implementation Update front and rear modulo the capacity Add resizing array the same as stack Leave that as an exercise CS2336: Computer Science II
15
Priority Queue add items in order from high to low A “priorityInsert” method If the input is the first item just insert it in the queue Call the “insert” method If the new input is greater that the one that it is inside the array the we have to shift them (see example) CS2336: Computer Science II
16
Priority queue example 10 rear front CS2336: Computer Science II priorityInsert(10) 1015 rearfront priorityInsert(15) 10111519 rearfront priorityInsert(11) priorityInsert(19) 101115 rearfront remove() 1011 rearfront remove() Peek()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.