Implementing Dynamic Data Structures

Size: px
Start display at page:

Download "Implementing Dynamic Data Structures"

Transcription

1 Chapter 16 Implementing Dynamic Data Structures Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, ISBN: Permission is hereby granted to use these lecture slides in conjunction with the book. Modified: 21/1/10 Java Actually 16: Implementing Dynamic Data Structures 16-1/44 Simple Linked Lists: Insertion Deletion Lookup Other Abstract Data Types (ADT): Stack Queues Overview Java Actually 16: Implementing Dynamic Data Structures 16-2/44

2 Linked lists A simple linked list is a linear collection of objects, called nodes. A node is an example of an object that has a reference to an object of the same type, i.e., is self-referencing. The class Node<E> defines two field variables: The reference that refers to any object. The reference that refers to a node. The nodes can be linked together using the field to form a linked list. Java Actually 16: Implementing Dynamic Data Structures 16-3/44 The class Node /** A node holds and a reference to a node. */ public class Node<E> { /** Data in the node. */ private E ; /** Reference to the node. */ private Node<E> ; public Node(E _obj, Node<E> noderef) { = _obj; = noderef; public void setdata(e obj) { = obj; public void setnext(node<e> node) { = node; public E getdata() { return ; public Node<E> getnext() { return ; Java Actually 16: Implementing Dynamic Data Structures 16-4/44

3 Class diagram for a simple linked list (Figure 16.1) E E LinkedList head Node tail A concrete linked list for strings :String :String :String :String "hail" "ice" "snow" "sleet" :LinkedList<String> = null p:ref(node<string>) Java Actually 16: Implementing Dynamic Data Structures 16-5/44 To insert into and get from the node: String str = head.getdata(); // Get from the first node. p.setdata("frost");// Data in the node referenced by p is changed to "frost". Find the node s successor: Node<String> successor = p.getnext(); // Get successor to p. Set the successor of the node: p.setnext(q); // q set as successor to p. Move the reference p so that it refers to its successor: p = p.getnext(); // Reference p now refers to its successor. Java Actually 16: Implementing Dynamic Data Structures 16-6/44

4 Interface ILinkedListe<E> (Program 16.2, Figure 16.2) public interface ILinkedList<E> extends Iterable<E> { // Mutators public void insertathead(e _obj); public void insertattail(e _obj); public E removefromhead(); public E removefromtail(); boolean remove(e _obj); // Selectors public boolean isempty(); int numberofnodes(); Node<E> first(); Node<E> last(); Node<E> find(e _obj); // Other methods void listtoarray(e[] array); «interface» Iterable<E> Iterator<E> iterator() «interface» ILinkedList<E> LinkedList<E> «interface» Iterator<E> boolean hasnext() E () void remove() LinkedListIterator<E> (a) Implementing linked lists (b) Implementing an iterator for linked lists Java Actually 16: Implementing Dynamic Data Structures 16-7/44 The class LinkedList<E> public class LinkedList<E> implements ILinkedList<E> { /** Head of the list */ private Node<E> head; /** Tail of the list */ private Node<E> tail; /** Number of nodes in the list */ private int numberofnodes; //... Java Actually 16: Implementing Dynamic Data Structures 16-8/44

5 Inserting at the head of a linked list (Program 16.3) If the list is empty The head and the tail are set to refer to the new node Else Step 1: Set the head to be the successor of the new node Step 2: Set the head to refer to the new node public void insertathead(e Obj) { // (1) Node<E> p = new Node<E>(Obj, null); if (isempty()) { head = tail = p; else { // head = new MyNode<E>(Obj, head); p.setnext(head); // Step 1 head = p; // Step 2 numberofnodes++; It is important that the two steps are performed in the right order (Figure 16.3). Java Actually 16: Implementing Dynamic Data Structures 16-9/44 Insert a node at the head of a linked list (Figure 16.4) = null p:ref(node<string>) = "frost" Step 1: p.setnext(head); (b) Step 1 in inserting at the head of the list Step 2: head = p; = null p:ref(node<string>) = "frost" (c) Step 2 in inserting at the head of the list Java Actually 16: Implementing Dynamic Data Structures 16-10/44

6 Inserting at the tail of a linked list If the list is empty The head and the tail are set to refer to the new node Else Step 1: Set the new node as the successor of the tail node Step 2: Set the tail to refer to the new node public void insertattail(e Obj) { // (2) Node<E> p = new Node<E>(Obj, null); if (isempty()) { head = tail = p; else { tail.setnext(p); // Step 1 tail = p; // Step 2 numberofnodes++; Java Actually 16: Implementing Dynamic Data Structures 16-11/44 Insert a node at the tail of a linked list (Figure 16.5b og c) ( ) g Step 1: tail.setnext(p); p:ref(node<string>) = "frost" = null (b) Step 1 in inserting at the tail of the list Step 2: tail = p; p:ref(node<string>) = "frost" = null (c) Step 2 in inserting at the tail of the list Java Actually 16: Implementing Dynamic Data Structures 16-12/44

7 Removing a node from the head of a linked list Step 1: Let p refer to the first node in the list, i.e. same node as the head If the head and the tail refer to the same node, i.e. only one node in the list Both the head and the tail are set to null Else Step 2: Set the head to refer to the successor of the first node referenced by p public E removefromhead() { // (3) assert numberofnodes > 0 : "The list cannot be empty."; Node<E> p = head; // Step 1 if (head == tail) { head = tail = null; // Only one node in the list. else { head = p.getnext(); // Step 2 numberofnodes--; return p.getdata(); Java Actually 16: Implementing Dynamic Data Structures 16-13/44 Removing a node from the head of a linked list (Figure 16.6b og c) ( ) g = null Step 1: p = head; p:ref(node<string>) = "frost" (b) Step 1 in removing from the head of the list Step 2: head = p.getnext(); = null p:ref(node<string>) = "frost" (c) Step 2 in removing from the head of the list Java Actually 16: Implementing Dynamic Data Structures 16-14/44

8 Removing a node from the tail of a linked list We let reference p denote the node to be removed (Figure 16.7, step 1): p = tail; // Step 1 p:ref(node<string>) Step 1: p = tail; (b) Step 1 in removing from the tail of the list = "frost" = null Deleting a node in a list requires knowledge of the predecessor of the node to be deleted. To find the second last node, we have to traverse the list from the head: Let ToLast refer to the first node in the list, i.e. same node as the head Repeat while not arrived at the -to-last node Move the reference ToLast to the successor Java Actually 16: Implementing Dynamic Data Structures 16-15/44 Source code to find the second last node (Figure 16.7, step 2): Node<E> ToLast = head; while (ToLast.getNext()!= tail) // Arrived at the -to-last node? ToLast = ToLast.getNext(); // Move to the successor. Step 2: update ToLast; ToLast:Ref(Node<String>) p:ref(node<string>) (c) Step 2 in removing from the tail of the list = "frost" = null Java Actually 16: Implementing Dynamic Data Structures 16-16/44

9 Removing a node from the tail of a linked list (Fig. 16.8, steps 3 and 4) ToLast:Ref(Node<String>) Step 3: tail = ToLast; p:ref(node<string>) = "frost" = null (d) Step 3 in removing from the tail of the list ToLast:Ref(Node<String>) = null Step 4: tail.setnext(null); p:ref(node<string>) = "frost" = null (e) Step 4 in removing from the tail of the list Java Actually 16: Implementing Dynamic Data Structures 16-17/44 public E removefromtail() { // (4) assert numberofnodes > 0 : "The list cannot be empty."; Node<E> p = tail; // Step 1 if (head == tail) { head = tail = null; // Only one node in the list. else { Node<E> ToLast = head; // Step 2 while (ToLast.getNext()!= tail) { ToLast = ToLast.getNext(); tail = ToLast; // Step 3 tail.setnext(null); // Step 4 numberofnodes--; return p.getdata(); Java Actually 16: Implementing Dynamic Data Structures 16-18/44

10 Removing a node from inside a linked list Find the node to be deleted and its predecessor (step 1): Repeat while the end of the list not reached and the node to be removed not found If the current node is the one to be removed The node is found, and thereby its predecessor Else Move the reference for the current node and for the predecessor Source code to find the node to be deleted and its predecessor: Node<E> currentnode = head; Node<E> predecessor = null; boolean found = false; while (currentnode!= null &&!found) { if (currentnode.getdata().equals(obj)) { found = true; else { // Not yet found. Update predecessor and currentnode references. predecessor = currentnode; // (1) currentnode = currentnode.getnext(); // (2) Java Actually 16: Implementing Dynamic Data Structures 16-19/44 Removing a node from inside a linked list (Figure 16.9, step 1) = "snow" = "sleet" = null (a) Before removing a node from inside the list predecessor:ref(node<string>) currentnode:ref(node<string>) = "snow" = "sleet" = null Step 1: Find node that is to be deleted, and its predecessor; (b) Step 1 in removing a node from inside the list Java Actually 16: Implementing Dynamic Data Structures 16-20/44

11 There are three possibilities to consider in order to delete a node that exists in the list: The node is the first node in the list (predecessor == null). The method removefromhead() is called to delete it. The node is the last node in the list (currentnode.getnext () == null). The tail and the last node are updated: tail = predecessor; tail.setnext(null); Node is inside the list. The successor to the node to be deleted is now the successor to its predecessor (step 2): predecessor.setnext(currentnode.getnext()); Java Actually 16: Implementing Dynamic Data Structures 16-21/44 Removing a node from inside a linked list (Figure 16.9, step 2) predecessor:ref(node<string>) currentnode:ref(node<string>) = "snow" = "sleet" = null Step 2: predecessor.setnext(currentnode.getnext()); (c) Step 2 in removing a node from inside the list Java Actually 16: Implementing Dynamic Data Structures 16-22/44

12 Finding in a linked list The method uses equals() method to compare objects in the list. The method returns the node that contains the object to be found. public Node<E> find(e Obj) { // Traverse the list, starting from the head. Node<E> currentnode = head; while (currentnode!= null) { if (currentnode.getdata().equals(obj)) { return currentnode; // Found. currentnode = currentnode.getnext(); return null; // Not found. Java Actually 16: Implementing Dynamic Data Structures 16-23/44 Iterator for a linked list. The class LinkedList<E> implements the contract ILinkedList<E>, and thus also indirectly the contract Iterable<E> (See Figure 16.3). The class LinkedListIterator implements an iterator for the class LinkedList: public class LinkedListIterator<E> implements Iterator<E> { private Node<E> currentnode; public LinkedListIterator(ILinkedList<E> list) { currentnode = list.first(); public boolean hasnext() { return currentnode!= null; public E () { E = currentnode.getdata(); currentnode = currentnode.getnext(); return ; public void remove() { throw new UnsupportedOperationException(); Java Actually 16: Implementing Dynamic Data Structures 16-24/44

13 Convert a linked list to an array The method fills the specified array with elements from the list. public void listtoarray(e[] toarray) { // (7) assert toarray.length == numberofnodes : "Wrong size of array."; int i = 0; for (E : this) { toarray[i++] = ; Java Actually 16: Implementing Dynamic Data Structures 16-25/44 Example: Convert a linked list to an array public class ListeTilTabellDemo { public static void main(string[] args) { LinkedList<Character> lst = new LinkedList<Character>(); lst.insertathead('a'); lst.insertathead('b'); lst.insertathead('b'); lst.insertathead('a'); for (char c : lst) System.out.print(c); System.out.println(); Character[] array = new Character[lst.numberOfNodes()]; lst.listtoarray(array); System.out.println(array.length); for (char c : array) System.out.print(c); System.out.println(); //... Java Actually 16: Implementing Dynamic Data Structures 16-26/44

14 Data can be shared among linked lists list1: :Node :Node :Node Name: head Type: ref(node) Name: tail Type: ref(node) : : : : list2: :Node :Node :Node :Node Name: head Type: ref(node) Name: tail Type: ref(node) Name: p Type: ref(node) Java Actually 16: Implementing Dynamic Data Structures 16-27/44 ADT: Stack A stack is a LIFO (Last-In, First-Out) structure (Figure 16.10). push(element) pop() tos or top-of-stack "ice" "sleet" "snow" stack peek() Java Actually 16: Implementing Dynamic Data Structures 16-28/44

15 Stack operations Stack operation push(element) pop() peek() isempty() Description Add the element to top of the stack. Removes and returns the first element on the top of the stack. Returns the first element on the top of the stack without removing it from the stack. Returns true or false depending on whether the stack is empty or not. Java Actually 16: Implementing Dynamic Data Structures 16-29/44 Stack Implementation by aggregation Stack operations are delegated to the linked list operations. In this implementation a client can not break the abstraction that a stack represents. The class java.util.stack<e> provides another implementation of stacks. class StackByAggregation<E> { // Field private LinkedList<E> stacklist; // (1) // Constructor public StackByAggregation() { // (2) stacklist = new LinkedList<E>(); // Instance methods public void push(e ) { // (3) stacklist.insertathead(); Java Actually 16: Implementing Dynamic Data Structures 16-30/44

16 public E pop() { // (4) if (empty()) return null; return stacklist.removefromhead(); public E peek() { // (5) if (empty()) return null; return stacklist.first().getdata(); public boolean empty() { // (6) return stacklist.isempty(); Java Actually 16: Implementing Dynamic Data Structures 16-31/44 A client that uses a stack (StackClient.java) public class StackClient { public static void main(string[] args) { // (1) Check if there are program arguments: if (args.length == 0) { System.out.println("Usage: java StackClient <argument list>"); return; // (2) Create a stack: StackByAggregation<String> stack = new StackByAggregation<String>(); // (3) Push all program arguments (strings) on to the stack: for (int i = 0; i < args.length; i++) stack.push(args[i]); // (4) Pop all elements from the stack: while (!stack.empty()) System.out.print(stack.pop().toLowerCase() + " "); System.out.println(); Java Actually 16: Implementing Dynamic Data Structures 16-32/44

17 Running the program: >java StackClient Tall STACKS topple easily easily topple stacks tall Java Actually 16: Implementing Dynamic Data Structures 16-33/44 Using stacks We will look at a problem from graph theory. A graph has nodes and edges connecting the nodes together. The edges are directed, meaning that they have an arrowheads at one end. One can navigate from one node to another in the specified direction. Java Actually 16: Implementing Dynamic Data Structures 16-34/44

18 Graphs Each node is a city (which has a number specified in the node) An edge from one city indicates which city can be accessed directly from this city. 1 0 edge 3 2 node [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] F T F T T F F T F F F F F F T F T T F F F F F F F 4 T:true F:false (a ) Graph (b ) Graph array Java Actually 16: Implementing Dynamic Data Structures 16-35/44 The Problem Which cities can be reached from a given city? We may, for example, reach the cities with the numbers 1, 2 and 4 b starting in city number 3 We can step-wise find the cities that can be accessed directly from a city, and repeat the same process for these cities. The process uses a stack and a set. The stack keeps track of which cities we have not as yet checked to reach other cities directly. The set keeps track of cities that have been reached so far in the process. Each step in the process involves the following: Pop the city from the top of the stack If this city is not in the set Add this city to the set Push all cities that can be reached directly from this city on to the stack Java Actually 16: Implementing Dynamic Data Structures 16-36/44

19 Step to find the cities that are reachable from city number 3 Step no. Stack Set 1. <3> [] 2. <1, 2> [3] 3. <1, 4> [3, 2] 4. <1> [3, 2, 4] 5. <2> [3, 2, 4, 1] 6. <> [3, 2, 4, 1] Java Actually 16: Implementing Dynamic Data Structures 16-37/44 import java.util.hashset; import java.util.scanner; import java.util.set; Example: Graph public class StackClient2 { public static void main(string[] args) { // (1) Create a city graph: boolean[][] citygraph = { {false, true, false, true, true, {false, false, true, false, false, {false, false, false, false, true, {false, true, true, false, false, {false, false, false, false, false ; // (2) Read the city from the keyboard: System.out.print("Type the number of the city to start from [0-" + (citygraph.length-1) + "]: "); Java Actually 16: Implementing Dynamic Data Structures 16-38/44

20 Scanner keyboard = new Scanner(System.in); int startcitynum = keyboard.int(); if (startcitynum < 0 startcitynum >= citygraph.length) { System.out.print("Unknown city number: " + startcitynum); return; // (3) Create a city stack: StackByAggregation<Integer> citystack = new StackByAggregation<Integer>(); // (4) Create a city set: Set<Integer> cityset = new HashSet<Integer>(); // (5) Push start city on the stack: citystack.push(startcitynum); // (6) Handle each city found on the stack: while (!citystack.empty()) { // Pop current city from the stack. int currentcitynum = citystack.pop(); // Check if the city has already been handled. if (!cityset.contains(currentcitynum)) { Java Actually 16: Implementing Dynamic Data Structures 16-39/44 // Insert current city to the city set. cityset.add(currentcitynum); // Push all cities that can be reached directly from // the current city on to the stack for (int j = 0; j < citygraph[currentcitynum].length; j++) { if (citygraph[currentcitynum][j]) citystack.push(j); // Remove start city from the city set, and print the city stack. cityset.remove(startcitynum); System.out.print("From city no. " + startcitynum + ", the following cities can be reached: " + cityset); Running the program: >java StackClient2 Type the number of the city to start from [0-4]: 3 From city no. 3, the following cities can be reached: [1, 2, 4] Java Actually 16: Implementing Dynamic Data Structures 16-40/44

21 ADT: Queues A queue is a FIFO (First-In, First-Out) structure (Figure 16.13). enqueue(element) "ice" "sleet" "snow" dequeue() peek() Queue operation enqueue(element) dequeue() peek() isempty() Queue operations Description Adds the element to the back of the queue. Removes and returns the first element in queue. Returns the first element in the queue without removing it from the queue. Returns true or false depending on whether the queue is empty or not. Java Actually 16: Implementing Dynamic Data Structures 16-41/44 Queue implementation by inheritance Since we have used inheritance, a client can break the abstraction a queue represents. class QueueByInheritance<E> extends LinkedList<E> { public void enqueue(e ) { // (1) insertattail(); public E dequeue() { // (2) if (empty()) return null; return removefromhead(); public E peek() { // (3) if (empty()) return null; return first().getdata(); public boolean empty() { // (4) return isempty(); Java Actually 16: Implementing Dynamic Data Structures 16-42/44

22 Client using a Queue (QueueClient.java) public class QueueClient { public static void main(string[] args) { // (1) Check if there are any program arguments: if (args.length == 0) { System.out.println("Usage: java QueueClient <argument list>"); return; // (2) Create a queue: QueueByInheritance<String> queue = new QueueByInheritance<String>(); // (3) Enqueue all the program arguments (strings): for (int i = 0; i < args.length; i++) queue.enqueue(args[i]); // (4) Dequeue the queue for all elements: while (!queue.empty()) System.out.print(queue.dequeue().toUpperCase() + " "); System.out.println(); Java Actually 16: Implementing Dynamic Data Structures 16-43/44 // (5) Can call methods that queues inherit and break // the queue abstraction: for (int i = 0; i < args.length; i++) queue.insertathead(args[i]); // (6) Print the queue elements: while (!queue.empty()) System.out.print(queue.dequeue().toUpperCase() + " "); System.out.println(); Running the program: >java QueueClient You are now number two in the queue YOU ARE NOW NUMBER TWO IN THE QUEUE QUEUE THE IN TWO NUMBER NOW ARE YOU Java Actually 16: Implementing Dynamic Data Structures 16-44/44

More on Exception Handling

More on Exception Handling Chapter 18 More on Exception Handling Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

More information

More on Exception Handling

More on Exception Handling Chapter 18 More on Exception Handling Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

More information

Chapter 6. Arrays. Java Actually: A Comprehensive Primer in Programming

Chapter 6. Arrays. Java Actually: A Comprehensive Primer in Programming Lecture slides for: Chapter 6 Arrays Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 28. ISBN: 978-1-84448-933-2 http://www.ii.uib.no/~khalid/jac/

More information

Object Communication

Object Communication Chapter 8 Object Communication Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

More information

More on control structures

More on control structures Lecture slides for: Chapter 5 More on control structures Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

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

Data Abstraction and Specification of ADTs

Data Abstraction and Specification of ADTs CITS2200 Data Structures and Algorithms Topic 4 Data Abstraction and Specification of ADTs Example The Reversal Problem and a non-adt solution Data abstraction Specifying ADTs Interfaces javadoc documentation

More information

Program Control Flow

Program Control Flow Lecture slides for: Chapter 3 Program Control Flow Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

More information

Program Control Flow

Program Control Flow Lecture slides for: Chapter 3 Program Control Flow Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

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

CMSC 206: Data Structures Final Exam Reference May 2018

CMSC 206: Data Structures Final Exam Reference May 2018 CMSC 206: Data Structures Final Exam Reference May 2018 public interface BMCSet /** Adds a new item to the set * @param item The new item to add to the set * @return true if the item is a new item added

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

Week 11. Abstract Data Types. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Week 11. Abstract Data Types. CS 180 Sunil Prabhakar Department of Computer Science Purdue University Week 11 Abstract Data Types CS 180 Sunil Prabhakar Department of Computer Science Purdue University Unknown input size Consider a program that has to read in a large number of names from input and print

More information

Chapter 10. Exception Handling. Java Actually: A Comprehensive Primer in Programming

Chapter 10. Exception Handling. Java Actually: A Comprehensive Primer in Programming Chapter 10 Exception Handling Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

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

CS24 Week 4 Lecture 2

CS24 Week 4 Lecture 2 CS24 Week 4 Lecture 2 Kyle Dewey Overview Linked Lists Stacks Queues Linked Lists Linked Lists Idea: have each chunk (called a node) keep track of both a list element and another chunk Need to keep track

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

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11. Lecture 11 1 More Data Structures In this lecture we will use a linked list to implement two abstract data types (ADT) An ADT provides the interface, or what a data structure does We can then use code

More information

Sorting and searching arrays

Sorting and searching arrays Chapter 9 Sorting and searching arrays Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

More information

Stacks and Queues. David Greenstein Monta Vista

Stacks and Queues. David Greenstein Monta Vista Stacks and Queues David Greenstein Monta Vista Stack vs Queue Stacks and queues are used for temporary storage, but in different situations Stacks are Used for handling nested structures: processing directories

More information

Polymorphism and Interfaces

Polymorphism and Interfaces Chapter 13 Polymorphism and Interfaces Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

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

16-Dec-10. Collections & Data Structures ABSTRACTION VS. IMPLEMENTATION. Abstraction vs. Implementation

16-Dec-10. Collections & Data Structures ABSTRACTION VS. IMPLEMENTATION. Abstraction vs. Implementation Collections & Data Structures Boaz Kantor Introduction to Computer Science IDC Herzliya ABSTRACTION VS. IMPLEMENTATION 2 Abstraction vs. Implementation Data structures provide both abstraction and implementation.

More information

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

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice: 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 only one side of the yellow paper. 1. (16

More information

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10 Keeping Order:! Stacks, Queues, & Deques 1 Stacks 2 Stacks A stack is a last in, first out (LIFO) data structure Primary Operations: push() add item to top pop() return the top item and remove it peek()

More information

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1 CMSC 341 Linked Lists, Stacks and Queues CMSC 341 Lists, Stacks &Queues 1 Goal of the Lecture To complete iterator implementation for ArrayList Briefly talk about implementing a LinkedList Introduce special

More information

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

CSE 373 Data Structures and Algorithms. Lecture 2: Queues CSE 373 Data Structures and Algorithms Lecture 2: Queues Queue ADT queue: A list with the restriction that insertions are done at one end and deletions are done at the other First-In, First-Out ("FIFO

More information

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures 10/1/17 Abstract vs concrete data structures 2 Abstract data structures are interfaces they specify only interface (method names and specs) not implementation (method bodies, fields, ) HEAPS AND PRIORITY

More information

Queues. Stacks and Queues

Queues. Stacks and Queues Queues Reading: RS Chapter 14 Slides are modified from those provided by Marty Stepp www.buildingjavaprograms.com 1 Stacks and Queues Sometimes a less powerful, but highly optimized collection is useful.

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

EXAMINATIONS 2012 Trimester 1, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

EXAMINATIONS 2012 Trimester 1, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2012 Trimester 1, MID-TERM TEST COMP103 Introduction

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

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice Name Section Number CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice All Sections Bob Wilson OPEN BOOK / OPEN NOTES You will have all 90 minutes until the start of the next class period. Spend

More information

CIS Fall Data Structures Midterm exam 10/16/2012

CIS Fall Data Structures Midterm exam 10/16/2012 CIS 2168 2012 Fall Data Structures Midterm exam 10/16/2012 Name: Problem 1 (30 points) 1. Suppose we have an array implementation of the stack class, with ten items in the stack stored at data[0] through

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

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

Linked List Nodes (reminder)

Linked List Nodes (reminder) Outline linked lists reminders: nodes, implementation, invariants circular linked list doubly-linked lists iterators the Java foreach statement iterator implementation the ListIterator interface Linked

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

An Introduction to Data Structures

An Introduction to Data Structures An Introduction to Data Structures Advanced Programming ICOM 4015 Lecture 17 Reading: Java Concepts Chapter 20 Fall 2006 Adapded from Java Concepts Companion Slides 1 Chapter Goals To learn how to use

More information

Lecture 4. The Java Collections Framework

Lecture 4. The Java Collections Framework Lecture 4. The Java s Framework - 1 - Outline Introduction to the Java s Framework Iterators Interfaces, Classes and Classes of the Java s Framework - 2 - Learning Outcomes From this lecture you should

More information

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2 CSCI 136 Data Structures & Advanced Programming Lecture 13 Fall 2018 Instructors: Bill 2 Announcements Lab today! After mid-term we ll have some non-partner labs It s Lab5 not Lab 4 Mid-term exam is Wednesday,

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

Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15

Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15 Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15 Title: Demonstrate implementation of data structure in Java Objectives: To learn implementation of data structure

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Iterator 1 (part I) Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 24, 2013 Abstract These lecture notes are meant to be looked

More information

Notes on access restrictions

Notes on access restrictions Notes on access restrictions A source code file MyClass.java is a compilation unit and can contain at most one public class. Furthermore, if there is a public class in that file, it must be called MyClass.

More information

Interfaces & Generics

Interfaces & Generics Interfaces & Generics CSC207 Winter 2018 The Programming Interface The "user" for almost all code is a programmer. That user wants to know:... what kinds of object your class represents... what actions

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

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list } Linked Lists Since a variable referencing an object just holds the address of the object in memory, we can link multiple objects together to form dynamic lists or other structures. In our case we will

More information

Stacks and Queues. Fundamentals of Computer Science.

Stacks and Queues. Fundamentals of Computer Science. Stacks and Queues http://www.flickr.com/photos/mac-ash/4534203626/ http://www.flickr.com/photos/thowi/182298390/ Fundamentals of Computer Science Outline Terminology Abstract Data Types ADT) Data structures

More information

6.005 Elements of Software Construction Fall 2008

6.005 Elements of Software Construction Fall 2008 MIT OpenCourseWare http://ocw.mit.edu 6.005 Elements of Software Construction Fall 2008 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. 6.005 elements

More information

Collections and Iterators. Collections

Collections and Iterators. Collections Collections and Iterators Based on the notes from David Fernandez-Baca and Steve Kautz Based on The Java Tutorial (http://docs.oracle.com/javase/tutorial/java/) Bryn Mawr College CS206 Intro to Data Structures

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

CS 61B Spring 2017 Guerrilla Section 3 Solutions

CS 61B Spring 2017 Guerrilla Section 3 Solutions 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

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving In Computer Science, an abstract data type (ADT) is a mathematical model for a certain data type or structures. This doesn t mean that the ADTs cannot be programmed. But that we must first understand them

More information

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values) 9// References and Homework Text: Chapters, and ABSTRACT DATA TYPES; LISTS & TREES Homework: Learn these List methods, from http://docs.oracle.com/javase/7/docs/api/java/util/list.html add, addall, contains,

More information

C Sc 227 Practice Test 2 Section Leader Your Name 100pts. a. 1D array b. PriorityList<E> c. ArrayPriorityList<E>

C Sc 227 Practice Test 2 Section Leader Your Name 100pts. a. 1D array b. PriorityList<E> c. ArrayPriorityList<E> C Sc 227 Practice Test 2 Section Leader Your Name 100pts 1. Approximately how many lectures remain in C Sc 227 (give or take 2)? (2pts) 2. Determine the tightest upper bound runtimes of the following loops.

More information

CS171 Midterm Exam. October 29, Name:

CS171 Midterm Exam. October 29, Name: CS171 Midterm Exam October 29, 2012 Name: You are to honor the Emory Honor Code. This is a closed-book and closed-notes exam. You have 50 minutes to complete this exam. Read each problem carefully, 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

A List Implementation That Links Data

A List Implementation That Links Data A List Implementation That Links Data Chapter 14 Contents Operations on a Chain of Linked Nodes Adding a Node at Various Positions Removing a Node from Various Positions The Private Method getnodeat Contents

More information

The Java Collections Framework. Chapters 7.5

The Java Collections Framework. Chapters 7.5 The Java s Framework Chapters 7.5 Outline Introduction to the Java s Framework Iterators Interfaces, Classes and Classes of the Java s Framework Outline Introduction to the Java s Framework Iterators Interfaces,

More information

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University Lecture 6 COMP1006/1406 (the OOP course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments A1,A2,A3 are all marked A4 marking just started A5 is due Friday, A6 is due Monday a quick

More information

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue Module 4: Queue ADT Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Queue ADT Features (Logical View) A List that

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

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

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

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

CMSC 132, Object-Oriented Programming II Summer Lecture 6: CMSC 132, Object-Oriented Programming II Summer 2017 Lecturer: Anwar Mamat Lecture 6: Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 6.1 Singly

More information

More Data Structures (Part 1) Stacks

More Data Structures (Part 1) Stacks More Data Structures (Part 1) Stacks 1 Stack examples of stacks 2 Top of Stack top of the stack 3 Stack Operations classically, stacks only support two operations 1. push 2. pop add to the top of the stack

More information

EXAMINATIONS 2016 TRIMESTER 2

EXAMINATIONS 2016 TRIMESTER 2 T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW VICTORIA U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2016 TRIMESTER 2 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

More information

CS6202 - PROGRAMMING & DATA STRUCTURES I Unit IV Part - A 1. Define Stack. A stack is an ordered list in which all insertions and deletions are made at one end, called the top. It is an abstract data type

More information

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack. STACK Stack ADT 2 A stack is an abstract data type based on the list data model All operations are performed at one end of the list called the top of the stack (TOS) LIFO (for last-in first-out) list is

More information

Abstract Data Types. Abstract Data Types

Abstract Data Types. Abstract Data Types Abstract Data Types Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria Wolfgang.Schreiner@risc.jku.at http://www.risc.jku.at Wolfgang Schreiner

More information

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one Iterators What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one 9-2 2-2 What is an Iterator? An iterator is an abstract data

More information

DNHI Homework 3 Solutions List, Stacs and Queues

DNHI Homework 3 Solutions List, Stacs and Queues Solutions List, Stacs and Queues Problem 1 Given the IntegerQueue ADT below state the return value and show the content of the, initially empty, queue of Integer objects after each of the following operations.

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

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps 9/26/2018 Data Structure & Algorithm Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps 1 Quiz 10 points (as stated in the first class meeting)

More information

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Cpt S 122 Data Structures. Course Review Midterm Exam # 1 Cpt S 122 Data Structures Course Review Midterm Exam # 1 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 1 When: Friday (09/28) 12:10-1pm Where:

More information

CSC 321: Data Structures. Fall 2017

CSC 321: Data Structures. Fall 2017 CSC 321: Data Structures Fall 2017 Linked structures nodes & recursive fields singly-linked list doubly-linked list LinkedList implementation iterators 1 ArrayLists vs. LinkedLists to insert or remove

More information

Stacks and Queues. Gregory D. Weber. CSCI C243 Data Structures

Stacks and Queues. Gregory D. Weber. CSCI C243 Data Structures Stacks and Queues Gregory D. Weber CSCI C243 Data Structures Principal Points 1. The Stack interface: how to declare it, what it does. 2. Generics: why they were added to Java, how to use them. 3. The

More information

Data Structures G5029

Data Structures G5029 Data Structures G5029 Lecture 2 Kingsley Sage Room 5C16, Pevensey III khs20@sussex.ac.uk University of Sussex 2006 Lecture 2 Stacks The usual analogy is the stack of plates. A way of buffering a stream

More information

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/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

16. Dynamic Data Structures

16. Dynamic Data Structures Data Structures 6. Dynamic Data Structures A data structure is a particular way of organizing data in a computer so that it can be used efficiently Linked lists, Abstract data types stack, queue, Sorted

More information

Java Review: Objects

Java Review: Objects Outline Java review Abstract Data Types (ADTs) Interfaces Class Hierarchy, Abstract Classes, Inheritance Invariants Lists ArrayList LinkedList runtime analysis Iterators Java references 1 Exam Preparation

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

Lecture 2: Stacks and Queues

Lecture 2: Stacks and Queues Lecture 2: Stacks and Queues CSE 373: Data Structures and Algorithms CSE 373 19 WI - KASEY CHAMPION 1 Warm Up 1. Grab a worksheet 2. Introduce yourself to your neighbors J 3. Discuss the answers 4. Log

More information

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , CSE 143 Lecture 26 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, 15.3-15.4, 16.4-16.5 slides created by Marty Stepp, adapted by Alyssa Harding

More information

Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types

Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types 1 Data Structures and Algorithms Chapter 5 and Abstract Data Types Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Information Technology and Engineering Iterator (part II) Inner class Implementation: fail-fast Version of March 20, 2011 Abstract These

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

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

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Queues. CITS2200 Data Structures and Algorithms. Topic 5 CITS2200 Data Structures and Algorithms Topic 5 Queues Implementations of the Queue ADT Queue specification Queue interface Block (array) representations of queues Recursive (linked) representations of

More information

1. Introduction. Lecture Content

1. Introduction. Lecture Content Lecture 6 Queues 1 Lecture Content 1. Introduction 2. Queue Implementation Using Array 3. Queue Implementation Using Singly Linked List 4. Priority Queue 5. Applications of Queue 2 1. Introduction A queue

More information

Abstract data types (again) Announcements. Example ADT an integer bag (next) The Java Collections Framework

Abstract data types (again) Announcements. Example ADT an integer bag (next) The Java Collections Framework Announcements Abstract data types (again) PS 5 ready Tutoring schedule updated with more hours Today s topic: The Java Collections Framework Reading: Section 7.5 An ADT is a model of a collection of data

More information

Basic Programming Elements

Basic Programming Elements Chapter 2 Basic Programming Elements Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

More information

Stacks and Queues as Collections

Stacks and Queues as Collections An abstract data type (ADT) is a set of data and the particular operations that are allowed on that data Data Type is really about techniques managing collections of data in certain ways Abstract means

More information

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/22/04 Lecture 4 1

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/22/04 Lecture 4 1 Abstract Data Types Add Client Prog Rem Find Data Str. Show 01/22/04 Lecture 4 1 Containers Powerful tool for programming data structures Provides a library of container classes to hold your objects 2

More information

Lecture 16. Lecture

Lecture 16. Lecture Recursive lists D0010E Variants of lists Doubly linked lists Binary trees Circular lists - Håkan Jonsson 1 - Håkan Jonsson 2 - Håkan Jonsson 3 1 1. Circular lists A singly linked list has a beginning and

More information

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example Stacks and Queues Introduction to abstract data types (ADTs) Stack ADT applications interface for Java Stack ex use: reverse a sequence of values Queue ADT applications interface for Java Queue Time permitting:

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

CS 314 Midterm 2 Fall 2012

CS 314 Midterm 2 Fall 2012 Points off 1 2 3 4 5 Total off Net Score CS 314 Midterm 2 Fall 2012 Your Name_ Your UTEID Circle yours TA s name: John Zihao Instructions: 1. There are 5 questions on this test. 2. You have 2 hours to

More information

Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Stacks and Queues EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG What is a Stack? A stack is a collection of objects. Objects in a stack are inserted and removed according to the

More information