Programming II (CS300)

Size: px
Start display at page:

Download "Programming II (CS300)"

Transcription

1 1 Programming II (CS300) Chapter 07: Linked Lists MOUNA KACEM Spring 2019

2 Linked Lists 2 Introduction Linked List Abstract Data Type SinglyLinkedList ArrayList Keep in Mind

3 Introduction: General Linked List 3 This chapter introduces how to: Declare the Abstract Data Type (ADT) Linked List Define the most commonly Operations for General Linked Lists Implement a General Linked List The basic Linked List consists of a collection of connected, dynamically allocated nodes or items of the same type linkedlist null item next item next item next

4 Introduction: General Linked List 4 A linked list contains a collection of data items of the same type that are stored in different objects referred as nodes Each node consists of two fields: A data item field : stores or refers to the data value A reference to an object of the same type This reference is used to connect to the next node in the list node linkedlist null item next item next data item Reference to the next node in the list

5 LinkedNode 5 public class LinkedNode<T>{ // Fields private T item; // item data field of any type T private LinkedNode<T> next; // reference to the next node in the list // Constructors... // Methods... } // end generic class LinkedNode node linkedlist null item next item next data item Reference to the next node in the list

6 Linked List Abstract Data Type 6 public interface ListADT<T> { // List of Operations public void add(t newobject); public void add(int index, T newobject); public boolean contains(t findobject); public boolean isempty(); public int size(); public int indexof(t findobject); public T get(int index); public T remove(int index); } // end ListADT generic interface T refers to AnyType

7 7 /** * LinkedList class represents a reference-based implementation of ADT list. <author> */ public class LinkedList<T> implements ListADT<T> { private LinkedNode<T> head; of type<t> public LinkedList<T>() { head = null; } // end default constructor /** // entry point reference to linked list of items * TODO Implementation of the interface ListADT declared methods */ } // end LinkedList class

8 8 public void add(t newobject); // inserts newobject at the end of the linked list 1. Create a NewNode that contains newobject as data field 2. If the list is empty, insert the newnode at the head of the list 3. Otherwise, insert the newnode at the end of the list

9 9 public void add(t newobject); // inserts newobject at the end of the linked list The list is empty 35 newnode: head:

10 10 public void add(t newobject); // inserts newobject at the end of the linked list The list is empty newnode: head: 35

11 11 public void add(t newobject); // inserts newobject at the end of the linked list The list is empty (summary diagram) newnode: 1 3 head: head: 35 2

12 12 public void add(t newobject); // inserts newobject at the end of the linked list List is not empty newnode: newobject: 35 runner: head: runner: a pointer of type LinkedNode<T> used to traverse the list

13 13 public void add(t newobject); // inserts newobject at the end of the linked list List is not empty newnode: 35 runner: head: runner: a pointer of type LinkedNode<T> used to traverse the list

14 14 public void add(t newobject); // inserts newobject at the end of the linked list List is not empty newnode: 35 runner: head: runner: a pointer of type LinkedNode<T> used to traverse the list

15 15 public void add(t newobject); // inserts newobject at the end of the linked list List is not empty newnode: 35 runner: head: runner: a pointer of type LinkedNode<T> used to traverse the list

16 16 public void add(t newobject); // inserts newobject at the end of the linked list List is not empty newnode: 35 runner: head: runner: a pointer of type LinkedNode<T> used to traverse the list

17 17 public void add(t newobject); // inserts newobject at the end of the linked list List is not empty newnode: runner: head: runner: a pointer of type LinkedNode<T> used to traverse the list

18 18 public void add(t newobject); // inserts newobject at the end of the linked list The list is not empty (summary diagram) newnode: Traverse the list runner: 3 2 head: runner: a pointer of type LinkedNode<T> used to traverse the list

19 19 public void add(int index, T newobject); Precondition: 0 index list.size() If index < 0 or index > size() error (throw an exception or display an error message) Otherwise, create a newnode (instance of LinkedNode) that contains the newobject as item data field If index == 0 insert the newnode at the head of the list If index == size() insert the newnode at the end of the list If index > 0 and index < size() insert newnode in the middle of the list at the index position

20 20 public void add(int index, T newobject); Case3: 0 < index < list.size() Index: 3-3 newnode: runner: inx = 0 inx = 1 indices: head: runner initialized to head : a pointer of type LinkedNode<T> used to traverse the list inx an integer initialized to 0 : the index of the node pointed by runner in the list

21 21 public void add(int index, T newobject); Case3: 0 < index < list.size() Index: 3 newnode: -3 indices: runner: inx = 1 inx = head: runner initialized to head : a pointer of type LinkedNode<T> used to traverse the list inx an integer initialized to 0 : the index of the node pointed by runner in the list

22 22 public void add(int index, T newobject); Case3: 0 < index < list.size() Index: 3 inx == index -1 newnode: -3 indices: runner: inx = head: X runner initialized to head : a pointer of type LinkedNode<T> used to traverse the list inx an integer initialized to 0 : the index of the node pointed by runner in the list

23 public void add(int index, T newobject); Case3: 0 < index < list.size() 23 Summary Diagram newnode: Index: Traverse the list until inx = index runner: 4 3 inx = 2 indices: head: X runner initialized to head : a pointer of type LinkedNode<T> used to traverse the list inx an integer initialized to 0 : the index of the node pointed by runner in the list

24 24 public void add(int index, T newobject); Case3: 0 < index < list.size() Index: 3 newnode: runner: indices: head: runner initialized to head : a pointer of type LinkedNode<T> used to traverse the list inx an integer initialized to 0 : the index of the node pointed by runner in the list

25 25 public boolean contains(t findobject); If the list is empty return false Otherwise (list is not empty) define a runner (a reference to a LinkedNode object) to traverse the list and look for the first node whose data item matches with findobject initialize runner to the head of the list traverse the list looking for a much with findobject while runner!= null && there is no match go to the next node in the list if the findobject is found return true if the list is entirely traversed without finding a match, return false runner: a pointer of type LinkedNode<T> used to traverse the list

26 26 List not empty findobject: 10 is runner.getitem().equals(findobject)? No runner: head: runner: a pointer of type LinkedNode<T> used to traverse the list

27 27 List not empty Example findobject: 10 is runner.getitem().equals(findobject)? runner: Yes return true head: runner: a pointer of type LinkedNode<T> used to traverse the list

28 28 public int indexof(t findobject); Initialize index of type int to -1 if list is empty return index Otherwise define a runner (a reference to a LinkedNode object) to traverse the list and look for the first node whose data item matches with findobject initialize runner to the head of the list and increment index traverse the list looking for a much with findobject while runner!= null && there is no match go to the next node in the list and increment index if the findobject is found return index if the list is entirely traversed without finding a match, return -1 runner: a pointer of type LinkedNode<T> used to traverse the list

29 29 public T get(int index); Precondition: 0 index < list.size() if index < 0 or index >= size() error (throw an exception or return null) Otherwise (list is not empty and index in the range of the list indices) define a runner (a reference to a LinkedNode object) to traverse the list looking for the node of index index initialize runner to the head of the list and inx of type int to 0 while inx < index go to the next node in the list (runner = runner.getnext()) and increment inx return runner.getitem() runner: a pointer of type LinkedNode<T> used to traverse the list

30 30 public T remove(int index); Precondition: 0 index < list.size() if index < 0 or index >= size() error (throw an exception or return null) Otherwise (list is not empty and index in the range of the list indices) We distinguish two cases Case1: index == 0 : return the item at the head of the list, then remove the node at the head Case2: Index > 0 and index <= size() -1 : remove the item stored at index position of the list and return it

31 31 public T remove(int index); Case1: List not empty and index == 0 : remove the node at the head of the list 1 2 remove the node at the head of the list 3 item = head.getitem(); head = head.getnext(); return item 1 item: 18 head: X head:

32 32 public T remove(int index); Case3: List not empty and index > 0 and index <= size -1 : remove a node other than the head To remove a node from the list (at a given index not zero), we need a pointer to its previous node Use a runner to reach the node of index index - 1 Initialize runner to head and inx of type int to 0 While inx < index 1, go to the next node (runner = runner.getnext()) and increment inx item = runner.getnext().getitem(); remove the node at the position index the list: runner.setnext(runner.getnext().getnext()); return item head: runner: a pointer of type LinkedNode<T> used to traverse the list

33 33 public T remove(int index); Case2: (Example) List not empty and index > 0 and index == size -1 : remove the node at the end (tail) of the list Index: 4 1 runner: inx = 0 indices: 2 runner: inx = 1 Traverse the list to reach the node of index index-1 2 runner: inx = 2 2 runner: inx = head: X Get the item at runner.getnext() and Remove runner.getnext() Return item 3 item = runner.getnext().getitem(); item: 35 3 runner: a pointer of type LinkedNode<T> used to traverse the list

34 34 public T remove(int index); Case2 (general case): List not empty and index > 0 and index <= size -1 : remove a node at the middle of the list Index: Traverse the list to reach the node of index index-1 3 Get the item at runner.getnext() // remove runner.getnext() runner.setnext(runner.getnext().getnext()) return item runner: inx = 0 indices: runner: 2 3 item = runner.getnext().getitem(); item: 78 inx = head: 18-5 X runner: a pointer of type LinkedNode<T> used to traverse the list 3

35 Practice Examples 35 Declare the ListADT<T> interface Implement the generic LinkedNode<T> class Implement the linked list of Integers class public class LinkedList implements ListADT<Integer> Test the implementation Implement the generic linked list SinglyLinkedList<T> class public class SinglyLinkedList<T> implements ListADT<T> Test the SinglyLinkedList class implementation considering different types T Implement the generic class ArrayList<T> implements ListADT<T>

36 Doubly Linked Lists 36 The singly linked list does not efficiently support some important operations For instance, it is time consuming to go to the end of the list Singly linked lists cannot implement retreat method which allows one backward move at the time while traversing a list A doubly linked list allows bidirectional traversal by storing two links per node Each node now has two links (next and prev) prev node: -5

37 Doubly linked lists 37 Searching and traversing the list can easily be performed in both directions Doubly linked lists allow to move up just as easily as down in the list to insert after as well as before a node at a given position (index) Insertion (add) and removal (remove) operations involve twice as many link changes as for a singly linked list. head: : tail

38 Circular Linked List 38 In a circularly linked list, the last node s next link references the first one in the list (i.e. the head of the list) The circular list is useful when we want searching to allow wraparound head:

39 Keep in Mind 39 A linked list represents a collection (that may be empty) of connected, dynamically allocated nodes (known as linked nodes) A linked node of a singly linked list must contain a reference to a node of the same type. This reference is used to connect to the next node in the list To add a node at the middle of a singly linked list, we need a reference to the previous node We need a reference to the node at the prior position to the one that we want to insert To remove a node from a singly linked list other than the head of the list, we need a reference to the node prior to the node that we want to remove To remove an item or object that we don t know the index, we can make use of the method indexof which returns the position (index) of an item in the linked list

40 Keep in Mind 40 Doubly linked list allows bidirectional traversal of the list A doubly linked node has two links: a reference to the next node and a reference to the previous node in the list Insertion and deletion operations are more easy with doubly linked lists, but ensure more link changes compared to a singly linked list Common error while implementing linked list operations: NullPointerException! Methods should not be allowed to access fields via a null reference

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

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

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

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

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

Array Based Lists. Collections

Array Based Lists. Collections Array Based Lists Reading: RS Chapter 15 1 Collections Data structures stores elements in a manner that makes it easy for a client to work with the elements Specific collections are specialized for particular

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

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List. Implementing a List in Java CSE 143 Java List Implementation Using Arrays Reading: Ch. 13 Two implementation approaches are most commonly used for simple lists: Arrays Linked list Java Interface List concrete

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

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

Introduction to Computer Science II CS S-20 Linked Lists III

Introduction to Computer Science II CS S-20 Linked Lists III Introduction to Computer Science II CS112-2012S-20 Linked Lists III David Galles Department of Computer Science University of San Francisco 20-0: Linked List Previous Practical Example: removeat(int index)

More information

Dynamic Data Structures

Dynamic Data Structures Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To

More information

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015 LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 5 due tonight at midnight -assignment 6 is out -YOU WILL BE SWITCHING PARTNERS! 3 assignment

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

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam CIS 110 Introduction to Computer Programming Spring 2016 Final Exam Name: Recitation # (e.g., 201): Pennkey (e.g., eeaton): My signature below certifies that I have complied with the University of Pennsylvania

More information

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List. Implementing a List in Java CSE 143 Java List Implementation Using Arrays Reading: Ch. 22 Two implementation approaches are most commonly used for simple lists: Arrays Linked list Java Interface List concrete

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

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

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

COMP 250. Lecture 29. interfaces. Nov. 18, 2016 COMP 250 Lecture 29 interfaces Nov. 18, 2016 1 ADT (abstract data type) ADT s specify a set of operations, and allow us to ignore implementation details. Examples: list stack queue binary search tree priority

More information

CSC 1052 Algorithms & Data Structures II: Lists

CSC 1052 Algorithms & Data Structures II: Lists CSC 1052 Algorithms & Data Structures II: Lists Professor Henry Carter Spring 2018 Recap Collections hold and access elements based on content Order and index no longer considered Comparable elements implement

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

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 11: Binary Search Trees MOUNA KACEM mouna@cs.wisc.edu Fall 2018 General Overview of Data Structures 2 Introduction to trees 3 Tree: Important non-linear data structure

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

Spring 2008 Data Structures (CS301) LAB

Spring 2008 Data Structures (CS301) LAB Spring 2008 Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Singly Linked List practically using c++ and adding more functionality in it. o Enabling

More information

CMSC 341 Lecture 7 Lists

CMSC 341 Lecture 7 Lists CMSC 341 Lecture 7 Lists Today s Topics Linked Lists vs Arrays Nodes Using Linked Lists Supporting Actors (member variables) Overview Creation Traversal Deletion UMBC CMSC 341 Lists 2 Linked Lists vs Arrays

More information

Data Structures (CS301) LAB

Data Structures (CS301) LAB Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Doubly Linked List practically using c++ and adding more functionality in it. Introduction to Singly

More information

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion Prelim 1 CS 2110, October 1, 2015, 5:30 PM 0 1 2 3 4 5 Total Question Name True Short Testing Strings Recursion False Answer Max 1 20 36 16 15 12 100 Score Grader The exam is closed book and closed notes.

More information

CS S-20 Linked Lists IV 1

CS S-20 Linked Lists IV 1 CS112-2012S-20 Linked Lists IV 1 20-0: Doubly Linked List Deleting from (and inserting into!) a linked can be challenging because you need to find the node before the node you are looking for Once you

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2015S-05 Abstract Data Types and Lists David Galles Department of Computer Science University of San Francisco 05-0: Abstract Data Types Recall that an Abstract Data

More information

Lists. ordered lists unordered lists indexed lists 9-3

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

More information

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

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

Lists. Chapter 12. Copyright 2012 by Pearson Education, Inc. All rights reserved

Lists. Chapter 12. Copyright 2012 by Pearson Education, Inc. All rights reserved Lists Chapter 12 Contents Specifications for the ADT List Using the ADT List Java Class Library: The Interface List Java Class Library: The Class ArrayList Objectives Describe the ADT list Use the ADT

More information

CS S-05 Abstract Data Types and Lists 1

CS S-05 Abstract Data Types and Lists 1 CS245-2016S-05 Abstract Data Types and Lists 1 05-0: Abstract Data Types Recall that an Abstract Data Type is a definition of a type based on the operations that can be performed on it. An ADT is an interface

More information

Slides are adapted from the originals available at

Slides are adapted from the originals available at C H A P T E R 1 1! Arrays and ArrayLists Little boxes, on a hillside, little boxes made of ticky-tacky Little boxes, little boxes, little boxes, all the same There s a green one and a pink one and a blue

More information

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created.

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created. 1. Because an ArrayList is an indexed collection, you can access its elements using a subscript. 2. The actual object type stored in an object of type CollectionClassName is specified when the object

More information

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists Lecture 6: Linked Lists Object References When you declare a variable of a non-primitive type you are really declaring a reference to that object String

More information

Computer Science 145

Computer Science 145 Name: Computer Science 145 Final Exam Answer Sheet Fall 2016 1. a b c d 8. 2. a b c d 9. a b c d 3. a b c d e 10. a b c d 4. a b c d 11. 5. a b c d 12. 6. 13. 7. 14. a b c d 15. 16. 17. 1 18. 19. 2 20.

More information

Midterm Exam 2 CS 455, Spring 2011

Midterm Exam 2 CS 455, Spring 2011 Name: USC loginid (e.g., ttrojan): Midterm Exam 2 CS 455, Spring 2011 March 31, 2011 There are 6 problems on the exam, with 50 points total available. There are 7 pages to the exam, including this one;

More information

Lecture No.04. Data Structures

Lecture No.04. Data Structures Lecture No.04 Data Structures Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i

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

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

Introduction to Computer Science II CS S-20 Linked Lists IV

Introduction to Computer Science II CS S-20 Linked Lists IV Introduction to Computer Science II CS112-2012S-20 Linked Lists IV David Galles Department of Computer Science University of San Francisco 20-0: Doubly Linked List Deleting from (and inserting into!) a

More information

CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15)

CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15) CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15) A data structure is a software construct used to organize our data in a particular way. Some common data structures include lists,

