So what's next? Introduction to data structures Two Perspectives: Abstract description...
-
Upload
kian-skidgel -
Category
Documents
-
view
218 -
download
0
Transcript of So what's next? Introduction to data structures Two Perspectives: Abstract description...
![Page 1: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/1.jpg)
So what's next?Introduction to data structures
Two Perspectives:
• Abstract description (capabilities)
• Implementation(s)
For structures:
• Stack
• Queue
• Deque (maybe)
![Page 2: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/2.jpg)
Clicker quiz12/3/13
CSE 1102 Fall 2013
![Page 3: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/3.jpg)
The country with the red star on it is:
A. Canada
B. Sweden
C. Latvia
D. Finland
E. None of the above
![Page 4: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/4.jpg)
A. "Finland"
B. "is"
C. "my"
D. "home"
E. None of the above
Suppose I have a stack s that can store Strings, and I execute the following statements starting with an empty stack:
1. s.push("Finland");2. s.push("is");3. s.push("my");4. String w = s.peek();5. String x = s.pop();6. s.push("home");7. String y = s.pop();8. String z = s.pop();
What is the value of z?
![Page 5: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/5.jpg)
A. "Sweden"
B. "is"
C. "my"
D. "neighbor"
E. None of the above
Suppose I have a queue q that can store Strings, and I execute the following statements starting with an empty queue:
1. q.enqueue("Sweden");2. q.enqueue("is");3. q.enqueue("my");4. String w = q.dequeue();5. String x = q.peek();6. q.enqueue("neighbor");7. String y = q.dequeue();8. String z = q.dequeue();
What is the value of z?
![Page 6: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/6.jpg)
Clicker questions and stuff12/3/13
CSE 1102 Fall 2013
![Page 7: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/7.jpg)
Abstract Data Types (1970s)
Description of a data structure that includes only its operations, not its implementation
In Java, ADTs are best modeled by interfaces
![Page 8: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/8.jpg)
Example ADT: Stack
Operations:• push(element) – adds element to "top" of
the stack• pop – returns the top element of the stack,
which is removed from the stack • peek – returns the top element of the
stack without changing the stack• isEmpty – returns true if stack is empty, false otherwise
![Page 9: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/9.jpg)
Stack
• intuitions
• examples in the world
• uses in computing
![Page 10: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/10.jpg)
A. "Iceland"
B. "cool"
C. true
D. false
E. None of the above
Suppose I have a stack S that can store Strings, and I execute the following statements starting with an empty stack:
1. S.push("Iceland");2. S.push("is");3. String w = S.peek();4. String x = S.pop();5. S.push("cool");6. String y = S.pop();7. boolean z = S.isEmpty();
What is the value of z?
![Page 11: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/11.jpg)
Example ADT: Queue
Operations:• enqueue(element) – adds element to
"back" of the queue• dequeue – returns the "front" element of
the queue, which is removed from the queue
• front – returns the front element of the queue without changing the queue
• isEmpty – returns true if queue is empty, false otherwise
![Page 12: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/12.jpg)
Queue
• intuitions
• examples in the world
• uses in computing
![Page 13: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/13.jpg)
A. "Sweden"
B. "is"
C. "my"
D. "neighbor"
E. None of the above
Suppose I have a queue q that can store Strings, and I execute the following statements starting with an empty queue:
1. q.enqueue("Sweden");2. q.enqueue("is");3. q.enqueue("my");4. String w = q.dequeue();5. String x = q.peek();6. q.enqueue("neighbor");7. String y = q.dequeue();8. String z = q.dequeue();
What is the value of z?
![Page 14: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/14.jpg)
Suppose we have a class that acts as a Holder for Colors:
public class ColorHolder {
private Color _myColor;
public ColorHolder(Color color) {
_myColor = color;
}
public Color getValue(){
return _myColor;
}
public void setColor (Color col) {
_myColor = col;
}
}
It might be useful to have a more general version, that could hold anything.
Quick Topic 1: Generics
![Page 15: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/15.jpg)
Java allows us to define a class generically, with type declared at instantiation time:
public class Holder<ValType> {private ValType _myValue;public Holder(ValType value) {
_myValue = value;}
public ValType getValue(){return _myValue;}
public void setValue (ValType value) { _myValue = value;}
}
To use this class, you need to declare and instantiate object with actual type:
Holder<Color> currentColor;currentColor = new Holder<Color>(Color.red);
More examples in Chapter 14!
![Page 16: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/16.jpg)
Example: Node class holds an element of some type, plus another Node of the same sort.
public class Node<ValType> {private ValType _myValue;private Node<ValType> _nextpublic Node(ValType value) {
_myValue = value;_next = null;}
public ValType getValue(){return _myValue;}
public void setValue (ValType value) { _myValue = value;}
public Node<ValType> getNext(){return _next;
}} Since _next is a Node of the same sort, we can chain instances of
these together.
![Page 17: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/17.jpg)
Suppose we want to diagram a set of instances and their relationships
Answer: object (or instance) diagram
Quick Topic 2: Object Diagrams
For example, a sequence of two Nodes whose elements are Cars
![Page 18: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/18.jpg)
"syntax"
![Page 19: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/19.jpg)
Topic 3: Are you on this list?
Bidhan AdhikariDevin DelaneyErming GaoChristopher LawrenceJeffrey MetterJonathan Rarey
![Page 20: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/20.jpg)
ADT: defined by capabilities
In Java: a natural fit to Interfaces, e.g.
public interface StackADT<ElementType>{
void push(ElementType t);
ElementType pop();
ElementType peek();
boolean isEmpty();
}
![Page 21: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/21.jpg)
So let's use Nodes to implement a Stack
One instance variable, _top, which refers to a Node
Stack<Car> fred = new Stack<Car>();
// create empty Stack
// now add some stuff
fred.push(new Car(Color.RED));
fred.push(new Car(Color.BLUE));
![Page 22: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/22.jpg)
So let's use Nodes to implement a Stack ctd.
One instance variable, _top, which refers to a Node
// now pop something
Car ethyl = fred.pop();
// what actually happens is this
![Page 23: So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.](https://reader036.fdocuments.us/reader036/viewer/2022062312/551c3a1f550346ea388b48e2/html5/thumbnails/23.jpg)
So let's use Nodes to implement a Stack
public class Stack<ElType> implements StackADT<ElType>{private Node<EiType> _top;public Stack() {
_top = null;}public void push(EiType value) {
Node<EiType> nodeToPush = new Node<EiType>(value);nodeToPush.setNext(_top);_top = nodeToPush;
}public EiType pop() {
EiType retVal = _top.getValue();_top = _top.getNext();return retVal;
}public boolean isEmpty(){
return _top == null;}
}