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

Size: px
Start display at page:

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

Transcription

1 Abstract Data Type List

2 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 items has positions) Operations on lists Add new entry at end, or anywhere Remove an item Remove all items Replace an entry Look at any entry Look for an entry of a specific value Count how many entries Check if list is empty, full Display all the entries 2

3 ADT List To specify an ADT list Describe its data Specify the operations ADT list must be considered in general Not necessarily a list of strings 3

4 an interface for the ADT list. After redefine the draft specifications and it is become complete we can starting to build the Java interface for the list ADT as follows: /** an interface for the ADT list. * entries in the list have positions that begin with 0. * / public interface ListInterface { /** Task: adds a new entry to the end of the list.increase i the length by 1 *@ param newentry the object to be added as a new entry *@return true if the addition is successful ( list not full), false otherwise */ public boolean add (Object newentry); /** Task: adds a new entry at a specified position within the list. * The list size increased by 1. *@param newposition an integer that specifies the desired position of the new entry ; *@param newentry the object to be added as a new entry *@return true if the addition is successful (newposition >= 0 and newposition <= * getlength (), and the list not full), or false if not */ public boolean add (int newposition, object newentry); 4

5 an interface for the ADT list. /** Task: removes the entry at a given position from the list. decrease the length by 1 *@param givenposition an integer that indicates the position of the entry to be * removed; *@return either the entry at position givenposition (if the list not empty && the * givenposition >=0 && the givenposition< getlength()), otherwise return null */ public Object remove (int givenposition); /** Task: Removes all entries from the list. */ public void clear(); /** Task: replaces the entry at a given position in the list. *@param givenposition an integer that indicates the position of the entry to be * replaced; *@param newentry the object that t will replace the entry at the position givenposition iti *@return true if the replacement occurs (givenposition >= 0 and givenposition < * getlength() and the list not empty), or false if either the list was empty * or givenposition is invalid */ public boolean replace (int givenposition, Object newentry); 5

6 an interface for the ADT list. /** Task: retrieves the entry at a given position in the list. *@param givenposition an integer that indicates the position of the desired entry; *@return a reference to the indicated list entry, if (the list not empty and * givenposition >= 0 and givenposition < getlength() and the entry found, * otherwise returns null */ public Object getentry (int givenposition); /** Task: determines whether the list contains a given entry. *@param anentry the object that t is the desired d entry *@return true if (the list not empty and the list contains anentry), or false if not */ public boolean contains (Object anentry); /** Task: gets the length of the list. *@return the integer number of entries currently in the list */ public int getlength(); 6

7 an interface for the ADT list. /** Task: determines whether the list is empty. py true if the list is empty, or false if not */ public boolean isempty(); /** Task: determines whether the list is full. true if the list is full, or false if not */ public boolean isfull(); /** Task: displays all entries that are in the list, one per line, in the order in which * they occur in the list. */ public void display(); }// end ListInterface 7

8 LIST Implementations List ADT can be implemented using: a Fixed-Size Array implementation ti Dynamic Array Expansion implementation a Vector implementation Linked-based implementation 8

9 Using Linked based List ADT Implementation Linked list is a linear data structure of objects (items), each item is called a node. Each node contains two fields: a reference to an entry in the list (information field, data portion ) a reference to another node in the chain ( next field that points to the next node, link portion) 9