More information

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S. Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S. Bowers 1 of 11 Doubly Linked Lists Each node has both a next and a prev pointer head \ v1 v2 v3 \ tail struct

More information

Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Introduction to Object-Oriented Programming Linked Lists Christopher Simpkins chris.simpkins@gatech.edu CS 1331 (Georgia Tech) Linked Lists 1 / 13 Linked Lists Dynamic data structures Singly linked lists

More information

Linked Lists. Chapter 4

Linked Lists. Chapter 4 Linked Lists Chapter 4 1 Linked List : Definition Linked List: A collection of data items of the same type that are stored in separate objects referred to as "nodes". Each node contains, in addition to

More information

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

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

More information

Chapter 17: Linked Lists

Chapter 17: Linked Lists Chapter 17: Linked Lists 17.1 Introduction to the Linked List ADT Introduction to the Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures list head

More information

Topic 11 Linked Lists

Topic 11 Linked Lists Topic 11 "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101, a data structures course, and when they hit the pointers business

More information

Data structure is an organization of information, usually in memory, for better algorithm efficiency.

Data structure is an organization of information, usually in memory, for better algorithm efficiency. Lecture 01 Introduction Wednesday, 29 July 2015 12:59 pm Data structure is an organization of information, usually in memory, for better algorithm efficiency. Abstract data types (ADTs) are a model for

