Announcements. FINAL EXAM Monday May 21, 1:30pm Review Session. Wednesday May 16, 7-9pm

Similar documents
1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

CmpSci 187: Programming with Data Structures Spring 2015

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Chapter 3: Restricted Structures Page 1

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

D06 PROGRAMMING with JAVA

Universidad Carlos III de Madrid

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Common Data Structures

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Data Structures Using C++ 2E. Chapter 5 Linked Lists

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Data Structures and Algorithms Stacks and Queues

Data Structures in the Java API

DATA STRUCTURES USING C

LINKED DATA STRUCTURES

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

STACKS,QUEUES, AND LINKED LISTS

Algorithms and Data Structures

Analysis of a Search Algorithm

DATA STRUCTURE - QUEUE

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Introduction to Data Structures

Project 4 DB A Simple database program

Programming with Data Structures

CompSci-61B, Data Structures Final Exam

Algorithms and Data Structures Written Exam Proposed SOLUTION

DATA STRUCTURE - STACK

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

HW3: Programming with stacks

10CS35: Data Structures Using C

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

7.1 Our Current Model

Linked Lists Linked Lists, Queues, and Stacks

Stacks. Linear data structures

Data Structure [Question Bank]

Practical Session 4 Java Collections

Module 2 Stacks and Queues: Abstract Data Types

PES Institute of Technology-BSC QUESTION BANK

COMPUTER SCIENCE. Paper 1 (THEORY)

Introduction to Programming System Design. CSCI 455x (4 Units)

Stacks. Data Structures and Data Types. Collections

Sequential Data Structures

Introduction to Stacks

St S a t ck a ck nd Qu Q eue 1

Data Structures and Data Manipulation

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

14 Stacks, Queues, And Linked Lists

Stack Allocation. Run-Time Data Structures. Static Structures

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Data Structures In Java

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Ordered Lists and Binary Trees

Data Structures. Level 6 C Module Descriptor

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Node-Based Structures Linked Lists: Implementation

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Software Development with UML and Java 2 SDJ I2, Spring 2010

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Lecture 12 Doubly Linked Lists (with Recursion)

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

CS 111 Classes I 1. Software Organization View to this point:

Datastrukturer och standardalgoritmer i Java

Big O and Limits Abstract Data Types Data Structure Grand Tour.

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Cpt S 223. School of EECS, WSU

Data Structures and Algorithms Lists

Analysis of Micromouse Maze Solving Algorithms

Collaboration policy. Where to get help (but no code in ) Office hours Lab TAs in Friend 008/009 Bounce ideas (but not code) off classmates

Computer Programming I

Monitors, Java, Threads and Processes

TESTING WITH JUNIT. Lab 3 : Testing

Collections in Java. Arrays. Iterators. Collections (also called containers) Has special language support. Iterator (i) Collection (i) Set (i),

Data Structures Using C++

Recursion vs. Iteration Eliminating Recursion

System Software Prof. Dr. H. Mössenböck

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Data Structures and Algorithms

Chapter 8: Bags and Sets

PLV Goldstein 315, Tuesdays and Thursdays, 6:00PM-7:50PM. Tuesdays and Thursdays, 4:00PM-5:30PM and 7:50PM 9:30PM at PLV G320

C++ INTERVIEW QUESTIONS

Introduction to Data Structures and Algorithms

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Transcription:

Announcements FINAL EXAM Monday May 21, 1:30pm Review Session Wednesday May 16, 7-9pm 1

Recitation 12 Root Finding, Sorting, Stacks, Queues 2

Outline Linked Lists Sorting Queues 3

