13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

Slides:



Advertisements
Similar presentations
1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5.
Advertisements

1 - Recursion on linked lists Lecture 7 ADS2 Lecture 7.
Stacks, Queues, and Linked Lists
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Building a Linked List in Java. Linked List In the Procedural Paradigm a linked list consisted of: –A pointer to the head of the list –Nodes (in dynamic.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
Stacks with Dynamic Memory Class Signature Class Node {public Data data; public Node next; } Class Stack {private Node top; public Stack(); public void.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Queues.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
TCSS 342, Winter 2005 Lecture Notes
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
OOP Languages: Java vs C++
Data Structures - Queues
Programming Languages and Paradigms Object-Oriented Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
CS 2430 Day 26. Announcements Exam #2: Wednesday, April 3 –Review in lab on Tuesday, April 2 –Sample problems sent via .
Stacks with Dynamic Memory Class Signature Class Node {public Data data; public Node next; } Class Stack {private Node top; public Stack(); public void.
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
Linked Structures See Section 3.2 of the text.. First, notice that Java allows classes to be recursive, in the sense that a class can have an element.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Announcements Review problems Review Outline. Online Survey
QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.
1 COMP313A Programming Languages Object Oriented Progamming Languages (2)
Introduction to Data Structures and Algorithms
CS 2430 Day 24. Announcements Quiz this Friday Program 5 posted on Monday Program 4 due date: Friday at 10pm Program 4 grace date: Wednesday at 10pm (don’t.
CS 367 Introduction to Data Structures Lecture 5.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
Linked Lists CS 367 – Introduction to Data Structures.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Linked Data Structures
Week 4 - Friday CS221.
CS230 Tutorial Week 3.
Stack and Queue APURBO DATTA.
Continuing Chapter 11 Inheritance and Polymorphism
8-1.
8-1.
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Java Programming Language
Lesson Objectives Aims
Initializing Objects.
Linked Lists [AJ 15].
Stacks with Dynamic Memory
ADT list.
Queues CSC212.
Stacks and Queues.
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11

Self-Referential? Simply means that a class has a reference to an object of that class Common applications –Linked list nodes –Binary tree nodes

13 X 11 Linked Lists Nodes in Java Amazingly similar to a cons cell Stripped down version: class Node { Object data; Node next; }

13 X 11 data next Node ref data next data next Some Object A Node Object

13 X 11 More useful? class Node { public Object data; public Node next; public Node(Object data) { this(data, null); } public Node(Object data, Node next) { this.data = data; this.next = next; }

13 X 11 Node (continued) public String toString() { return "Node: " + data; }

13 X 11 Pop Quiz public String toString() { return "Node: " + data+ " Next:\n" + next; } What does it do?

13 X 11 Test Main public static void main(String args[]) { Node n1 = new Node("Hello "); Node n2 = new Node("World!")' n1.next = n2; System.out.println("Test1\n" + n1); System.out.println("Test2\n" + n2); Node nz = new Node("and Nod."); Node ny = new Node("Blynken ", nz); Node nx = new Node("Wynken, ", ny); System.out.println("Test3\n" + nx); ny = null; nz = null; System.out.println("Test4\n" + nx);

13 X 11 Test Main Output Test1 Node: Hello Next: Node: World! Next: null Test2 Node: World! Next: null Test3 Node: Wynken, Next: Node: Blynken Next: Node: and Nod. Next: null Test4 Node: Wynken, Next: Node: Blynken Next: Node: and Nod. Next: null

13 X 11 Test Main Node head = new Node("Bob,", new Node("Carol, ", new Node("Ted, ", new Node("and Alice.")))); System.out.println("Test5\n" + head); Scheme like construction

13 X 11 Test Main Output Test5 Node: Bob, Next: Node: Carol, Next: Node: Ted, Next: Node: and Alice. Next: null

13 X 11 Test Main Node list = new Node("Larry,", new Node(null, new Node("Moe, ", new Node("and Curly.")))); System.out.println("Test6\n" + list); } // main } // Node Another Scheme like construction

13 X 11 Test Main Output Test6 Node: Larry, Next: Node: *null* Next: Node: Moe, Next: Node: and Curly. Next: null

13 X 11 Questions?

13 X 11

So What's a Queue? First-In First-Out Data Structure British word for line (Queue up for a pint.) French word for tail (Like a horse's tail). Multiple ways to implement –Common to use Linked list –etc. Typical behaviors –isEmpty –enqueue –dequeue –head (or front or top or peek)

13 X 11 Linked List Implementation Can use our Node class Will need another class with a catchy name like Queue What's in the Queue class? –head pointer (reference) –tail pointer (reference) –Note: If head == null then tail == null (and vice versa) and the Queue isEmpty! –implementation of behaviors

13 X 11 isEmpty Returns a boolean Something like: return (head == null);

13 X 11 Let's write some code! class Queue { private Node head; private Node tail; public Queue() { head = null; tail = null; } public boolean isEmpty() { return (head == null); }

13 X 11 Enqueue(Object newData) Case: isEmpty() Create a new Node –data points to newData Make head and tail point to the new Node Case: ! isEmpty() Create a new Node –data points to newData Make the old tail Node point to the new Node Make the tail pointer point to the new Node

13 X 11 Enqueue(Object newData) Create a new Node –data points to newData Case: isEmpty(); Make head and tail point to the new Node Create a new Node –data points to newData Case: ! isEmpty() Make the old tail Node point to the new Node Make the tail pointer point to the new Node

13 X 11 Enqueue(newData): isEmpty() head tail Queue Object

13 X 11 Nota Bene In the case of a Queue we will always make new Nodes with the next reference set equal to null

13 X 11 Enqueue(newData): isEmpty() head tail Queue Object data next newData temp

13 X 11 Enqueue(newData): isEmpty() head tail Queue Object data next newData temp

13 X 11 Enqueue(newData): isEmpty() head tail Queue Object data next newData temp

13 X 11 Enqueue(newData): isEmpty() head tail Queue Object data next newData

13 X 11 Questions?

13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next

13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next newData temp

13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next newData temp

13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next newData temp

13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next newData

13 X 11 Questions?

13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next

13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next temp data next newData

13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next temp data next newData

13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next temp data next newData

13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next data next newData

13 X 11 Questions?

13 X 11 Let's write some more code! public void enqueue(Object o) { Node temp; temp = new Node(o); if(isEmpty()) { head = temp; tail = temp; } else // Queue is not empty... { tail.next = temp; tail = temp; }

13 X 11 Almost done! Now Dequeue Assume ! isEmpty –we'll check Save value that head data reference is pointing to –(return value) Make head pointer point to whatever first node's next is pointing to... Case: head is not null Assume ! isEmpty –we'll check Save value that head data reference is pointing to –(return value) Make head pointer point to whatever first node's next is pointing to... Case: head is null Set tail to null

13 X 11 Red Dequeue() head tail Queue Object data next data next Green data next Blue

13 X 11 Red Dequeue() head tail Queue Object data next data next Green data next Blue retval

13 X 11 Red Dequeue() head tail Queue Object data next data next Green data next Blue retval

13 X 11 Red Dequeue() head tail Queue Object data next Green data next Blue retval

13 X 11 Red Dequeue() head tail Queue Object data next Green data next Blue retval Return

13 X 11 Dequeue() head tail Queue Object data next Green data next Blue

13 X 11 Questions?

13 X 11 Dequeue() head tail Queue Object data next Green data next Blue

13 X 11 Dequeue() head tail Queue Object data next Green data next Blue retval

13 X 11 Dequeue() head tail Queue Object data next Green data next Blue retval

13 X 11 Dequeue() head tail Queue Object Green data next Blue retval Return

13 X 11 Dequeue() head tail Queue Object data next Blue

13 X 11 Questions?

13 X 11 Dequeue() head tail Queue Object data next Blue

13 X 11 Dequeue() head tail Queue Object data next Blue retval

13 X 11 Dequeue() head tail Queue Object data next Blue retval

13 X 11 Dequeue() head tail Queue Object data next Blue retval isEmpty???

13 X 11 Dequeue() head tail Queue Object data next Blue retval

13 X 11 Dequeue() head tail Queue Object Blue retval

13 X 11 Dequeue() head tail Queue Object Blue retval Return

13 X 11 Dequeue() head tail Queue Object

13 X 11 Dequeue Code public Object dequeue() { Object retval; if(isEmpty()) { retval = null; } else { retval = head.data; head = head.next; if(isEmpty()) { tail = null; } return retval; }

13 X 11 Test Main public static void main(String args[]) { Queue q; q = new Queue(); q.enqueue("yada1"); q.enqueue("yada2"); q.enqueue("yada3"); while(! q.isEmpty()) { System.out.println(q.dequeue()); } } // main } // Queue

13 X 11 Questions?

13 X 11 Over break... Rewrite class Node with –private head and tail –getHead and setHead methods –getNext and setNext methods Rewrite class Queue using your new Node Build a working scale model of the Three-Mile Island Power Plant Have a Merry Christmas!

13 X 11 What you should know about now Syntax –Operators –Operator overloading –Assignment statements –Control structures if else case –Iterative structures while do while for Data Types –Primitives –References class attribute –access modifiers public/private –static –final/constants –initialization

13 X 11 What you should know about now constructors –access modifiers –default –chaining –overloading methods –access modifiers public/private –static –return type/void –main method –accessors –modifiers –overloading

13 X 11 What you should know about now object/instance Inheritance –Redefinition (Overriding) –Extension –super class –subclass –abstract Polymorphism Compilation –reference type checking –method checking –Type mismatch checking Run Time –interpreting –dynamic binding –Java virtual machine

13 X 11