More information

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison Linked lists Comp Sci 1575 Data Structures Outline 1 2 3 4 5 Linked list Linked lists are of a linear collection of data elements, called nodes, each pointing to the next node Each node is composed of

More information

03/31/03 Lab 7. Linked Lists

03/31/03 Lab 7. Linked Lists 03/31/03 Lab 7 Lists are a collection of items in which each item has a specific position. The specification for positioning the items provides some rules of order so this data structure is called an ordered

More information

CS Introduction to Data Structures Week 1 Thursday

CS Introduction to Data Structures Week 1 Thursday CS 367 - Introduction to Data Structures Week 1 Thursday We assume that you are proficient at object-oriented programming in Java. Please enroll in CS300 if you do not know Java and have not written object-oriented

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

Outline. iterator review iterator implementation the Java foreach statement testing

Outline. iterator review iterator implementation the Java foreach statement testing Outline iterator review iterator implementation the Java foreach statement testing review: Iterator methods a Java iterator only provides two or three operations: E next(), which returns the next element,

More information

CMPT 225. Lecture 6 linked list

CMPT 225. Lecture 6 linked list CMPT 225 Lecture 6 linked list 1 Last Lecture Class documentation Linked lists and its operations 2 Learning Outcomes At the end of this lecture, a student will be able to: define one of the concrete data

