Stacks, Queues, and Bucket Sort - PowerPoint PPT Presentation

1 / 57
About This Presentation
Title:

Stacks, Queues, and Bucket Sort

Description:

... in a class like Reference to store the data, see TA for grade ... Stack used to store return locations for calls to methods. Note how return follows stack order ... – PowerPoint PPT presentation

Number of Views:564
Avg rating:3.0/5.0
Slides: 58
Provided by: anselmo9
Category:
Tags: bucket | queues | sort | stacks

less

Transcript and Presenter's Notes

Title: Stacks, Queues, and Bucket Sort


1
Stacks, Queues, and Bucket Sort
  • COMP 114
  • Thursday April 11

2
Announcements
  • Program 2 grading
  • If you used a String in a class like Reference to
    store the data, see TA for grade change
  • 10 pt. Instead of 20 pt.
  • Program 4 due Tuesday

3
Topics
  • Last Time
  • Lists
  • This Class
  • Stacks
  • Queues

4
Stacks
  • A stack is a last-in, first-out data structure.
  • Think of a stack of paper or books
  • put paper on top
  • remove paper from top
  • These operations are called push and pop
  • push onto the stack
  • pop from the stack

5
Processor Stack
  • Stack used to store return locations for calls to
    methods
  • Note how return follows stack order
  • New call, push to stack
  • Return, always top of stack
  • More info later

6
Interface
  • push(Object p)
  • Same as add(Object p)
  • Object pop()
  • Same as remove()
  • Object peek() Optional
  • int size() Optional

7
Implementations
  • Several standard ways
  • List
  • Vector/Array
  • Combination

8
List
Head of list is latest element.
Head
9
List pushing to stack
Head
10
List pushing to stack
Head
11
Popping stack
Head
12
Popping stack
Head
13
Popping stack
Head
Return data.
data5
14
Vector Implementation
0
Size 6
1
2
3
4
5
6
7
8
9
15
Push
0
Size 6
1
2
3
4
5
6
7
8
9
16
Push
0
Size 7
1
2
3
4
5
6
7
8
9
17
Pop
0
Size 7
1
2
3
4
5
6
7
8
9
18
Pop
0
Return ref. to data
Size 6
1
2
3
data6
4
5
6
7
8
9
19
Real Implementations
  • Would you implement a processors stack like
    this?
  • Advantages
  • Disadvantages
  • Normally with hardware assist
  • Stack pointer

20
List of Stacks
Theyre more than just arrays. Have to keep size
and next location.
21
What Good Are Stacks?
  • Used to implement method (function) calls
  • Call frame
  • Program counter
  • Parameters
  • Local variables
  • Have you run out of stack space?
  • Of course can be used to do recursion manually

22
Queues
  • First in, first out, or FIFO
  • Line at movie or grocery-store checkout
  • Print queue
  • Operations
  • enqueue
  • dequeue

23
Interface
  • enqueue(Object p)
  • Same as add(Object p)
  • Object dequeue()
  • Same as Object remove()
  • Object peek()

24
Implementations
  • List based
  • Vector based
  • Circular array
  • Fixed size

25
List (doubly linked)
Tail of list is latest element.
Tail
Head
26
Enqueue
Tail
Head
27
Enqueue
Tail
Head
28
Dequeue
Remove oldest element
Tail
Head
29
Dequeue
Remove oldest element
Tail
Head
30
Dequeue
Return
Tail
data1
Head
31
Vector-Based Queue
0
1
2
3
4
5
6
7
8
Queue new entries to end.
32
Vector-Based Queue
0
1
2
3
4
5
6
7
8
Dequeue entries from beginning.
Whats the cost?
33
Array-Based Queues
  • If we have an upper bound, an array has multiple
    advantages
  • We can wrap around

34
Array-Based Queue
0
1
2
3
4
5
6
7
8
Enqueue
35
Array-Based Queue
0
1
2
3
4
5
6
7
8
36
Array-Based Queue
0
1
2
3
4
5
6
7
8
Enqueue another??
37
Array-Based Queue
0
1
2
3
4
5
6
7
8
Warp around!
38
Array-Based Queue
0
1
2
3
4
5
6
7
8
39
How do you compute wrap?
  • Mod operator
  • location location size_of_stack
  • Like remainder
  • Divides by size of stack, return remainder
  • Examples
  • 4 5 ? 4
  • 5 5 ? 0
  • 6 5 ? 1

40
What if you run out?
  • Some queues are fixed size
  • Old keyboard queue in DOS
  • Tossed out newer keystrokes
  • You could toss older data in other apps
  • Maybe for a temperature monitor
  • Alternative Copy

41
Priority Queues
  • A hospital
  • Triage
  • Used to schedule jobs/processes
  • By priority
  • Assign a range of priorities
  • Keep a separate list-based queue for each

42
Many Uses in Computers
  • Most hardware has queues
  • Called FIFOs
  • Characters input
  • Ethernet

43
Summary
  • Stacks
  • Used for subroutine calls, local variables
  • Queues
  • Used to buffer data

44
Problem
  • In creating something like a Linked List class,
    you need to access LinkedListNode
  • But want to keep it private from rest of program
  • Can use package and keyword protected
  • How to make package
  • Then how to use protected

45
How to Make Package
  • First make empty VJ project with same name as
    package.
  • I named example lastra

46
Create or Copy Files
  • For my example, Ill copy ReadURL and Keyboard

47
Make Classes part of Package
  • Add package lastra to top of files.

48
Compile (Build)
  • A folder names lastra will be created in project

49
Copy Folder
  • Copy the folder to a convenient place
  • I used c\java
  • Youll use this as item in CLASSPATH

50
Use New Package
  • Create new project
  • Add c\java to classpath tab in Project
    Properties

51
Add Import to Classes
  • Add import directive, just as when using other
    packages
  • Can now use package classes

52
Dont Forget!
  • If you make changes, you must copy again to
    package folder
  • Alternatively, you could import from location of
    VJ project

53
Protected
  • You can now make the ListNode constructor
    protected
  • Only accessible from classes in package

54
Inner Classes
  • Alternative way to hide LinkedListNode
  • public class LinkedList
  • private LinkedListNode head
  • ...
  • private class LinkedListNode
  • private Object data
  • private LinkedListNode next
  • private LinkedListNode(...)
  • data v
  • next n

55
Inner Classes vs. Package
  • Package allows access from a number of classes
  • Maybe necessary if you plan to have a lot of
    functionality
  • Maybe some type of sort
  • Different types of Linked Lists that use common
    code base

56
Examples
  • On the class code page
  • MinimalLinkedList_Inner.zip
  • MinimalLinkedList_Package.zip

57
Next Time
  • Trees
  • more complex data structure
  • often built on lists
  • Good for searching
Write a Comment
User Comments (0)
About PowerShow.com