CS/CE 2336 Computer Science II

Size: px
Start display at page:

Download "CS/CE 2336 Computer Science II"

Transcription

1 CS/CE 2336 Computer Science II UT D Session 11 OO Data Structures Lists, Stacks, Queues Adapted from D. Liang s Introduction to Java Programming, 8 th Ed. 2

2 What is a Data Structure? A collection of data structured(organized) in some fashion A data structure not only stores data, but also supports the operations for manipulating data in the structure in a certain way E.g., an array is a data structure that holds a collection of data in sequential order Array manipulation: store, retrieve, and modify data in entries of the array, find the size of the array etc Array is simple and easy to use but it has limitations 3 Limitations of Arrays Once created, the size of an array cannot be altered (cannot grow or shrink)! Array provides inadequate support for many operations on its s Insertingand deleting Sorting and searching Arrays are in a sense a staticdata structure But many applications require dynamicdata structures Data structures that can change dynamically at run-time 4

3 Object-Oriented Data Structures In object-oriented paradigm, a data structure is an objectthat stores other objects, referred to as data or s Some time data structure is referred to as a container object or a collection object To define a data structure is essentially to declare a class The class for a data structure should use data fields to store s and provide methods to support operations on its s To create a data structure is therefore to create an instance from the class Methods of the instance are used to manipulate the data structure: inserting, deleting etc 5 Four Classic Data Structures Four classic dynamic data structures to be introduced in this chapter: list, stack, queue and binary tree A list is a collection of data stored sequentially that supports insertion and deletion anywhere in the list A stack can be perceived as a special type of the list where insertions and deletions take place only at the one end, referred to as the top of a stack A queue represents a special list where insertions take place at the back (also referred to as the of) of a queue and deletions take place from the front (also referred to as the of) of a queue A binary tree is a data structure to support efficient searching, sorting, inserting, and deleting data 6

4 Lists A list is a popular data structure to store data in sequential order For example, a list of students, a list of available rooms, a list of cities, and a list of books, etc. can be stored using lists Common operations on a list Retrieve an from the list Insert a new to the list Delete an from the list Find how many s are in the list Determine if an is in the list Determine if the list is empty (contains zero ) 7 Implementing a List Two ways to implement a list can be considered: array and linked structure Array Array is used to store the s The array is dynamically created If the capacity of the array is exceeded, create a new larger array and copy all the s from the current array to the new array Linked structure A linked structure consists of nodes Each node is dynamically created to hold an All the nodes are linkedtogether to form a list 8

5 Design of ArrayList and LinkedList For convenience, let s name these two classes: MyArrayList and MyLinkedList These two classes have common operations, but different data fields The common operations can be generalizedin an interface or an abstract class A good strategy is to combine the virtues of interfaces and abstract classes by providing both interface and abstract class in the design The user can use either the interface or the abstract class whichever is convenient Such an abstract class is known as a convenience class MyArrayList MyList MyAbstractList MyLinkedList 9 MyList Interface and MyAbstractList Class +add(e: E) : void «interface» MyList<E> +add(index: int, e: E) : void +clear(): void +contains(e: E): boolean +get(index: int) : E +indexof(e: E) : int +isempty(): boolean +lastindexof(e: E) : int +remove(e: E): boolean +size(): int +remove(index: int) : E +set(index: int, e: E) : E MyAbstractList<E> #size: int #MyAbstractList() #MyAbstractList(objects: E[]) +add(e: E) : void +isempty(): boolean +size(): int +remove(e: E): boolean Appends a new at the end of this list. Adds a new at the specified index in this list. Removes all the s from this list. Returns true if this list contains the. Returns the from this list at the specified index. Returns the index of the first matching in this list. Returns true if this list contains no s. Returns the index of the last matching in this list. Removes the from this list. Returns the number of s in this list. Removes the at the specified index and returns the removed. Sets the at the specified index and returns the you are replacing. The size of the list. Creates a default list. Creates a list from an array of objects. Implements the add method. Implements the isempty method. Implements the size method. Implements the remove method. MyList MyAbstractList 10

6 Array Lists Array is a fixed-sizedata structure: once an array is created, its size cannot be changed but it can be used to implement dynamic data structures The trick is to create a new larger array to replace the current array if the current array cannot hold new s in the list: Initially an array is created with a default size When inserting a new into the array, first ensure there is enough room in the array If not create a new array with the size as twice as the current one Copy the s from the current array to the new array The new array now becomes the current array 11 Array List Animation 12