10 logical view of linked list NODE External pointer Data Link portion Null pointer portion /next (last node (internal pointer) 10

11 logical view of linked list firstnode (head reference ): is an external reference that points to the first node of the list. Next: is an internal reference that points to the next node The next portion of the last node of the list has a value of null (don t points to any thing). 11

12 Example Two linked nodes with : (a) primitive data; (b) object data 12

13 A Linked Implementation of the ADT List Use a chain of nodes Remember the address of the first node in the chain Record a reference to the first node The head reference The implementation contains the class Node as an inner class 13

14 A Linked Implementation of the ADT List We can use the concept of performing chains, in order to implement our ListInterfase with its all methods. add new entry (new node ) in the list - case 1: at the beginning i - case 2 : at the middle - case 3: at the end 14

15 A Linked Implementation of the ADT List Remove an entry (node) from the List Case 1: at the beginning Case 2: at the middle Case 3: at the end Method replace Method getentry Method contains.and Remaining methods 15

16 add new entry (new node ) in the list Case1 at the beginning need: - new node - first node reference Process: - get new node - store data - connect: - newnode.next=firstnode - firstnode=newnode 2 null 1 16

17 add new entry (new node ) in the list Case2 at the middle need: - new node - previous node reference - after node Process: - get new node - store data - get previous node - previousnode= getnodeat(pos -1) - get after node - afternode= previousnode.next - connect newnode.next=afternode PreviousNode.next=newNode 17

18 add new entry (new node ) in the list Case 3: at the end In this case we can use two approaches: 1. the same process As case Get a reference to the last node as part of the implementation class ( will be explained later on). 1. as case 2: 18

19 Remove an entry (node) from the List Case 1: at the beginning i Process: get data stored in the firstnode Result = firstnode.data reconnect firstnode = firstnode.next. 19

20 Remove an entry (node) from the List Case 2: at the middle Process: get node before the deleted node beforenode=getnodeat(pos-1) get node to be removed nodetoremove=beforenode.next get node after deleted node afternode=nodetoremove.next reconnect beforenode.next=afternode 20

21 Remove an entry (node) from the List Case 2: at the middle 21

22 Remove an entry (node) from the List Case 3: at the end In this case, what we need is to get the reference to the node which is before the last node, so we can use the same process as case 2 to implement this case. 22

23 Replace operation Process: Get a reference to the desired node, where the old data of this node will be replaced by new data. desirednode dn d = getnodeat(position); Store the new data in the data portion of the desired node. desirednode.data = newentry 23

24 Get an entry from the list Process Get a reference to the desired node, where the data of this node will be returned to the user. desirednode = getnodeat(position); Get the data portion of this node result = desirednode.data; ed data; Return the result return result 24

25 Contains operation Process: Get a referance to the firstnode currentnode= firstnode; Using this reference (currentnode) to traverse all nodes of the list with equality comparison While (not found && not reach the end of the list(currentnode!= null )) { compare and If found return true else proceeds to the next node (currentnode = currentnode.next) The above repetition continuing until found or reach the end of the list and not found in this case return false. 25

26 Is full, is empty and get length operations isfull(): always return fall as no limitations on the list size {return false;} isempty(): if the first node has a null value means that the list hasn t any node (the list empty { return firstnode == null;} getlength(): returns the value of the counter that holds the number of nodes in the list { return length;} 26

27 Node Class It is implementation detail of the ADT list that should be hidden from the client: in a package with the implementation class inner private class (the data fields of inner class are accessible directly by the outer class) 27

28 The class Node as an inner class (private Class) private class Node{ private Object data; // data portion private Node next; // link to next node private Node(Object dataportion){ data = dataportion; next = null; } // end constructor t private Node(Object dataportion, node nextnode){ data = dataportion; next = nextnode; } // end constructor } // end node 28

29 the implementation class public class LList implements ListInterface{ private Node firstnode; // reference to first node private int length; // number of entries in list public Llist(){ clear(); } // end default constructor <<private class Node>> // as an Inner class goes here 29

30 implementation continue // private! /** Task: returns a reference to the node at a given position. precondition: list is not empty; 0 <= givenposition < length. */ private Node getnodeat(int givenposition){ node currntnode=firstnode; // traverse the list to locate the desired node for (int i =0 ; i< givenposition ; i++) currentnode=currentnode.next; t d t return currentnode; } // end getnodeat 30

31 implementation continue public boolean isfull() /*always false. no limit. The system can only produce an error*/ { return false; } public int getlength() { return length; } public boolean isempty() //like ArList { return length == 0; } public final void clear() { firstnode = null; length=0; } 31

32 implementation continue public boolean add (Object newentry){ Node newnode = new Node(newEntry); if (isempty()) firstnode = newnode; else { // add to end of nonempty list Node lastnode = getnodeat (length - 1); lastnode.next = newnode; // make last node reference new node } // end if length++; return true; } // end add 32

33 implementation continue public boolean add (int newposition, Object newentry){ boolean issuccessful = true; if ((newposition >= 0) && (newposition <= length)){ Node newnode = new Node(newEntry); if (isempty() (newposition= = 0)) { // case 1 newnode.next = firstnode; firstnode = newnode; } else { // case 2 and 3: newposition >= 1, list is not empty Node prevnode = getnodeat (newposition -1); Node nodeafter = prevnode.next; newnode.next = afternode; prevnode.next= newnode; } // end second if length ++; } // end first if else issuccessful = false; return issuccessful; }// end add 33

34 implementation continue public Object remove (int givenposition){ object result = null; // returned value if (!isempty() && (givenposition >= 0) && (givenposition <length)){ if (givenposition = = 0) { // case 1: remove first entry result = firstnode.data; d // save data of entry before removing firstnode = firstnode.next; } else { // case 2 and 3: givenposition >= 1 Node prevnode = getnodeat (givenposition -1); Node nodetoremove = prevnode.next; Node afternode = nodetoremove.next; prevnode.next = afternode; // disconnect the node to be removed result = nodetoremove.data; // save data of entry before removing } // end second if length --; } // end first if return result; // return data of removed entry, or null if operation fails } // end remove 34

35 implementation continue public boolean replace (int givenposition, Object newentry){ boolean issuccessful = true; if(!isempty() () && (givenposition >= 0) && (givenposition <length)) { Node desirednode = getnodeat (givenposition); desirednode.data = newentry; } else issuccessful = false; return issuccessful; } // end replace 35

36 implementation continue public Object getentry (int givenposition) { Object result = null; // result to return if (!isempty() () && (givenposition>= iti 0) &&( (givenposition iti < length)) result = getnodeat(givenposition).data; return result; } // end getentry public void display() { Node currentnode = firstnode; for( int i = 0; i <length; i++) { System.out.println(currentNode.data); t e t ode data); currentnoden = currentnode.next; } } // end display 36

37 implementation continue public boolean contains(object anentry){ boolean found = false; Node currentnode= firstnode; while (!found && (currentnode!= null)){ if (anentry. equals(currentnode.data)) found = true; else currentnode = currentnode.next; } // end while return found; } // end contains } // end of LList class 37

38 Linked-based implementation of ADT List with Tail References Consider a set of data where we repeatedly add data to the end of the list Each time the getnodeat method must traverse the whole list This is inefficient Solution: maintain a pointer that always keeps track of the end of the chain The tail reference A linked chain with a head and tail reference When adding to an empty list Both head and tail references must point to the new solitary node When adding to a non empty list No more need to traverse to the end of the list lastnode points to it Adjust lastnode.next in new node and lastnode 38

39 Adding a node to the end of a nonempty chain that has a tail reference 39

40 Removing a node from a chain that has both head and dtail references when the chain contains (a) one node; (b) more than one node 40

41 the implementation pe e tato cass class with tail reference e e The code is written only for methods that have been changed. public class LList implements ListInterface{ private Node firstnode; // refrence to first node private node lastnode; private int length; // number of entries in list public Llist(){ clear(); } // end default constructor Note: the underlined text shows the difference at the previous implementation 41

42 implementation continue public Boolean add (Object newentry){ Node newnode = new Node(newEntry); if (isempty()) { firstnode = newnode; } else { // add to end of nonempty list // Node lastnode = getnodeat(length g -1); omitted lastnode.next = newnode; // make last node reference new node } // end if lastnode =newnode; length++; return true; } // end add Note: the underlined text shows the difference at the previous implementation ti 42

43 public Boolean add(int newposition, Object newentry){ boolean issuccessful = true; if ((newposition >= 0) && (newposition <= length)){ Node newnode = new Node(newEntry); If (isempty){ // If (isempty()) (newposition= = 0)) { // case 1 //NewNode.next = firstnode; omitted implementation continue 43 FirstNode = newnode; lastnod=newnode;} Else if(newposition= = 0){ newnode.next=firstnode; firstnode=newnode; } else if (newposition==length){ lastnode.next=newnoe; lastnode=newnode;} else { // case 2: newposition > 0 and <length, list is not empty Node prevnode = getnodeat(newposition-1); Node after = prevnode.next; newnode.next = afternode; prevnode.next= newnode; } // end if length++;} else issuccessful = false; return issuccessful; } // end add Note: the underlined text shows the difference at the previous implementation

44 implementation continue public Object remove(int givenposition){ Object result = null; // returned value if f( (!isempty()&&( (givenposition >= 0) && (givenposition <length)){ if (givenposition = = 0) { // case 1: remove first entry result = firstnode.data; // save entry to be removed firstnode = firstnode.next; if(legth ==1) lastnode=null; // the list become empty } else { // case 2 and 3: givenposition iti > 0 Node prevnode = getnodeat(givenposition-1); Node nodetoremove = prevnode.next; Node afternode = nodetoremove.next; prevnode.next = afternode; // disconnect the node to be removed result = nodetoremove.data; //save entry to be removed if(givenposition== length-1); lastnode = prevnode; Note: the underlined text } // end if shows the difference at the length - -; previous implementation } // end if return result; // return removed entry, or null } // end remove // if operation fails 44

45 Pros and Cons of a Chain for an ADT List The chain (list) can grow as large as necessary (advantage) Can add and remove nodes without shifting existing entries (advantage) But Must traverse a chain to determine where to make addition/deletion (disadvantage) Retrieving an entry requires traversal As opposed to direct access in an array (disadvantage) 45

46 Java Class Library: The Class LinkedList The standard java package java.util contains the class LiknkedList This class implements the interface List Contains additional methods addfirst() addlast() removefirst() removelast() getfirst() getlast() 46

University of Petra Faculty of Information Technology

University of Petra Faculty of Information Technology University of Petra Faculty of Information Technology LIST Implementation using Linked DATA Dr. techn. Dipl. Inform. Bassam Haddad Associate Professor of Computer Science Faculty of Information Technology

More information

A List Implementation That Links Data

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

More information

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

University of Petra LIST-I PART II. Specifications for the ADT List

University of Petra LIST-I PART II. Specifications for the ADT List University of Petra Specifications for the ADT List Faculty of Information Technology Dr techn Dr. techn. Dipl Dipl. Inform Inform. Bassam Haddad Associate Professor of Computer Science Faculty of Information

More information

Linked Bag and List Implementations

Linked Bag and List Implementations Lab 6 Linked Bag and List Implementations Goal Having constructed array-based classes to represent Bags and Lists in the last lab, we are now going to repeat what we did last week, only using a chain of

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

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

University of Petra Faculty of Information Technology

University of Petra Faculty of Information Technology University of Petra Faculty of Information Technology Measuring Algorithm Efficiency Dr. techn. Dipl. Inform. Bassam Haddad d Associate Professor of Computer Science Faculty of Information Technology Petra

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

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

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

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

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Sorted Lists Kostas Alexis The ADT Sorted List If your application is using a list in a some phase you want to order its elements in a certain way (e.g., numerically)

More information

Introduction to Linked Data Structures

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

More information

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1 CS200: Queues Prichard Ch. 8 CS200 - Queues 1 Queues First In First Out (FIFO) structure Imagine a checkout line So removing and adding are done from opposite ends of structure. 1 2 3 4 5 add to tail (back),

More information

Ch6. Linear Lists Linked Representation

Ch6. Linear Lists Linked Representation Ch6. Linear Lists Linked Representation copyright 2006 Bird s-eye View Ch.5 ~ Ch.7: Linear List Ch. 5 array representation Ch. 6 linked representation Singly Linked Lists and Chains Circular Lists and

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

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

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

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

Iterators. Chapter 15. Copyright 2012 by Pearson Education, Inc. All rights reserved

Iterators. Chapter 15. Copyright 2012 by Pearson Education, Inc. All rights reserved Iterators Chapter 15 Contents What Is an Iterator? The Interface Iterator Using the Interface Iterator A Separate Class Iterator An Inner Class Iterator A Linked Implementation An Array-Based Implementation

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

Abstract Data Types - Lists Arrays implementation Linked-lists implementation

Abstract Data Types - Lists Arrays implementation Linked-lists implementation Abstract Data Types - Lists Arrays implementation Linked-lists implementation Lecture 17 Jérôme Waldispühl School of Computer Science McGill University From slides by Mathieu Blanchette Abstract data types

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

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

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

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

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

ADT Implementation Array

ADT Implementation Array ADT Implementation Array Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131 1 Outline ADT Implementation using fixed size Array

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

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS LECTURE 5 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 In Lecture 4... Linked Lists Singly Linked Lists Doubly Linked Lists Today Algorithmic problems using Linked Lists

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Implementation Kostas Alexis s Definition: A (BST) is a binary tree where each node has a Comparable key (and an associated value) and satisfies the restriction

More information

CSC 273 Data Structures

CSC 273 Data Structures CSC 273 Data Structures Lecture 7 - Queues, Deques, and Priority Queues The ADT Queue A queue is another name for a waiting line Used within operating systems and to simulate real-world events Come into

More information

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved Stacks Chapter 5 Copyright 2012 by Pearson Education, Inc. All rights reserved Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions A Problem Solved: Checking for Balanced

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

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

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

DATA STRUCTURES AND ALGORITHMS LECTURE 5 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 In Lecture 4... Linked Lists Binary Heap Singly Linked List Insert at the beginning Insert after a node Today Linked

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

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues By: Pramod Parajuli, Department of Computer Science, St. Xavier

More information

CSCD 326 Data Structures I Queues

CSCD 326 Data Structures I Queues CSCD 326 Data Structures I Queues 1 Linked List Queue Implementation Diagram Front Back A B C D 0 6 public interface QueueInterface { Queue Interface public boolean isempty(); // Determines whether

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

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

Implementing Linked Lists

Implementing Linked Lists Implementing Linked Lists Lecture 16 Sections 17.1-17.3 Robb T. Koether Hampden-Sydney College Wed, Feb 27, 2013 Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 27, 2013 1 /

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

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

Array based Bag and List Implementations

Array based Bag and List Implementations Lab 5 Array based Bag and List Implementations Goal Having worked with Bags and Lists in previous labs, we are now going to pull the covers off them and learn how they are constructed and how they work

More information

ADTs, Arrays, and Linked-Lists

ADTs, Arrays, and Linked-Lists ADTs, Arrays, and Linked-Lists EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Abstract Data Type (ADT) abstract implementation details are not specified! Abstract Data Types (ADTs)

More information

Lecture 7: Implementing Lists, Version 2

Lecture 7: Implementing Lists, Version 2 CS18 Integrated Introduction to Computer Science Fisler, Nelson Lecture 7: Implementing Lists, Version 2 Contents 1 The Impact of addfirst on Lists 1 2 Mutating List Contents 2 2.1 The Basic List Classes...................................

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

Queues. CITS2200 Data Structures and Algorithms. Topic 5

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

More information

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

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

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

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

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Queues and Priority Queues Kostas Alexis Implementations of the ADT Queue Like stacks, queues can have Array-based or Link-based implementation Can also use implementation

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

Linked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Linked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I College of Computing & Information Technology King Abdulaziz University CPCS-204 Data Structures I What are they? Abstraction of a list: i.e. a sequence of nodes in which each node is linked to the node

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

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

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

Tutorial 2: Linked Lists

Tutorial 2: Linked Lists Tutorial 2: Linked Lists ? 2 Agenda Introduction of Linked List Arrays Vs Linked Lists Types of Linked Lists Singly Linked List Doubly Linked List Circular Linked List Operations Insertion Deletion 3 Data

More information

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/30/03 Lecture 7 1

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/30/03 Lecture 7 1 Abstract Data Types Add Client Prog Rem Find Data Str. Show 01/30/03 Lecture 7 1 Linear Lists It is an ordered collection of elements. Lists have items, size or length. Elements may have an index. Main

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

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

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

Data Abstraction and Abstract Data Types

Data Abstraction and Abstract Data Types Data Abstraction and Abstract Data Types Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131 1 Outline Abstract Data types (ADT)

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

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

Lists, Stacks, and Queues

Lists, Stacks, and Queues Unit 8, Part 2 Lists, Stacks, and Queues Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Representing a Sequence: Arrays vs. Linked Lists Sequence an ordered collection of items (position

More information

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

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

More information

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

Positional Sequences Section 4.2

Positional Sequences Section 4.2 Positional Sequences Section 4.2 PS-1 Problem with Ranked Sequences Problem with ranked sequences» rank of a particular element may change as other elements are inserted and deleted For example if you

More information

CS302 Data Structures using C++

CS302 Data Structures using C++ CS302 Data Structures using C++ Midterm Exam Instructor: Dr. Kostas Alexis Teaching Assistants: Shehryar Khattak, Mustafa Solmaz Semester: Fall 2018 Date: November 7 2018 Student First Name Student Last

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

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

Problem 1: Building and testing your own linked indexed list

Problem 1: Building and testing your own linked indexed list CSCI 200 Lab 8 Implementing a Linked Indexed List In this lab, you will be constructing a linked indexed list. You ll then use your list to build and test a new linked queue implementation. Objectives:

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

CS2210 Data Structures and Algorithms

CS2210 Data Structures and Algorithms CS2210 Data Structures and Algorithms Lecture 3: ADT and Java interfaces Instructor: Olga Veksler 2004 Goodrich, Tamassia Outline Review Data Structures Abstract Data Types 2 Principles of OO Programming

More information

Bags. Chapter 1. Copyright 2012 by Pearson Education, Inc. All rights reserved

Bags. Chapter 1. Copyright 2012 by Pearson Education, Inc. All rights reserved Bags Chapter 1 Copyright 2012 by Pearson Education, Inc. All rights reserved A little more about Lab 1 to take us into today's topics Carrano, Data Structures and Abstractions with Java, Second Edition,

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

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

CSC 273 Data Structures

CSC 273 Data Structures CSC 273 Data Structures Lecture 3- Stacks Some familiar stacks What is a stack? Add item on top of stack Remove item that is topmost Last In, First Out LIFO Specifications of the ADT Stack Specifications

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

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

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

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

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

More information

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

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

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

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

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

ADT Unsorted List. Outline

ADT Unsorted List. Outline Chapter 3 ADT Unsorted List Fall 2017 Yanjun Li CS2200 1 Outline Abstract Data Type Unsorted List Array-based Implementation Linked Implementation Comparison Fall 2017 Yanjun Li CS2200 2 struct NodeType

More information

Bag Implementations that Use Arrays

Bag Implementations that Use Arrays Bag Implementations that Use Arrays Chapter 2 Contents Using a Fixed-Size Array to Implement the ADT Bag An Analogy A Group of Core Methods Implementing the Core Methods Testing the Core Methods Implementing

More information

Operations on Singly (Simply) Linked Lists

Operations on Singly (Simply) Linked Lists LEC. 4 College of Information Technology / Software Deartment.. Data Structures / Second Class / 2016-2017 InsertFirst Oerations on Singly (Simly) Linked Lists The insertfirst() method of LinkList inserts

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

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

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

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1 Topic #9: Collections CSE 413, Autumn 2004 Programming Languages http://www.cs.washington.edu/education/courses/413/04au/ If S is a subtype of T, what is S permitted to do with the methods of T? Typing

More information