1 / 23

So what's next?

So what's next?. Introduction to data structures. Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque (maybe). Clicker quiz 12/3/13. CSE 1102 Fall 2013. The country with the red star on it is: Canada Sweden Latvia

finian
Download Presentation

So what's next?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. So what's next? Introduction to data structures • Two Perspectives: • Abstract description (capabilities) • Implementation(s) • For structures: • Stack • Queue • Deque (maybe)

  2. Clicker quiz12/3/13 CSE 1102 Fall 2013

  3. The country with the red star on it is: • Canada • Sweden • Latvia • Finland • None of the above

  4. Suppose I have a stack s that can store Strings, and I execute the following statements starting with an empty stack: • s.push("Finland"); • s.push("is"); • s.push("my"); • String w = s.peek(); • String x =s.pop(); • s.push("home"); • String y = s.pop(); • String z = s.pop(); • What is the value of z? • "Finland" • "is" • "my" • "home" • None of the above

  5. Suppose I have a queue q that can store Strings, and I execute the following statements starting with an empty queue: • q.enqueue("Sweden"); • q.enqueue("is"); • q.enqueue("my"); • String w = q.dequeue(); • String x = q.peek(); • q.enqueue("neighbor"); • String y = q.dequeue(); • String z = q.dequeue(); • What is the value of z? • "Sweden" • "is" • "my" • "neighbor" • None of the above

  6. Clicker questions and stuff12/3/13 CSE 1102 Fall 2013

  7. 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

  8. 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

  9. Stack intuitions examples in the world uses in computing

  10. Suppose I have a stack S that can store Strings, and I execute the following statements starting with an empty stack: • S.push("Iceland"); • S.push("is"); • String w = S.peek(); • String x =S.pop(); • S.push("cool"); • String y = S.pop(); • boolean z = S.isEmpty(); • What is the value of z? • "Iceland" • "cool" • true • false • None of the above

  11. 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

  12. Queue intuitions examples in the world uses in computing

  13. Suppose I have a queue q that can store Strings, and I execute the following statements starting with an empty queue: • q.enqueue("Sweden"); • q.enqueue("is"); • q.enqueue("my"); • String w = q.dequeue(); • String x = q.peek(); • q.enqueue("neighbor"); • String y = q.dequeue(); • String z = q.dequeue(); • What is the value of z? • "Sweden" • "is" • "my" • "neighbor" • None of the above

  14. 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

  15. 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!

  16. 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> _next public 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.

  17. 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

  18. "syntax"

  19. Topic 3: Are you on this list? Bidhan Adhikari Devin Delaney Erming Gao Christopher Lawrence Jeffrey Metter Jonathan Rarey

  20. 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(); }

  21. 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));

  22. 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

  23. 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; } }

More Related