7 Insertion Before inserting a new at a specified index, shift all the s after the index to the right and increase the list size by 1 Before inserting e at insertion point i 0 1 i i+1 k-1 e 0 e 1 e i-1 e i e i+1 e k-1 k e k e Insertion point shift data.length -1 After inserting e at insertion point i, list size is incremented by i i+1 i+2 e 0 e 1 e i-1 e e i e i+1 k k+1 e k-1 e k e inserted here data.length Deletion To remove an at a specified index, shift all the s after the index to the left by one position and decrease the list size by 1 Before deleting the at index i 0 1 i i+1 k-1 e 0 e 1 e i-1 e i e i+1 e k-1 k e k Delete this shift data.length -1 After deleting the, list size is decremented by e 0 e 1 e i-1 i e i+1 k-2 k-1 e k-1 e k data.length -1 14

8 Implementing MyArrayList MyAbstractList<E> MyArrayList<E> -data: E[] +MyArrayList() +MyArrayList(objects: E[]) -ensurecapacity(): void Creates a default array list. Creates an array list from an array of objects. Doubles the current array size if needed. MyArrayList TestList 15 Linked Lists MyArrayList is implemented using an array The methods get(int index)and set(int index, Object o)for accessing and modifying an through an index and the add (Object o) for adding an at the end of the list are efficient But the methods add(int index, Object o) and remove(int index)are inefficient Why? Because it requires shifting potentially a large number of s A new technique is required! How about linked structures A linked structure improves efficiency for adding and removing an anywhere in a list 16