Object References Let s start with a class called Node Node.java Node.java class Node{ To create an instance of Node: Node n = new Node(); Node n2 = n; n2 n (Node object) These are object references. Notice how there is only ONE object, and each reference refers to it. 4

Fun with References Let s add to our Node class Node.java Node.java class Node{ Node next; Now, let s use the following code: Node n1 = new Node(); Node n2 = new Node(); n2.next = n1; n1.next = n2; n1 next n2 next 5

Serious Work with References Let s add a data member to store some information. (It could be a primitive type or an object reference or anything else you want) Node.java Node.java class Node{ Node next; int value; Now, let s use the following code: Node first = new Node(); first.value=1; first.next = new Node(); first.next.value=2; first.next.next = new Node(); first.next.next.value = 3; It s a Linked List! first next value 1 next value 2 next value 3 6

Linked Lists A linked list is made of a series of Nodes, each with: an associated item object a reference to the next node of the list Simplified double-box picture: next next next item item item A B C Traverse the list by following each node s next reference 7

SLinkedList For convenience, create a class with references to the first & last nodes, with methods, so we don t have to rewrite the manipulation code each time. public class SLinkedList implements List{ private int length = 0; private Node first = null; private Node last = null; Our original Node class private static class Node { Object item; Node next; Node( Object o, Node n ){ item = o; next = n; public int size() {/*code */ public boolean isempty() {/*code */ public boolean contains(object o) {/*code */ public void clear() {/*code */ // various add() and remove() methods 8

What s so great about Linked Lists? BIG Pros: A Linked List can grow dynamically. (To resize an array you have to create a new, larger array, and copy everything over) A linked list does NOT need contiguous memory. (A Java 1-D array has to occupy contiguous memory. When storing large amounts of data, finding back-to-back-to-back memory can be impossible) Cons: The references add overhead. Access is slower than an array. The code to maintain a Linked List can be complex. Depending on how the links (references) are structured, you may only be able to traverse one way 9

Linked List: Tips Always think of special cases What if your list is empty? What if there is only one element? Always draw a diagram! first last next next next 10

Sorting Sortable objects implement Comparable<Object> or have Comparator defined Comparable: Define compareto() For object.compareto(other): returns 1 if other higher ranked than object returns 0 if equally ranked returns -1 otherwise 11

Sorting Comparator: New class Object1Object2Comparator Implements Comparator<Object> Must define compare(). For compare(a, b): returns 1 if b higher ranked than a return 0 if equally ranked returns -1 otherwise 12

Sorting Exercise Sort restaurants by rating (high to low) then distance (close to far) public class Restaurant { String name; int rating; double distance; public Restaurant(String n, int r, double d){ name = n; rating = r; distance = d; public String tostring(){ return name + ": " + rating + "/5.0, " + distance + " meters away."; 13

Stacks and Queues Structures store, manage data For data with an inherent order Think of structures like a line to get into a movie Stacks: people are added and removed from same end of line Last person in an elevator is the first person out of the elevator FIFO Queue: people added to back of line, removed from front First In First Out, the way you expect a ticket line to work 14

Stacks Single end LIFO: Last In First Out push(): add an element pop(): remove top element Applications: Simulation: robots, machines Recursion: pending function calls Reversal of data 15

Stack Interface import java.util.*; // For exception public interface Stack { public boolean isempty(); public void push( Object o ); public Object pop() throws EmptyStackException; public void clear(); 16

Queues Two ends FIFO: First In First Out push(): add an element to top pop(): remove bottom element Applications: Simulation: lines Ordered requests: device drivers, routers, Searches 17

Queue Interface import java.util.*; public interface Queue { public boolean isempty(); public void add( Object o ); public Object remove() throws NoSuchElementException; public void clear(); 18

Exercise What is the final output? Add {2, 4, 6, 8 to a stack #1 Remove three items from stack, place in queue Remove two items from queue, place in stack #2 Remove one item from stack #2, place in queue Remove one item from stack #1, place in stack #2 Queue: {6, 4 Stack #2: {2, 8 19

Exercise Write a class to store a queue in a linked list What happens when you remove the last object? What happens when you try to remove an object from an empty list? public interface Queue { public void enqueue(int item); // add to end public int dequeue() throws Exception; // remove from front 20

MIT OpenCourseWare http://ocw.mit.edu 1.00 / 1.001 / 1.002 Introduction to Computers and Engineering Problem Solving Spring 2012 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.