More information

Chapter 17: Linked Lists

Chapter 17: Linked Lists Chapter 17: Linked Lists Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 17.1 Introduction to the

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

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

Dynamic Data Structures and Generics

Dynamic Data Structures and Generics Dynamic Data Structures and Generics Reading: Savitch ch. 12 Objectives Introduce Abstract Data Types (ADTs) and review interfaces Introduce Java's ArrayList class Learn about linked lists and inner classes

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists Prof. amr Goneid, AUC 1 Linked Lists Prof. amr Goneid, AUC 2 Linked Lists The Linked List Structure Some Linked List

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

CS/CE 2336 Computer Science II

CS/CE 2336 Computer Science II 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 What is a Data Structure? A collection of data

More information

CS 307 Final Spring 2008

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

More information

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016 Array Lists Lecture 15 Robb T. Koether Hampden-Sydney College Mon, Feb 22, 2016 Robb T. Koether (Hampden-Sydney College) Array Lists Mon, Feb 22, 2016 1 / 23 1 Inlining Functions 2 List Implementations

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms First Semester 2017/2018 Linked Lists Eng. Anis Nazer Linked List ADT Is a list of nodes Each node has: data (can be any thing, int, char, Person, Point, day,...) link to

More information

Review: List Implementations. CSE 143 Java. Links. A Different Strategy: Lists via Links. Linked Links. CSE143 Au List