9 Linked List Animation 17 Nodes in Linked Lists A linked list consists of nodes, each node contains an, and each node is linked to its neighbor A node can be defined as a class, as follows: class Node<E> { E ; Node ; public Node(E o) { = o; Node 1 Node 2 ele ment Node n ne xt 18

10 Adding Three Nodes The variable refers to the first node in the list, and the variable refers to the last node in the list If the list is empty, both are For example, you can create three nodes to store three strings in a list, as follows: Step 1: Declare and : Node<String> = ; Node<String> = ; The list is empty now 19 Adding Three Nodes - cont d Step 2: Create the first node and insert it to the list: = new Node<String>("Chicago"); = ; After the first n ode is inserted "Chicago" : 20

11 Adding Three Nodes - cont d Step 3: Create the second node and insert it to the list:. = new Node<String>("Denver"); "Chicago" "Denver" : =.; "Chicago" "Denver" : 21 Adding Three Nodes - cont d Step 4: Create the third node and insert it to the list:. = new Node<String>("Dallas"); "Chicago" "Denver" "Dallas" : =.; "Chicago" "Denver" "Dallas" : 22

12 Traversing All Elements in the List Each node contains the and a data field named that points to the (the link) If the node is the last in the list, its data field contains the value This property can be used to detect the last node List traversal Node<E> current = ; while (current!= ) { System.out.println(current.); current = current.; 23 MyLinkedList MyAbstractList<E> Node<E> m 1 MyLinkedList<E> : E : Node<E> -: Node<E> -: Node<E> 1 Link +MyLinkedList() +MyLinkedList(objects: E[]) +addfirst(e: E): void +addlast(e: E): void +getfirst(): E +getlast(): E +removefirst(): E +removelast(): E Creates a default linked list. Creates a linked list from an array of objects. Adds the object to the of the list. Adds the object to the of the list. Returns the first object in the list. Returns the last object in the list. Removes the first object from the list. Removes the last object from the list. MyLinkedList 24

13 Implementing addfirst(e o) public void addfirst(e o) { Node<E> newnode = new Node<E>(o); newnode. = ; = newnode; size++; if ( == ) = ; e0 ei A new node to be inserted here ei+1 (a) Before a new node is inserted. ek e0 ei ei+1 ek New node inserted here (b) After a new node is inserted. 25 Implementing addlast(e o) public void addlast(e o) { if ( == ) { = = new Node<E>(); else {. = new Node(); =.; size++; e0 ei ei+1 (a) Before a new node is inserted. ek A new node to be inserted here o e0 ei ei+1 ek o (b) After a new node is inserted. New node inserted here 26

14 e0 Implementing add(int index, E o) current ei A new node to be inserted here e temp ei+1 ek (a) Before a new node is inserted. e0 A new node is inserted in the list current ei e temp ei+1 ek (b) After a new node is inserted. public void add(int index, E o) { if (index == 0) addfirst(o); else if (index >= size) addlast(o); else { Node<E> current = ; for (int i = 1; i < index; i++) current = current.; Node<E> temp = current.; current. = new Node<E>(o); (current.). = temp; size++; 27 Implementing removefirst() e 0 e 1 e i e i+1 e k Delete this node (a) Before the node is deleted. public E removefirst() { if (size == 0) return ; else { Node<E> temp = ; =.; size--; if ( == ) = ; return temp.; e 1 (b) After the first node is deleted 28 e i e i+1 e k

15 public E removelast() { if (size == 0) return ; else if (size == 1) { Node<E> temp = ; = = ; e 0 size = 0; return temp.; else { Node<E> current = ; for (int i = 0; i < size - 2; i++) current = current.; e 0 Node temp = ; = current;. = ; size--; return temp.; Implementing removelast() e 1 e 1 e k-2 (a) Before the node is deleted. e k-2 e k-1 (b) After the last node is deleted current e k-1 e k Delete this node 29 Implementing remove(int index) public E remove(int index){ if (index < 0 index >= size) return ; else if (index == 0) return removefirst(); else if (index == size - 1) return removelast(); else { Node<E> previous = ; for (int i = 1; i < index; i++) { previous = previous.; Node<E> current = previous.; previous. = current.; size--; return current.; previous current current. Node to be deleted (a) Before the node is deleted. previous current. (b) After the node is deleted. 30

16 Circular Linked Lists A circular, singly linked listis like a singly linked list, except that the pointer of the last node points back to the first node Node 1 Node 2 Node n 31 Doubly Linked Lists A doubly linked list contains the nodes with two pointers (links) One points to the node and the other points to the previous node These two pointers are conveniently called a forward pointer and a backward pointer Why two pointers? A doubly linked list can be traversed both ways: forward and backward Node 1 Node 2 Node n previous previous 32

17 Circular Doubly Linked Lists A circular, doubly linked listis doubly linked list, except that the forward pointer of the last node points to the first node and the backward pointer of the first pointer points to the last node Node 1 Node 2 Node n previous previous previous 33 Stacks A stack can be viewed as a special type of list, where the s are accessed, inserted, and deleted only from the end, called the top, of the stack Data1 Data2 Data3 Data1 Data2 Data1 Data3 Data2 Data1 Data3 Data2 Data1 Data2 Data1 Data1 34

18 Queues A queue can be viewed as a special type of list, where the s are inserted into the end () of the queue, and are accessed and deleted from the beginning () of the queue Data1 Data2 Data3 Data1 Data2 Data1 Data3 Data2 Data1 Data3 Data2 Data3 Data1 Data2 Data3 35 Stack Animation 36

19 Queue Animation 37 Implementing Stacks and Queues How do we implement a Stack? How do we implement Queue? Since the insertion and deletion operations on a stack are made only at the end of the stack, using an array listto implement a stack is more efficient than a linked list Since deletions and insertions are made at the beginning and the end of the list, it is more efficient to implement a queue using a linked list than an array list 38

20 Design of the Stack and Queue Classes Two ways to design the stack and queue classes: Using inheritance: declare the stack class by extending the array list class, and the queue class by extending the linked list class MyArrayList MyStack MyLinkedList MyQueue Using composition: You can declare an array list as a data field in the stack class, and a linked list as a data field in the queue class MyStack MyArrayList MyQueue MyLinkedList 39 Composition is Better Both designs are fine, but using composition is better Why? Because composition enables the definition of a complete new stack class and queue class with only necessary attributes and methods No need to inherit the unnecessary and inappropriate methods from the array list and linked list 40

21 MyStack and MyQueue MyStack -list: MyArrayList +isempty(): boolean +getsize(): int +peek(): Object +pop(): Object +push(o: Object): Object +search(o: Object): int MyStack Returns true if this stack is empty. Returns the number of s in this stack. Returns the top in this stack. Returns and removes the top in this stack. Adds a new to the top of this stack. Returns the position of the specified in this stack. MyQueue<E> -list: MyLinkedList<E> +enqueue(e: E): void +dequeue(): E +getsize(): int MyQueue Adds an to this queue. Removes an from this queue. Returns the number of s from this queue. 41 Examples Using Stacks and Queues Write a program that creates a stack using MyStack and a queue using MyQueue. It then uses the push (enqueue) method to add strings to the stack (or queue) and the pop(dequeue) method to remove strings from the stack (or queue). TestStackQueue 42

22 Priority Queue A regular queue is a first-in and first-out data structure Elements are appended to the end of the queue and are removed from the beginning of the queue In a priority queue, s are assigned with priorities When accessing s, the with the highest priority is removed first A priority queue has a largest-in, first-out behavior E.g., in the emergency room in a hospital assigns patients with priority numbers; the patient with the highest priority is treated first MyPriorityQueue<E> -heap: Heap<E> +enqueue(: E): void +dequeue(): E +getsize(): int Adds an to this queue. Removes an from this queue. Returns the number of s from this queue. MyPriorityQueue TestPriorityQueue 43

Implementing Lists, Stacks, Queues, and Priority Queues

Implementing Lists, Stacks, Queues, and Priority Queues Implementing Lists, Stacks, Queues, and Priority Queues CSE260, Computer Science B: Honors Stony Brook University http://www.cs.stonybrook.edu/~cse260 1 Objectives To design common features of lists in

More information

Linked Lists, Stacks, Queues, and Priority Queues

Linked Lists, Stacks, Queues, and Priority Queues ***This chapter is a bonus Web chapter CHAPTER 18 Linked Lists, Stacks, Queues, and Priority Queues Objectives To design and implement linked lists ( 18.2). To explore variations of linked lists ( 18.2).

More information

CSC 321: Data Structures. Fall 2012

CSC 321: Data Structures. Fall 2012 CSC 321: Data Structures Fall 2012 Lists, stacks & queues Collection classes: List (ArrayList, LinkedList), Set (TreeSet, HashSet), Map (TreeMap, HashMap) ArrayList performance and implementation LinkedList

More information

CSC 321: Data Structures. Fall 2013

CSC 321: Data Structures. Fall 2013 CSC 321: Data Structures Fall 2013 Lists, stacks & queues Collection classes: List (ArrayList, LinkedList), Set (TreeSet, HashSet), Map (TreeMap, HashMap) ArrayList performance and implementation LinkedList

More information

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux CS 231 Data Structures and Algorithms Fall 2018 DDL & Queue Lecture 20 October 22, 2018 Prof. Zadia Codabux 1 Agenda Mid Semester Analysis Doubly Linked List Queue 2 Administrative None 3 Doubly Linked

More information

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES CH4.2-4.3. ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER

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

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College CISC 3115 TY3 C24a: Lists Hui Chen Department of Computer & Information Science CUNY Brooklyn College 11/15/2018 CUNY Brooklyn College 1 Outline Concept of data structure Use data structures List Stack

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

ITI Introduction to Computing II

ITI Introduction to Computing II index.pdf March 17, 2013 1 ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 17, 2013 Definitions A List is a linear abstract

More information

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists CSE 11 Computer Science 1 Module 9a: ADT Representations Stack and queue ADTs array implementations Buffer ADT and circular lists Buffer ADT add and remove from both ends»can grow and shrink from either

More information

Lecture 12: Doubly Linked Lists

Lecture 12: Doubly Linked Lists Lecture 12: Doubly Linked Lists CS 62 Fall 2018 Alexandra Papoutsaki & William Devanny 1 Doubly Linked List A linked list consisting of a sequence of nodes, starting from a head pointer and ending to a

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 07: Linked Lists MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Linked Lists 2 Introduction Linked List Abstract Data Type SinglyLinkedList ArrayList Keep in Mind Introduction:

More information

Queue. COMP 250 Fall queues Sept. 23, 2016

Queue. COMP 250 Fall queues Sept. 23, 2016 Queue Last lecture we looked at a abstract data type called a stack. The two main operations were push and pop. I introduced the idea of a stack by saying that it was a kind of list, but with a restricted

More information

COMP 250 Midterm #2 March 11 th 2013

COMP 250 Midterm #2 March 11 th 2013 NAME: STUDENT ID: COMP 250 Midterm #2 March 11 th 2013 - This exam has 6 pages - This is an open book and open notes exam. No electronic equipment is allowed. 1) Questions with short answers (28 points;

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

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

1 P age DS & OOPS / UNIT II

1 P age DS & OOPS / UNIT II UNIT II Stacks: Definition operations - applications of stack. Queues: Definition - operations Priority queues - De que Applications of queue. Linked List: Singly Linked List, Doubly Linked List, Circular

More information

CSC212:Data Structure

CSC212:Data Structure CSC212:Data Structure 1 Queue: First In First Out (FIFO). Used in operating systems, simulations etc. Priority Queues: Highest priority item is served first. Used in operating systems, printer servers

More information

Array Lists Outline and Required Reading: Array Lists ( 7.1) CSE 2011, Winter 2017, Section Z Instructor: N. Vlajic

Array Lists Outline and Required Reading: Array Lists ( 7.1) CSE 2011, Winter 2017, Section Z Instructor: N. Vlajic rray Lists Outline and Required Reading: rray Lists ( 7.) CSE 20, Winter 207, Section Z Instructor: N. Vlajic Lists in Everyday Life 2 List convenient way to organize data examples: score list, to-do list,

More information

CSE 143 Lecture 4. Implementing ArrayIntList; Binary Search. reading:

CSE 143 Lecture 4. Implementing ArrayIntList; Binary Search. reading: CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search reading: 15.1-15.3 slides adapted from Marty Stepp and Hélène Martin http://www.cs.washington.edu/143/ Exercise Let's write a class that implements

More information

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures Data Structures in C C Programming and Software Tools N.C. State Department of Computer Science Data Structures in C The combination of pointers, structs, and dynamic memory allocation allow for creation

More information

CS350: Data Structures. Doubly Linked Lists

CS350: Data Structures. Doubly Linked Lists Doubly Linked Lists James Moscola Department of Physical Sciences York College of Pennsylvania James Moscola Doubly Linked Lists Adds an additional pointer to a the list nodes that points to the previous

More information

COMP 250. Lecture 8. stack. Sept. 25, 2017

COMP 250. Lecture 8. stack. Sept. 25, 2017 COMP 250 Lecture 8 stack Sept. 25, 2017 1 get(i) set(i,e) add(i,e) remove(i) remove(e) clear() isempty() size() What is a List (abstract)? // Returns the i-th element (but doesn't remove it) // Replaces

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

COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Fall Midterm COS 226 Algorithms and Data Structures Fall 2017 Midterm This exam has 10 questions (including question 0) worth a total of 55 points. You have 0 minutes. This exam is preprocessed by a computer, so please

More information

A Linked Structure. Chapter 17

A Linked Structure. Chapter 17 Chapter 17 A Linked Structure This chapter demonstrates the OurList interface implemented with a class that uses a linked structure rather than the array implementation of the previous chapter. The linked

More information

PA3 Design Specification

PA3 Design Specification PA3 Teaching Data Structure 1. System Description The Data Structure Web application is written in JavaScript and HTML5. It has been divided into 9 pages: Singly linked page, Stack page, Postfix expression

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

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Readings List Implementations Chapter 20.2 Objectives Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Additional references:

More information

182 review 1. Course Goals

182 review 1. Course Goals Course Goals 182 review 1 More experience solving problems w/ algorithms and programs minimize static methods use: main( ) use and overload constructors multiple class designs and programming solutions

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

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

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS T E W H A R E W Ā N A N G A O T E Student ID:....................... Ū 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 2015 TRIMESTER 2 COMP103 INTRODUCTION

More information

Data Structures and Algorithms Notes

Data Structures and Algorithms Notes Data Structures and Algorithms Notes Notes by Winst Course taught by Dr. G. R. Baliga 256-400 ext. 3890 baliga@rowan.edu Course started: September 4, 2012 Last generated: December 18, 2013 Interfaces -

More information

Lists. The List ADT. Reading: Textbook Sections

Lists. The List ADT. Reading: Textbook Sections Lists The List ADT Reading: Textbook Sections 3.1 3.5 List ADT A list is a dynamic ordered tuple of homogeneous elements A o, A 1, A 2,, A N-1 where A i is the i-th element of the list The position of

More information

Abstract Data Types. Definitions, Implementations, and Uses

Abstract Data Types. Definitions, Implementations, and Uses Abstract Data Types Definitions, Implementations, and Uses ADT An abstract data representation without reference to a specific implementation TABLE QUEUE STACK The primitive operations on the ADT elements

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 07: Linked Lists and Iterators MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Linked Lists 2 Introduction Linked List Abstract Data Type General Implementation of the ListADT

More information

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved. Data Structures Outline Introduction Linked Lists Stacks Queues Trees Introduction dynamic data structures - grow and shrink during execution Linked lists - insertions and removals made anywhere Stacks

More information

January 24, Abstract Data Types (ADTs) An ADT is an abstraction of a data structure.

January 24, Abstract Data Types (ADTs) An ADT is an abstraction of a data structure. Lists CSE 2011 Winter 2007 January 24, 2007 1 Abstract Data Types (ADTs) An ADT is an abstraction of a data structure. An ADT specifies: data stored operations on the data error conditions associated with

More information

Iterators and Sequences

Iterators and Sequences Iterators and Sequences Iterators and Sequences 1 Iterators An iterator abstracts the process of scanning through a collection of elements It maintains a cursor that sits between elements in the list,

More information

Doubly LinkedList is Symmetrical! LinkedList Efficiency. Monday, April 8, 13. insert insert remove remove remove walk

Doubly LinkedList is Symmetrical! LinkedList Efficiency. Monday, April 8, 13. insert insert remove remove remove walk How Can We Improve the State of Experimental Evaluation in Computer Siene Peter Sweeney IBM Researh, TJ Watson Friday, April 12, 12:00 Kendade 307 1 Doubly LinkedList is Symmetrial! insert insert remove

More information

Chapter 9 Lists. Concepts: The list abstraction Singly linked lists Doubly linked lists Circular lists Vectors as lists

Chapter 9 Lists. Concepts: The list abstraction Singly linked lists Doubly linked lists Circular lists Vectors as lists Chapter 9 Lists Concepts: The list abstraction Singly linked lists Doubly linked lists Circular lists Vectors as lists He s makin a list and checkin it twice! Haven Gillespie IMAGINE YOU ARE TO WRITE A

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

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University //3 Part Queues CS Algorithms and Data Structures Outline Queue? Implementing Queue Comparison implementations 8 Photo by David Jump 9 //3 "Grill the Buffs" event Queue A queue is like a line of people

More information

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue Outline Part Queues Queue? Implementing Queue Comparison implementations CS Algorithms and Data Structures 8 "Grill the Buffs" event Photo by David Jump 9 Queue A queue is like a line of people New item

More information

Ashish Gupta, Data JUET, Guna

Ashish Gupta, Data JUET, Guna Categories of data structures Data structures are categories in two classes 1. Linear data structure: - organized into sequential fashion - elements are attached one after another - easy to implement because

More information

COMP200 GENERICS. OOP using Java, from slides by Shayan Javed

COMP200 GENERICS. OOP using Java, from slides by Shayan Javed 1 1 COMP200 GENERICS OOP using Java, from slides by Shayan Javed 2 ArrayList and Java Generics 3 Collection A container object that groups multiple objects 4 Collection A container object that groups multiple

More information

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3.

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3. Linked Lists Walls and Mirrors Chapter 5 Linked List Nodes public class Node { private int item; private Node next; public Node(int item) { this(item,null); public Node(int item, Node next) { setitem(item);

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

Specifications for the ADT List Ali list provides a way to organize data List of students List of sales List of lists

Specifications for the ADT List Ali list provides a way to organize data List of students List of sales List of lists Abstract Data Type List ADT List Specifications for the ADT List Ali list provides a way to organize data List of students List of sales List of lists A list has first, last, and in between items (the

More information

Name CPTR246 Spring '17 (100 total points) Exam 3

Name CPTR246 Spring '17 (100 total points) Exam 3 Name CPTR246 Spring '17 (100 total points) Exam 3 1. Linked Lists Consider the following linked list of integers (sorted from lowest to highest) and the changes described. Make the necessary changes in

More information

CISC 323 Intro to Software Engineering

CISC 323 Intro to Software Engineering CISC 323 Intro to Software Engineering Topic 6: Design Patterns Readings in Custom Courseware, taken from Design Patterns: Elements of Reusable Object-Oriented Software by Gamma, Helm, Johnson, Vlissides

More information

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1 Queues October 20, 2017 Hassan Khosravi / Geoffrey Tien 1 Queue ADT Queue ADT should support at least the first two operations: enqueue insert an item to the back of the queue dequeue remove an item from

More information

Cpt S 122 Data Structures. Data Structures

Cpt S 122 Data Structures. Data Structures Cpt S 122 Data Structures Data Structures Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Self Referential Structures Dynamic Memory Allocation

More information

.:: UNIT 4 ::. STACK AND QUEUE

.:: UNIT 4 ::. STACK AND QUEUE .:: UNIT 4 ::. STACK AND QUEUE 4.1 A stack is a data structure that supports: Push(x) Insert x to the top element in stack Pop Remove the top item from stack A stack is collection of data item arrange

More information

Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Thursday, April 25, 13

Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Thursday, April 25, 13 Queue Implemented with 1 2 numitems = enqueue (); 1 Queue Implemented with 2 numitems = 2 enqueue (); enqueue (); 2 Queue Implemented with 2 numitems = 2 enqueue (); enqueue (); enqueue (5); enqueue (6);

More information

Summer Final Exam Review Session August 5, 2009

Summer Final Exam Review Session August 5, 2009 15-111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding

More information

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front. /3/ Outline Queue? Part Queues Implementing Queue Comparison implementations CS Algorithms and Data Structures "Grill the Buffs" event 9/ 3 Photo by David Jump Queue OperaLons A queue is like a line of

More information

Abstract Data Types - Lists Arrays implementa4on Linked- lists implementa4on

Abstract Data Types - Lists Arrays implementa4on Linked- lists implementa4on Abstract Data Types - Lists Arrays implementa4on Linked- lists implementa4on Lecture 16 Jérôme Waldispühl School of Computer Science McGill University Slides by Mathieu BlancheIe Recap from last lecture

More information

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology Lists and Iterators CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology Announcements Should be making progress on VectorGraphics. Should have turned in CRC cards,

More information

Lists Chapters Linked Lists Abstract Implementations Storage Leaks Ordered Lists

Lists Chapters Linked Lists Abstract Implementations Storage Leaks Ordered Lists Lists Chapters 8-11 Linked Lists Abstract Implementations Storage Leaks Ordered Lists Data Storage Organisation Three basic implementation techniques for linear data collections Files Arrays Linked Lists

More information

Announcements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists.

Announcements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists. Announcements MODULE WEB-SITE: http://www.csc.liv.ac.uk/ michele/teaching/comp102/comp102.html FIRST ASSIGNMENT DEADLINE: Wednesday, February 1st, 14.30, LAB 7 Boxes (late submissions to be left in the

More information

Insertion Sort: an algorithm for sorting an array

Insertion Sort: an algorithm for sorting an array Insertion Sort: an algorithm for sorting an array Let s use arrays to solve a problem that comes up often in programming, namely sorting. Suppose we have an array of objects that is in no particular order

More information

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016 COMP 250 Winter 2016 5 generic types, doubly linked lists Jan. 28, 2016 Java generics In our discussion of linked lists, we concentrated on how to add or remove a node from the front or back of a list.

More information

List ADT. B/W Confirming Pages

List ADT. B/W Confirming Pages wu3399_ch8.qxd //7 :37 Page 98 8 List ADT O b j e c t i v e s After you have read and studied this chapter, you should be able to Describe the key features of the List ADT. the List ADT using an array

More information

JAVA.UTIL.LINKEDLIST CLASS

JAVA.UTIL.LINKEDLIST CLASS JAVA.UTIL.LINKEDLIST CLASS http://www.tutorialspoint.com/java/util/java_util_linkedlist.htm Copyright tutorialspoint.com Introduction The java.util.linkedlist class operations perform we can expect for

More information

CSE 143 Lecture 2. reading:

CSE 143 Lecture 2. reading: CSE 143 Lecture 2 Implementing ArrayIntList reading: 15.1-15.3 slides adapted from Marty Stepp, Hélène Martin, Ethan Apter and Benson Limketkai http://www.cs.washington.edu/143/ Exercise Pretend for a

More information

+ Abstract Data Types

+ Abstract Data Types Linked Lists Abstract Data Types An Abstract Data Type (ADT) is: a set of values a set of operations Sounds familiar, right? I gave a similar definition for a data structure. Abstract Data Types Abstract

More information

Introduction to Software Design

Introduction to Software Design CSI 1102 1 Abdulmotaleb El Saddik University of Ottawa School of Information Technology and Engineering (SITE) Multimedia Communications Research Laboratory (MCRLab) Distributed Collaborative Virtual Environments

More information

Abstract Data Types Spring 2018 Exam Prep 5: February 12, 2018

Abstract Data Types Spring 2018 Exam Prep 5: February 12, 2018 CS 61B Abstract Data Types Spring 2018 Exam Prep 5: February 12, 2018 1 Assorted ADTs A list is an ordered collection, or sequence. 1 interface List { 2 boolean add(e element); 3 void add(int index,

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

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow. Assignment 1 Check your mailbox on Thursday! Grade and feedback published by tomorrow. COMP250: Queues, deques, and doubly-linked lists Lecture 20 Jérôme Waldispühl School of Computer Science McGill University

More information

Data Structures Lecture 5

Data Structures Lecture 5 Fall 2017 Fang Yu Software Security Lab. Dept. Management Information Systems, National Chengchi University Data Structures Lecture 5 Announcement Project Proposal due on Nov. 9 Remember to bring a hardcopy

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

Data Structure: Lists and Iterators. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering

Data Structure: Lists and Iterators. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering Data Structure: Lists and Iterators 2017 Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering Contents Data structures to be covered in this lecture Array lists (aka Vectors) Node lists

More information

10/1/2018 Data Structure & Algorithm. Circularly Linked List Doubly Linked List Priority queues Heaps

10/1/2018 Data Structure & Algorithm. Circularly Linked List Doubly Linked List Priority queues Heaps 10/1/2018 Data Structure & Algorithm Circularly Linked List Doubly Linked List Priority queues Heaps 1 Linked list: Head and Tail NULL Element Next 1. Head 2. Tail 3. Size 2 Make SinglyLinkedList implements

More information

CS61BL: Data Structures & Programming Methodology Summer 2014

CS61BL: Data Structures & Programming Methodology Summer 2014 CS61BL: Data Structures & Programming Methodology Summer 2014 Instructor: Edwin Liao Final Exam August 13, 2014 Name: Student ID Number: Section Time: TA: Course Login: cs61bl-?? Person on Left: Possibly

More information

CS 216 Exam 1 Fall SOLUTION

CS 216 Exam 1 Fall SOLUTION CS 216 Exam 1 Fall 2004 - SOLUTION Name: Lab Section: Email Address: Student ID # This exam is closed note, closed book. You will have an hour and fifty minutes total to complete the exam. You may NOT

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

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage: Discussion 2C Notes (Week 3, January 21) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Abstraction In Homework 1, you were asked to build a class called Bag. Let

More information

Model Solutions. COMP 103: Mid-term Test. 19th of August, 2016

Model Solutions. COMP 103: Mid-term Test. 19th of August, 2016 Family Name:............................. Other Names:............................. ID Number:............................... Signature.................................. Instructions Time allowed: 45 minutes

More information

CS350: Data Structures Linked Lists

CS350: Data Structures Linked Lists Linked Lists James Moscola Department of Physical Sciences York College of Pennsylvania James Moscola Linked Lists Come in a variety of different forms - singly linked lists - doubly linked lists - circular

More information

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. CSC-220 Exam4 on Chapters 18 and 24. Closed Book, Closed Notes, Closed Internet MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) What are the base

More information

COSC This week. List ADT. Abstract Data Type (ADT) Lis t ADT Implementations. Data Struc tures and Algorithms

COSC This week. List ADT. Abstract Data Type (ADT) Lis t ADT Implementations. Data Struc tures and Algorithms This week COSC 1030.03 Week 9, March 8, 2004 Read chapter 19 Work on assignment #3 Discussed in Tutorial this week Must have a partial solution shown to your TA next week. Data structures (continued) S

More information

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal Queues COL 106 Slides by Amit Kumar, Shweta Agrawal The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out (FIFO) scheme Insertions are at the rear

More information

Computer Science 210: Data Structures. Linked lists

Computer Science 210: Data Structures. Linked lists Computer Science 210: Data Structures Linked lists Arrays vs. Linked Lists Weʼve seen arrays: int[] a = new int[10]; a is a chunk of memory of size 10 x sizeof(int) a has a fixed size a[0] a[1] a[2]...

More information

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes. CS231 - Spring 2017 Linked Lists List o Data structure which stores a fixed-size sequential collection of elements of the same type. o We've already seen two ways that you can store data in lists in Java.

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures Computer Science 210 Data Structures Siena College Fall 2018 Topic Notes: Linear Structures The structures we ve seen so far, Vectors/ArrayLists and linked list variations, allow insertion and deletion

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

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

Programming Abstractions

Programming Abstractions Programming Abstractions C S 1 0 6 B Cynthia Lee Topics du Jour: Make your own classes! (cont.) Last time we did a BankAccount class (pretty basic) This time we will do something more like the classes

More information

Department of Computer Science and Engineering. CSE 2011: Fundamentals of Data Structures Winter 2009, Section Z

Department of Computer Science and Engineering. CSE 2011: Fundamentals of Data Structures Winter 2009, Section Z Department of omputer Science and Engineering SE 2011: Fundamentals of Data Structures Winter 2009, Section Z Instructor: N. Vlajic Date: pril 14, 2009 Midterm Examination Instructions: Examination time:

More information

C27a: Stack and Queue

C27a: Stack and Queue CISC 3115 TY3 C27a: Stack and Queue Hui Chen Department of Computer & Information Science CUNY Brooklyn College 11/27/2018 CUNY Brooklyn College 1 Outline Discussed Concept of data structure Use data structures

More information

The time and space are the two measure for efficiency of an algorithm.

The time and space are the two measure for efficiency of an algorithm. There are basically six operations: 5. Sorting: Arranging the elements of list in an order (either ascending or descending). 6. Merging: combining the two list into one list. Algorithm: The time and space

More information

Recap. List Types. List Functionality. ListIterator. Adapter Design Pattern. Department of Computer Science 1

Recap. List Types. List Functionality. ListIterator. Adapter Design Pattern. Department of Computer Science 1 COMP209 Object Oriented Programming Container Classes 3 Mark Hall List Functionality Types List Iterator Adapter design pattern Adapting a LinkedList to a Stack/Queue Map Functionality Hashing Performance

More information

Introduction to Data Structures and Algorithms

Introduction to Data Structures and Algorithms Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can perform operations on these data in an effective way. Data Structures

More information

C Sc 127B Practice Test 2 Section Leader Your Name 100pts

C Sc 127B Practice Test 2 Section Leader Your Name 100pts C Sc 127B Practice Test 2 Section Leader Your Name 100pts Assume we have two collection class named Stack and Queue that have the appropriate messages: Stack public boolean isempty(); public void push(e

More information

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering Readings and References Java Collections "Collections", Java tutorial http://java.sun.com/docs/books/tutorial/collections/index.html CSE 403, Spring 2004 Software Engineering http://www.cs.washington.edu/education/courses/403/04sp/

More information