Linked Structures - Review Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Size: px
Start display at page:

Download "Linked Structures - Review Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013"

Transcription

1 Linked Structures - Review Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

2 2 Scope Introduction to Linked Structures: Object references as links Linked vs. array-based structures Managing linked lists Linked implementation of a stack Wk14.1 Slide 2

3 3 Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references to create links between objects Recall that an object reference variable holds the address of an object Wk14.1 Slide 3

4 4 Linked Lists A Person object could contain a reference to another Person public class Person { private String name; private String addr; private Person next; // Link to Another Person object A series of Person objects could make up a linked list: Wk14.1 Slide 4

5 5 Linked Non-Linear Structures Links could also be used to form more complicated, non-linear structures This is called a graph Wk14.1 Slide 5

6 6 Linked Lists There are no index values built into linked lists To access each node in the list you must follow the references from one node to the next Person current = firstperson; while (current!= null) { System.out.println(current); current = current.next; Wk14.1 Slide 6

7 7 Linked Lists Inserting a node in the Middle 1. Set the next member in obj to refer to the next object in the list 2. Set the next member of the previous object to refer to the new object obj 1 2 prev x next Wk14.1 Slide 7

8 8 Linked Lists Inserting a node at the front Care must be taken to maintain the integrity of the links To insert a node at the front of the list, first point the new node to the front node, then reassign the front reference Wk14.1 Slide 8

9 9 Linked Lists Deleting the First Node To delete the first node, reassign the front reference accordingly If the deleted node is needed elsewhere, a reference to it must be established before reassigning the front pointer Wk14.1 Slide 9

10 10 Put Linked List Details into separate Node Class So far we've assumed that the list contains nodes that are self-referential (Person points to a Person) But often we'll want to make lists of objects that don't contain such references Solution: have a separate Node class that forms the list and holds a reference to the objects being stored Node Node Node Node Node Node Person Person Person Person Person Person Wk14.1 Slide 10

11 Doubly Linked Lists 11 There are many variations on the basic linked list concept For example, we could create a doubly-linked list with next and previous references in each node and a separate pointer to the rear of the list next Node Node Node Node Node Node previous Person Person Person Person Person Person Wk14.1 Slide 11

12 12 Implementing a Stack using Links Let's implement a stack using a linked list to hold the elements Our LinkedStack<T> class stores a generic type T and implements the same StackADT<T> interface used previously A separate LinearNode<T> class forms the list and hold a reference to the element stored An integer count will store how many elements are currently in the stack Wk14.1 Slide 12

13 13 LinkedStack<T> public class LinkedStack<T> implements StackADT<T> { private int count; private LinearNode<T> top; /** * Creates an empty stack. LinkedStack<T> */ public LinkedStack() { count = 0; top = null; LinearNode<T> top int count Linear Node<T> Wk14.1 Slide 13

14 14 LinearNode<T> public class LinearNode<T> { private LinearNode<T> next; private T element; LinearNode<t> top public LinearNode() { next = null; element = null; public LinearNode(T elem) { next = null; element = elem; public LinearNode<T> getnext() { return next; LinearNode<t> next T element Object of Class T Linear Node<T> public void setnext(linearnode<t> node) { next = node; public T getelement() { return element; public void setelement(t elem) { element = elem; Wk14.1 Slide 14

15 15 Implementing a Stack using Links Since all activity on a stack happens on one end, a single reference to the front of the list will represent the top of the stack Linear Node Linear Node Linear Node Linear Node Linear Node Linear Node T T T T T T Wk14.1 Slide 15

16 16 Implementing a Stack using Links The stack after A, B, C, and D are pushed, in that order: Wk14.1 Slide 16

17 17 Implementing a Stack using Links After E is pushed onto the stack: Wk14.1 Slide 17

18 Implementing a Stack using Links 18 package jsjf; /** * Represents a node in a linked list. * Java Foundations 4.0 */ public class LinearNode<T> { private LinearNode<T> next; private T element; /** * Creates an empty node. */ public LinearNode() { next = null; element = null; /** * Creates a node storing the specified element. elem element to be stored */ public LinearNode(T elem) { next = null; element = elem; Wk14.1 Slide 18

19 19 Implementing a Stack using Links /** * Returns the node that follows this one. reference to next node */ public LinearNode<T> getnext() { return next; /** * Sets the node that follows this one. node node to follow this one */ public void setnext(linearnode<t> node) { next = node; /** * Returns the element stored in this node. element stored at the node */ public T getelement() { return element; /** * Sets the element stored in this node. elem element to be stored at this node */ public void setelement(t elem) { element = elem; Wk14.1 Slide 19

20 20 Implementing a Stack using Links package jsjf; import jsjf.exceptions.*; import java.util.iterator; /** * Represents a linked implementation of a stack. * Java Foundations 4.0 */ public class LinkedStack<T> implements StackADT<T> { private int count; private LinearNode<T> top; /** * Creates an empty stack. */ public LinkedStack() { count = 0; top = null; Wk14.1 Slide 20

21 21 Implementing a Stack using Links /** * Adds the specified element to the top of this stack. element element to be pushed on stack */ public void push(t element) { LinearNode<T> temp = new LinearNode<T>(element); temp.setnext(top); top = temp; count++; /** * Removes the element at the top of this stack and returns a * reference to it. element from top of stack EmptyCollectionException if the stack is empty */ public T pop() throws EmptyCollectionException { if (isempty()) throw new EmptyCollectionException("stack"); T result = top.getelement(); top = top.getnext(); count--; return result; Wk14.1 Slide 21

22 22 Implementing a Stack using Links Wk14.1 Slide 22

23 23 Key Things to take away: Linked Objects: Object Reference variables can be used to create linked structures A Linked List is composed on objects that each point to the next in the list Objects stored in a collection should not contain any implementation details of the underlying data structure that The order in which references are changed are very important Dealing with the first node in the list often requires special handling A Linked List implementation of a Stack adds elements to, or removes elements from, one end of the linked list. Queues, Trees, and other structures can be created with Linked Objects Wk14.1 Slide 23

24 24 Practice Array Questions Problem 1: Write a program that shuffles a deck of cards, then deals out 5 cards each to two players. Problem 2: Create a method that accepts two 2-dimensional arrays A and B as formal parameters and returns the matrix product A*B. Problem 3: What is wrong with Bob s Array code? Problem 4: Write a method called sumarray that accepts an array of floating point values and returns the sum of the values stored in the array Problem 5: Write a method called sum2darray that accepts an int[][] array and sums all the numbers in the array and returns the sum of the values stored in the array. Wk14.1 Slide 24

25 25 Problem 1: Write a program to shuffle and deal cards Step 1: Create the Shuffled Deck using a Stack for(int i=0; i<card.numsuits*card.numvalues; i++) { boolean foundnextcard = false; int tries = 0; while (!foundnextcard) { int cardvalue = valuegen.nextint(card.numvalues); int cardsuit = suitgen.nextint(card.numsuits ); tries++; if (carddealt[cardsuit][cardvalue] == false) { Card nextcard = new Card(cardValue+1, cardsuit); deckofcards.push(nextcard); carddealt[cardsuit][cardvalue] = true; foundnextcard = true; System.out.println("Card "+(i+1)+" : "+ nextcard+" found after "+tries+" tries"); Wk14.1 Slide 25

26 26 Problem 1: Write a program to shuffle and deal cards Step 2: Deal five cards to each player for(int p=0; p<numplayers; p++) for(int c=0; c<numcardsperhand; c++) hand[p][c] = deckofcards.pop(); Wk14.1 Slide 26

27 27 Problem 1: Write a program to shuffle and deal cards Step 3: Display both player s hands for(int p=0; p<numplayers; p++) { System.out.println("\nPlayer "+p+" hand:"); for(int c=0; c<numcardsperhand; c++) { System.out.println( "Card " + c + ": "+hand[p][c]); Wk14.1 Slide 27

28 28 Problem 1: Write a program to shuffle and deal cards Discussion: How does a Card get converted to a String? System.out.println("Card "+c+": "+hand[p][c]); Using the tostring method from the Card class! public String tostring() { if (value == 0) return "Joker"; else return cardnames[value]+" of "+suit; enum Suit {Hearts, Diamonds, Clubs, Spades; private final static String[] cardnames = {"Joker","Ace", "Two", "Three","Four","Five", "Six", "Seven","Eight","Nine","Ten", "Jack","Queen","King"; Wk14.1 Slide 28

29 29 Problem 2: Method to return the matrix product A*B. public static int[][] matrixmultiply(int[][] A, int [][] B) { int arows = A.length; int acols = A[0].length; int brows = B.length; int bcols = B[0].length; int N = A.length; // Assume A and B are both NxN if (acols!= brows) throw new IllegalArgumentException( "A Columns: " + acols + " did not match number of B Rows " + brows); int[][] C = new int[arows][bcols]; for (int i = 0; i < arows; i++) // A Row for (int j = 0; j < bcols; j++) // B Column for (int k = 0; k < acols; k++) // A Column C[i][j] += A[i][k] * B[k][j]; return C; Wk14.1 Slide 29

30 30 And the Winner for best TicTacToe GUI is Curtis Babnik Human vs Smarter: Human vs Learns : Wk14.1 Slide 30

31 31 TicTacToe UML A UML Diagram using a Eclipse UML Plugin: Wk14.1 Slide 31

32 32 References: 1. J. Lewis, P. DePasquale, and J. Chase., Java Foundations: Introduction to Program Design & Data Structures. Addison-Wesley, Boston, Massachusetts, 3rd edition, 2014, ISBN Wk14.1 Slide 32

Linked Structures Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Linked Structures Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Linked Structures Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Introduction to Linked Structures: Object references as links Linked vs. array-based structures Managing

More information

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011 CMPSCI 187: Programming With Data Structures Lecture 12: Implementing Stacks With Linked Lists 5 October 2011 Implementing Stacks With Linked Lists Overview: The LinkedStack Class from L&C The Fields and

More information

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Collections Chapter 12 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Introduction to Collections: Collection terminology The Java Collections API Abstract nature of collections

More information

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Introduction to Recursion: The concept of recursion Recursive methods Infinite recursion When to use (and

More information

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Trees Chapter 19, 20 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Trees: Trees as data structures Tree terminology Tree implementations Analyzing tree efficiency Tree traversals

More information

Graphs Chapter 24. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Graphs Chapter 24. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Graphs Chapter 24 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Graphs: Directed and undirected graphs Weighted graphs (networks) Common graph algorithms Wk12.5 Slide 2 3 Graphs

More information

Bonus Puzzle #1 Random Doubles [0,5] Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Bonus Puzzle #1 Random Doubles [0,5] Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Bonus Puzzle #1 Random Doubles [0,5] Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Bonus Puzzle #1 Write an expression to calculate double random numbers from [0, 5.000000000] inclusive

More information

Analysis of Algorithms Chapter 11. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Analysis of Algorithms Chapter 11. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Analysis of Algorithms Chapter 11 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Analysis of Algorithms: Efficiency goals The concept of algorithm analysis Big-Oh notation The

More information

Announcements Queues. Queues

Announcements Queues. Queues Announcements. Queues Queues A queue is consistent with the general concept of a waiting line to buy movie tickets a request to print a document crawling the web to retrieve documents A queue is a linear

More information

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6 Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Creating

More information

Introduction to Computing II (ITI 1121) Final Examination

Introduction to Computing II (ITI 1121) Final Examination Université d Ottawa Faculté de génie École de science informatique et de génie électrique University of Ottawa Faculty of Engineering School of Electrical Engineering and Computer Science Introduction

More information

A queue is a linear collection whose elements are added on one end and removed from the other

A queue is a linear collection whose elements are added on one end and removed from the other A queue is consistent with the general concept of a waiting line to buy movie tickets a request to print a document crawling the web to retrieve documents Adding an element Removing an element A queue

More information

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Queues CSE 2011 Fall 2009 9/28/2009 7:56 AM 1 Queues: FIFO Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Applications,

More information

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Inheritance Chapter 8 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Class Inheritance: Deriving classes Method overriding Class hierarchies Abstract classes Visibility and inheritance

More information

Searching and Sorting Chapter 18. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Searching and Sorting Chapter 18. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Searching and Sorting Chapter 18 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Scope 2 Searching and Sorting: Linear search and binary search algorithms Several sorting algorithms,

More information

Queue rear tail front head FIFO

Queue rear tail front head FIFO The Queue ADT Objectives Examine queue processing Define a queue abstract data type Demonstrate how a queue can be used to solve problems Examine various queue implementations Compare queue implementations

More information

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Things to Review Review the Class Slides: Key Things to Take Away Do you understand

More information

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

More information

LECTURE OBJECTIVES 6-2

LECTURE OBJECTIVES 6-2 The Queue ADT LECTURE OBJECTIVES Examine queue processing Define a queue abstract data type Demonstrate how a queue can be used to solve problems Examine various queue implementations Compare queue implementations

More information

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University (6-1) Basics of a Queue Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University What is a Queue? 2 A linear data structure with a finite sequence of nodes, where nodes

More information

Introduction to Computer Science II (ITI 1121) Midterm Examination

Introduction to Computer Science II (ITI 1121) Midterm Examination Introduction to Computer Science II (ITI 1121) Midterm Examination Instructor: Marcel Turcotte February 2008, duration: 2 hours Identification Student name: Student number: Signature: Instructions 1. 2.

More information

Exceptions Chapter 10. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Exceptions Chapter 10. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Exceptions Chapter 10 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Exceptions: The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception

More information

Introduction to Computer Science II (ITI 1121) Final Examination

Introduction to Computer Science II (ITI 1121) Final Examination Université d Ottawa Faculté de génie École d ingénierie et de technologie de l information University of Ottawa Faculty of Engineering School of Information Technology and Engineering Introduction to Computer

More information

Lists. ordered lists unordered lists indexed lists 9-3

Lists. ordered lists unordered lists indexed lists 9-3 The List ADT Objectives Examine list processing and various ordering techniques Define a list abstract data type Examine various list implementations Compare list implementations 9-2 Lists A list is a

More information

CSE 143 SAMPLE MIDTERM

CSE 143 SAMPLE MIDTERM CSE 143 SAMPLE MIDTERM 1. (5 points) In some methods, you wrote code to check if a certain precondition was held. If the precondition did not hold, then you threw an exception. This leads to robust code

More information

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Introduce the Java programming

More information

Midterm Solutions. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Midterm Solutions. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Midterm Solutions Instructor: Scott Kristjanson CMP 125/125 SU Burnaby, all 2013 2 Question 1 1 Mark if checked something that should be checked 1 Mark if left unchecked, something that should not be checked

More information

Arrays Chapter 7. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Arrays Chapter 7. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Arrays Chapter 7 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Arrays: Array declaration and use Bounds checking Arrays as objects Arrays of objects Command-line arguments Variable-length

More information

CS350: Data Structures Stacks

CS350: Data Structures Stacks Stacks James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Stacks Stacks are a very common data structure that can be used for a variety of data storage

More information

CMSC 132, Object-Oriented Programming II Summer Lecture 9:

CMSC 132, Object-Oriented Programming II Summer Lecture 9: CMSC 132, Object-Oriented Programming II Summer 2018 Lecturer: Anwar Mamat Lecture 9: Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 9.1 QUEUE

More information

ADTs, Arrays, Linked Lists

ADTs, Arrays, Linked Lists 1 ADTs, Arrays, Linked Lists Outline and Required Reading: ADTs ( 2.1) Using Arrays ( 3.1) Linked Lists ( 3.2, 3.3, 3.4) CSE 2011, Winter 2017 Instructor: N. Vlajic Data Type 2 A data type is a classification

More information

Introduction to Computer Science II (CSI 1101) Final Examination

Introduction to Computer Science II (CSI 1101) Final Examination Introduction to Computer Science II (CSI 1101) Final Examination Instructor: Marcel Turcotte April 2005, duration: 3 hours Identification Student name (last, first): designated seat: Signature: student

More information

CSIS 10B Lab 2 Bags and Stacks

CSIS 10B Lab 2 Bags and Stacks CSIS 10B Lab 2 Bags and Stacks Part A Bags and Inheritance In this part of the lab we will be exploring the use of the Bag ADT to manage quantities of data of a certain generic type (listed as T in the

More information

CE204 Data Structures and Algorithms Part 2

CE204 Data Structures and Algorithms Part 2 CE204 Data Structures and Algorithms Part 2 14/01/2018 CE204 Part 2 1 Abstract Data Types 1 An abstract data type is a type that may be specified completely without the use of any programming language.

More information

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request. University of Toronto CSC148 Introduction to Computer Science Fall 2001 Mid Term Test Section L5101 Duration: 50 minutes Aids allowed: none Make sure that your examination booklet has 8 pages (including

More information

(3-2) Basics of a Stack. Instructor - Andrew S. O Fallon CptS 122 (January 26, 2018) Washington State University

(3-2) Basics of a Stack. Instructor - Andrew S. O Fallon CptS 122 (January 26, 2018) Washington State University (3-2) Basics of a Stack Instructor - Andrew S. O Fallon CptS 122 (January 26, 2018) Washington State University What is a Stack? A finite sequence of nodes, where only the top node may be accessed Insertions

More information

CS 101 Spring 2006 Final Exam Name: ID:

CS 101 Spring 2006 Final Exam Name:  ID: This exam is open text book but closed-notes, closed-calculator, closed-neighbor, etc. Unlike the midterm exams, you have a full 3 hours to work on this exam. Please sign the honor pledge here: Page 1

More information

CMSC 132: Object-Oriented Programming II. Stack and Queue

CMSC 132: Object-Oriented Programming II. Stack and Queue CMSC 132: Object-Oriented Programming II Stack and Queue 1 Stack Allows access to only the last item inserted. An item is inserted or removed from the stack from one end called the top of the stack. This

More information

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12 CS 151 Linked Lists, Recursively Implemented 1 2 Linked Lists, Revisited Recall that a linked list is a structure that represents a sequence of elements that are stored non-contiguously in memory. We can

More information

CSCD 326 Data Structures I Stacks

CSCD 326 Data Structures I Stacks CSCD 326 Data Structures I Stacks 1 Stack Interface public interface StackInterface { public boolean isempty(); // Determines whether the stack is empty. // Precondition: None. // Postcondition: Returns

More information

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

CS132 Algorithm. Instructor: Jialiang Lu   Office: Information Center 703 CS132 Algorithm Instructor: Jialiang Lu Email: jialiang.lu@sjtu.edu.cn Office: Information Center 703 Chapter 3 STRUCTURES IN C 2 Structures Introduction Collections of related variables (aggregates) under

More information

Stacks Fall 2018 Margaret Reid-Miller

Stacks Fall 2018 Margaret Reid-Miller Stacks 15-121 Fall 2018 Margaret Reid-Miller Today Exam 2 is next Tuesday, October 30 Today: Quiz 5 solutions Recursive add from last week (see SinglyLinkedListR.java) Stacks ADT (Queues on Thursday) ArrayStack

More information

CS 307 Final Fall 2009

CS 307 Final Fall 2009 Points off 1 2 3 4 5 6 Total off Net Score CS 307 Final Fall 2009 Name UTEID login name Instructions: 1. Please turn off your cell phones. 2. There are 6 questions on this test. 3. You have 3 hours to

More information

Computer Programming, I. Laboratory Manual. Experiment #9. Multi-Dimensional Arrays

Computer Programming, I. Laboratory Manual. Experiment #9. Multi-Dimensional Arrays Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #9

More information

Basic Data Structures

Basic Data Structures Basic Data Structures Some Java Preliminaries Generics (aka parametrized types) is a Java mechanism that enables the implementation of collection ADTs that can store any type of data Stack s1

More information

ECE Fall st Midterm Examination (120 Minutes, closed book)

ECE Fall st Midterm Examination (120 Minutes, closed book) ECE Fall 2009 1 st Midterm Examination (120 Minutes, closed book) Name: Question Score Student ID: 1 (15) 2 (20) 3 (20) 4 (15) 5 (20) 6 (10) NOTE: Any questions on writing code must be answered in Java

More information

CSE 143. Lecture 7: Linked List Basics reading: 16.2

CSE 143. Lecture 7: Linked List Basics reading: 16.2 CSE 143 Lecture 7: Linked List Basics reading: 16.2 References vs. objects variable = value; a variable (left side of = ) is an arrow (the base of an arrow) a value (right side of = ) is an object (a box;

More information

Has-a Relationships. A pen has a or contains a ball

Has-a Relationships. A pen has a or contains a ball Has-a Relationships A pen has a or contains a ball Has-a Relationships Has-a relationship Also called containment Cannot be implemented using inheritance Example: To implement the has-a relationship between

More information

Basic Data Structures 1 / 24

Basic Data Structures 1 / 24 Basic Data Structures 1 / 24 Outline 1 Some Java Preliminaries 2 Linked Lists 3 Bags 4 Queues 5 Stacks 6 Performance Characteristics 2 / 24 Some Java Preliminaries Generics (aka parametrized types) is

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University 9/5/6 CS Introduction to Computing II Wayne Snyder Department Boston University Today: Arrays (D and D) Methods Program structure Fields vs local variables Next time: Program structure continued: Classes

More information

COE 312 Data Structures. Welcome to Exam II Monday November 23, Instructor: Dr. Wissam F. Fawaz

COE 312 Data Structures. Welcome to Exam II Monday November 23, Instructor: Dr. Wissam F. Fawaz 1 COE 312 Data Structures Welcome to Exam II Monday November 23, 2016 Instructor: Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam is Closed Book. Please do not forget to write your name

More information

Generics in Java. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Generics in Java. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Generics in Java EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Motivating Example: A Book of Objects 1 class Book { 2 String[] names; 3 Object[] records; 4 /* add a name-record

More information

Motivating Example: Observations (1) Generics in Java. Motivating Example: A Book of Objects. Motivating Example: Observations (2)

Motivating Example: Observations (1) Generics in Java. Motivating Example: A Book of Objects. Motivating Example: Observations (2) Motivating Example: Observations (1) Generics in Java EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG In the Book class: By declaring the attribute Object[] records We meant that

More information

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt www.introsoftwaretesting.com OO Software and Designs Emphasis on modularity and reuse puts complexity

More information

Stacks and Queues. Chapter Stacks

Stacks and Queues. Chapter Stacks Chapter 18 Stacks and Queues 18.1 Stacks The stack abstract data type allows access to only one element the one most recently added. This location is referred to as the top of the stack. Consider how a

More information

Sample file. Practice Exam One COMPUTER SCIENCE A SECTION I. Directions: Determine the answer to each of the following questions or incomplete

Sample file. Practice Exam One COMPUTER SCIENCE A SECTION I. Directions: Determine the answer to each of the following questions or incomplete Practice Exam One / Level A Diagnostic Test 5 Practice Exam One COMPUTER SCIENCE A SECTION I Time 1 hour and 15 minutes Number of questions 40 Percent of total grade 50 Directions: Determine the answer

More information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM

More information

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice: CMP-338 106 Points Total Midterm Spring 2017 Version 1 Instructions Write your name and version number on the top of the yellow paper. Answer all questions on the yellow paper. One question per page. Use

More information

Introduction to Linked Data Structures

Introduction to Linked Data Structures Introduction to Linked Data Structures A linked data structure consists of capsules of data known as nodes that are connected via links Links can be viewed as arrows and thought of as one way passages

More information

Debugging [continued]

Debugging [continued] 1 Debugging [continued] Admiral Grace Murray Hopper http://www.history.navy.mil/photos/images/h96000/h96566kc.htm 2 Debugging Your Program Debugging Your Program. [summary] 1. Edit the program (type in

More information

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011 Stacks (5.1) CSE 2011 Winter 2011 26 January 2011 1 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming (Spring 2012) Lecture #9: Arrays Zhong Shao Department of Computer Science Yale University Office: 314 Watson http://flint.cs.yale.edu/cs112 Acknowledgements: some slides

More information

Class 26: Linked Lists

Class 26: Linked Lists Introduction to Computation and Problem Solving Class 26: Linked Lists Prof. Steven R. Lerman and Dr. V. Judson Harward 2 The Java Collection Classes The java.util package contains implementations of many

More information

1.4 Arrays. A Foundation for Programming. Many Variables of the Same Type. Arrays. !!! any program you might want to write

1.4 Arrays. A Foundation for Programming. Many Variables of the Same Type. Arrays. !!! any program you might want to write A Foundation for Programming 1.4 Arrays any program you might want to write objects functions and modules graphics, sound, and image I/O arrays store and manipulate huge quantities of data conditionals

More information

COP 3330 Final Exam Review

COP 3330 Final Exam Review COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time

More information

1.4 Arrays. A Foundation for Programming. Arrays. Many Variables of the Same Type. This lecture. Store and manipulate huge quantities of data.

1.4 Arrays. A Foundation for Programming. Arrays. Many Variables of the Same Type. This lecture. Store and manipulate huge quantities of data. A Foundation for Programming 1.4 Arrays any program you might want to write objects functions and modules graphics, sound, and image I/O arrays store and manipulate huge quantities of data conditionals

More information

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018 CSC 172 Data Structures and Algorithms Lecture #9 Spring 2018 SINGLY LINKED LIST 3.1.3 Linked lists We will consider these for Singly linked lists Doubly linked lists Basic Singly Linked List class Node

More information

CSE 143 SAMPLE MIDTERM SOLUTION

CSE 143 SAMPLE MIDTERM SOLUTION CSE 143 SAMPLE MIDTERM SOLUTION 1. (5 points) In some methods, you wrote code to check if a certain precondition was held. If the precondition did not hold, then you threw an exception. This leads to robust

More information

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh Computer Science CS221 Test 2 Name 1. Give a definition of the following terms, and include a brief example. a) Big Oh b) abstract class c) overriding d) implementing an interface 10/21/1999 Page 1 of

More information

1/27/2014. OO design approach with examples. UML and project clarifications (2) Project clarifications 1. UML

1/27/2014. OO design approach with examples. UML and project clarifications (2) Project clarifications 1. UML OO design approach with examples Sharma Chakravarthy Information Technology Laboratory (IT Lab) Computer Science and Engineering Department The University of Texas at Arlington, Arlington, TX 76019 Email:

More information

Arrays. Inf1-OP. Arrays. Many Variables of the Same Type. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein

Arrays. Inf1-OP. Arrays. Many Variables of the Same Type. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein Inf1-OP Arrays 1 Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein Arrays School of Informatics February 26, 2018 1 Thanks to Sedgewick&Wayne for much of this content Arrays Arrays:

More information

Linked List. ape hen dog cat fox. tail. head. count 5

Linked List. ape hen dog cat fox. tail. head. count 5 Linked Lists Linked List L tail head count 5 ape hen dog cat fox Collection of nodes with a linear ordering Has pointers to the beginning and end nodes Each node points to the next node Final node points

More information

Inf1-OOP. Arrays. Many Variables of the Same Type. Arrays Introduction to Arrays Arrays in Java Shuffling a Deck Multidimensional Arrays Summary/Admin

Inf1-OOP. Arrays. Many Variables of the Same Type. Arrays Introduction to Arrays Arrays in Java Shuffling a Deck Multidimensional Arrays Summary/Admin Inf1-OOP Arrays 1 Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics Arrays Introduction to Arrays Arrays in Java Shuffling a Deck Multidimensional Arrays Summary/Admin January

More information

Object-Oriented Design Lecture 13 CSU 370 Fall 2008 (Pucella) Friday, Oct 31, 2008

Object-Oriented Design Lecture 13 CSU 370 Fall 2008 (Pucella) Friday, Oct 31, 2008 Object-Oriented Design Lecture 13 CSU 370 Fall 2008 (Pucella) Friday, Oct 31, 2008 Laziness For this lecture, I want to return to something that came up during the last homework, the third homework where

More information

Question 1 [20 points]

Question 1 [20 points] Question 1 [20 points] a) Write the following mathematical expression in Java. c=math.sqrt(math.pow(a,2)+math.pow(b,2)- 2*a*b*Math.cos(gamma)); b) Write the following Java expression in mathematical notation.

More information

Linked Lists. References and objects

Linked Lists. References and objects Linked Lists slides created by Marty Stepp http://www.cs.washington.edu/143/ Modified by Sarah Heckman Reading: RS Chapter 16 References and objects In Java, objects and arrays use reference semantics.

More information

Lecture 3 Linear Data Structures

Lecture 3 Linear Data Structures Lecture 3 Linear Data Structures - 1 - Learning Outcomes Based on this lecture, you should: Know the basic linear data structures Be able to express each as an Abstract Data Type (ADT) Be able to specify

More information

Solution for Data Structure

Solution for Data Structure Solution for Data Structure May 2016 INDEX Q1 a 2-3 b 4 c. 4-6 d 7 Q2- a 8-12 b 12-14 Q3 a 15-18 b 18-22 Q4- a 22-35 B..N.A Q5 a 36-38 b N.A Q6- a 39-42 b 43 1 www.brainheaters.in Q1) Ans: (a) Define ADT

More information

Inf1-OP. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

Inf1-OP. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics Inf1-OP Arrays 1 Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics February 26, 2018 1 Thanks to Sedgewick&Wayne for much of this content Arrays Arrays Arrays:

More information

CS 126 Midterm Review Session

CS 126 Midterm Review Session CS 126 Midterm Review Session Overview: For important information, see the webpage Tips on getting started Reviewing important topics Question and Answer Tips on Getting Started Attend this session Read

More information

Inf1-OOP. Arrays 1. Perdita Stevens, adapting earlier version by Ewan Klein. January 11, School of Informatics

Inf1-OOP. Arrays 1. Perdita Stevens, adapting earlier version by Ewan Klein. January 11, School of Informatics Inf1-OOP Arrays 1 Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics January 11, 2015 1 Thanks to Sedgewick&Wayne for much of this content Arrays Introduction to Arrays Arrays

More information

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.) COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.) Recall: The Queue ADT A data structure in which elements enter at one end and are removed from the opposite end is called a

More information

CS61B Spring 2016 Guerrilla Section 2 Worksheet

CS61B Spring 2016 Guerrilla Section 2 Worksheet Spring 2016 27 February 2016 Directions: In groups of 4-5, work on the following exercises. Do not proceed to the next exercise until everyone in your group has the answer and understands why the answer

More information

Linked Lists. Chapter 12.3 in Savitch

Linked Lists. Chapter 12.3 in Savitch Linked Lists Chapter 12.3 in Savitch Preliminaries n Arrays are not always the optimal data structure: q An array has fixed size needs to be copied to expand its capacity q Adding in the middle of an array

More information

University of Palestine. Mid Exam Total Grade: 100

University of Palestine. Mid Exam Total Grade: 100 First Question No. of Branches (5) A) Choose the correct answer: 1. If we type: system.out.println( a ); in the main() method, what will be the result? int a=12; //in the global space... void f() { int

More information

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014 Lists CSC212 Lecture 8 D. Thiebaut, Fall 2014 Review List = Organization of Data in a Linear Fashion, where Order is Important Set of actions that can be carried out efficiently on the data. Typical Actions

More information

Refresher: Interface Specifications. ADT Documentation. Set Represented as an Array. Representation Invariant, Abstraction Function

Refresher: Interface Specifications. ADT Documentation. Set Represented as an Array. Representation Invariant, Abstraction Function CS 247: Software Engineering Principles Representation Invariant, Abstraction Function Refresher: Interface Specifications An interface specification is a contract between a module's provider and the client

More information

Queues Fall 2018 Margaret Reid-Miller

Queues Fall 2018 Margaret Reid-Miller Queues 15-121 Fall 2018 Margaret Reid-Miller Today Exam 2 is next Tuesday, October 30 Writing methods various classes that implement Lists. Methods using Lists and Big-O w/ ArrayList or LinkedLists Prove

More information

Representation Invariant, Abstraction Function

Representation Invariant, Abstraction Function CS 247: Software Engineering Principles Representation Invariant, Abstraction Function Reading: Barbara Liskov and John Guttag, Program Development in Java: Abstraction, Specification, and Object Oriented

More information

Debugging [continued]

Debugging [continued] Debugging [continued] Admiral Grace Murray Hopper http://www.history.navy.mil/photos/images/h96000/h96566kc.htm 1 2 Debugging Your Program Debugging: Where we left off Debugging Your Program. [summary]

More information

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1 CSE 2100 Data Structures and Algorithms... In Java Stacks 1 What is Stack A stack is a collection of objects that are inserted and removed according to the last-in, first-out (LIFO) principle. Internet

More information

Lecture 3 Linear Data Structures

Lecture 3 Linear Data Structures Lecture 3 Linear Data Structures - 1 - Learning Outcomes Based on this lecture, you should: Know the basic linear data structures Be able to express each as an Abstract Data Type (ADT) Be able to specify

More information

Stacks and Their Applications

Stacks and Their Applications Chapter 5 Stacks and Their Applications We have been discussing general list structures. In practice, we often work with some restricted cases, in which insertions and/or deletions occur only at one or

More information

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle. Stacks 1 Stacks The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle. 2 Main stack operations Insertion and removal are defined by: push(e): inserts

More information

CS 61B Spring 2017 Guerrilla Section 3 Worksheet. 25 February 2017

CS 61B Spring 2017 Guerrilla Section 3 Worksheet. 25 February 2017 Spring 2017 25 February 2017 Directions: In groups of 4-5, work on the following exercises. Do not proceed to the next exercise until everyone in your group has the answer and understands why the answer

More information

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013 Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every

More information

csci 210: Data Structures Stacks and Queues

csci 210: Data Structures Stacks and Queues csci 210: Data Structures Stacks and Queues 1 Summary Topics Stacks and Queues as abstract data types ( ADT) Implementations arrays linked lists Analysis and comparison Applications: searching with stacks

More information

Inf1-OP. Arrays 1. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. January 30, School of Informatics

Inf1-OP. Arrays 1. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. January 30, School of Informatics Inf1-OP Arrays 1 Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics January 30, 2017 1 Thanks to Sedgewick&Wayne for much of this content Arrays Arrays:

More information

Odds and Ends. (!) Wed night dinner, Monroe 5:30 Acknowledge assistance as appropriate

Odds and Ends. (!) Wed night dinner, Monroe 5:30 Acknowledge assistance as appropriate Odds and Ends (!) Wed night dinner, Monroe 5:30 Acknowledge assistance as appropriate Review Questions What is an algorithm and how does it relate to a method? What are the two questions about execution

More information

Chapter 8 STRUCTURES IN C

Chapter 8 STRUCTURES IN C Chapter 8 STRUCTURES IN C 1 Structures Introduction Collections of related variables (aggregates) under one name Can contain variables of different data types Commonly used to define records to be stored

More information