Review: List Implementations. CSE 143 Java. Links. A Different Strategy: Lists via Links. Linked Links. CSE143 Au List Review: Implementations CSE 143 Java ed s Reading: Ch. 23 The external interface is already defined Implementation goal: implement methods efficiently Array approach: use an array with extra space internally

More information

2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked

2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked Chapter 6 LISTS AND STRINGS 1. List Specifications 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked 3. Strings 4. Application:

More information

Linked Lists. What Is A Linked List? Example Declarations. An Alternative Collections Data Structure. Head

Linked Lists. What Is A Linked List? Example Declarations. An Alternative Collections Data Structure. Head Linked Lists An Alternative Collections Data Structure What Is A Linked List? A data structure using memory that expands and shrinks as needed Relies on identical nodes pointing or linked to other nodes

More information

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays Unit 3: Linked Lists Part 2: More on Linked Lists 1 Deletion in singly linked lists (cont d) 1 Other Functions Engineering 4892: Data Structures 1 Doubly Linked Lists Faculty of Engineering & Applied Science

More information

Arrays. Array Definition

Arrays. Array Definition Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Arrays Arrays 1 Array Definition An array

More information

Queues. Data Structures and Design with Java and JUnit Rick Mercer 18-1

Queues. Data Structures and Design with Java and JUnit Rick Mercer 18-1 Queues Data Structures and Design with Java and JUnit Rick Mercer 18-1 A Queue ADT First in first out access A Queue is another name for waiting line Example: Submit jobs to a network printer What gets

More information

Week 4, Wednesday (Spring 2015). Dr. Yoder. Sec 051. Page 1 of 5

Week 4, Wednesday (Spring 2015). Dr. Yoder. Sec 051. Page 1 of 5 CS2852 Exam 1 Name: No note-sheets, calculators, or other study aids on this exam. Write your initials at the top of each page except this one. Read through the whole exam before you get started. Have

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

11/2/ Dynamic Data Structures & Generics. Objectives. Array-Based Data Structures: Outline. Harald Gall, Prof. Dr.

11/2/ Dynamic Data Structures & Generics. Objectives. Array-Based Data Structures: Outline. Harald Gall, Prof. Dr. 12. Dynamic Data Structures & Generics Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch Objectives! Define and use an instance of ArrayList! Describe general idea

More information

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018 Array Lists Lecture 15 Robb T. Koether Hampden-Sydney College Fri, Feb 16, 2018 Robb T. Koether (Hampden-Sydney College) Array Lists Fri, Feb 16, 2018 1 / 21 1 Inlining Functions 2 List Implementations

More information

CS S-20 Linked Lists III 1. We can then use the next pointer of the previous node to do removal (example on board)

CS S-20 Linked Lists III 1. We can then use the next pointer of the previous node to do removal (example on board) CS112-2012S-20 Linked Lists III 1 20-0: Linked List ious Practical Example: removeat(int index) remove( o) 20-1: removeat First need to get to node before the one we want to remove We can then use the

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

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

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

More information

The list abstract data type defined a number of operations that all list-like objects ought to implement:

The list abstract data type defined a number of operations that all list-like objects ought to implement: Chapter 7 Polymorphism Previously, we developed two data structures that implemented the list abstract data type: linked lists and array lists. However, these implementations were unsatisfying along two

More information

Linked lists. Yet another Abstract Data Type Provides another method for providing space-efficient storage of data

Linked lists. Yet another Abstract Data Type Provides another method for providing space-efficient storage of data Linked lists One of the classic "linear structures" What are linked lists? Yet another Abstract Data Type Provides another method for providing space-efficient storage of data What do they look like? Linked

More information

2/22/2013 LISTS & TREES

2/22/2013 LISTS & TREES LISTS & TREES Lecture 9 CS2110 Spring 2013 1 List Overview 2 Purpose Maintain an ordered collection of elements (with possible duplication) Common operations Create a list Access elements of a list sequentially

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

Introduction to Computer Science II CS S-18 Linked Lists

Introduction to Computer Science II CS S-18 Linked Lists Introduction to Computer Science II CS112-2012S-18 Linked Lists David Galles Department of Computer Science University of San Francisco 18-0: Linked Lists Linked List node Data Pointer to the next element

More information

COMP 250. inheritance (cont.) interfaces abstract classes

COMP 250. inheritance (cont.) interfaces abstract classes COMP 250 Lecture 31 inheritance (cont.) interfaces abstract classes Nov. 20, 2017 1 https//goo.gl/forms/ymqdaeilt7vxpnzs2 2 class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object

More information

CS32 Discussion Week 3

CS32 Discussion Week 3 CS32 Discussion Week 3 Muhao Chen muhaochen@ucla.edu http://yellowstone.cs.ucla.edu/~muhao/ 1 Outline Doubly Linked List Sorted Linked List Reverse a Linked List 2 Doubly Linked List A linked list where

More information

Circular Linked List Assignment

Circular Linked List Assignment Page 1 of 6 Circular Linked List Assignment Overview A circular linked list is essentially a singly linked list in which the next pointer of the tail node is set to point to the head node of the linked

More information

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC Implementing a List in Java CSC 143 Java List Implementation Using Arrays Updated with Java 5.0 Generics Reading: Ch. 13 Two implementation approaches are most commonly used for simple lists: Arrays Linked

More information

Recursive Objects. Singly Linked List (Part 2)

Recursive Objects. Singly Linked List (Part 2) Recursive Objects Singly Linked List (Part 2) 1 Operations at the head of the list operations at the head of the list require special handling because there is no node before the head node 2 Adding to

More information

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists Advanced Linked Lists Doubly Linked Lists Circular Linked Lists Multi- Linked Lists Review The singly linked list: consists of nodes linked in a single direction. access and traversals begin with the first

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

Arrays. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Arrays. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html 1 Arrays Arrays in Java an array is a container object that holds a fixed number of values of a single type the length of an array is established when the array is created 2 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

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

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

Lists. CITS2200 Data Structures and Algorithms. Topic 9

Lists. CITS2200 Data Structures and Algorithms. Topic 9 CITS2200 Data Structures and Algorithms Topic 9 Lists Why lists? List windows Specification Block representation Singly linked representation Performance comparisons Reading: Lambert and Osborne, Sections

More information

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first Linked Lists Introduction to Linked Lists A data structure representing a Week 8 Gaddis: Chapter 17 CS 5301 Spring 2014 Jill Seaman A series of dynamically allocated nodes chained together in sequence

More information

Model Solutions. COMP 103: Test May, 2013

Model Solutions. COMP 103: Test May, 2013 Family Name:............................. Other Names:............................. ID Number:............................... Signature.................................. Instructions Time allowed: 45 minutes

More information

CMPSCI 187 Midterm 1 (Feb 17, 2016)

CMPSCI 187 Midterm 1 (Feb 17, 2016) CMPSCI 187 Midterm 1 (Feb 17, 2016) Instructions: This is a closed book, closed notes exam. You have 120 minutes to complete the exam. The exam has 10 pages printed on double sides. Be sure to check both

More information

A collision is said to occur when two or more keys hash to the same index location.

A collision is said to occur when two or more keys hash to the same index location. Problem Set #8 Solutions Problem 1-9 points Given the tree below, traverse the tree a. Inorder : a/b c * d*e / f + g b. Preorder: * - / a b c / * d e + f g c. Postorder a b / c d e * f g + / * * - / /

More information