1/27 COP 3540 Data Structures with OOP Chapter 5 Linked Lists.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Chapter 22 Implementing lists: linked implementations.
Linked Lists.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 3: Linked List Tutor: Angie Hui
Lecture 15 Linked Lists part 2
Chapter 7: Arrays In this chapter, you will learn about
Pointers and Data Structures 1 Yinzhi Cao Modified from slides made by Prof Goce Trajcevski.
Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of.
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
Linked Lists: deleting...
Linked Lists Chapter 4.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Linear Lists – Linked List Representation
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 24 Lists, Stacks, and Queues
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
11 Data Structures Foundations of Computer Science ã Cengage Learning.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
ADTs unsorted List and Sorted List
Data Structures Using C++
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping.
LIST PROCESSING.
Double-Linked Lists and Circular Lists
John Hurley Cal State LA
CHP-5 LinkedList.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
CSCI2100B Linked List Jeffrey
CSE Lecture 12 – Linked Lists …
Hash Tables.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
© Paradigm Publishing, Inc Access 2010 Level 1 Unit 1Creating Tables and Queries Chapter 2Creating Relationships between Tables.
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
Review Pseudo Code Basic elements of Pseudo code
Linked Lists.
Linked Lists.
The List ADT Textbook Sections
Backup Slides. An Example of Hash Function Implementation struct MyStruct { string str; string item; };
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
1 COP 3538 Data Structures with OOP Chapter 8 - Part 2 Binary Trees.
1/26 COP 3540 Data Structures with OOP Chapter 5 - Part 2 Linked Lists Abstract Data Types and Sorted Lists.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Introduction to Data Structures and Algorithms
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
LINKED LISTS.
Linked List First next Data Linked List Link next Data Link next Data Link next Data Link Null.
COP 3538 Data Structures with OOP
Notes on Assignment 1 Your code will have several classes, most notably the class that represents the entire list data structure, and the class.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Presentation transcript:

1/27 COP 3540 Data Structures with OOP Chapter 5 Linked Lists

2/27 Different Storage Structure Arrays: Some real advantages and disadvantages depending on whether or not the array was sorted AND what you wanted to do Insert Delete Search… Second most commonly used data storage structure after arrays: the Linked List. Extremely versatile. Will discuss strengths and weaknesses

3/27 Links In a linked list, each data item is embedded in a link. (I sometimes call these nodes or cells). (You may not use the link in the API for link operations in your program assignments.) Each link object contains data – (any kind; any amount) and a reference (points to the next item in the list).

4/27 Links. Consider: class Link { public int iData //some data public double dData //more data public Link next; // reference to next link } // end class definition This kind of class definition is called self- referential, because it contains a link that points to the next node that is of the same type as itself. We can have many data items in a link. But the key is the next reference to the next link.

5/27 Basic Types Note: next is only a reference to the next link. Next has the address of the next object or null. For next to have meaning, there must be an object created and its address placed intonext.

6/27 References In an array, we merely add one (1) to access the next physical item. In a linked-list, we follow a chain of links – from one link to another to find a desired link. It is unlikely you have the direct access of a specific link to directly access (although processing linked lists does allow you to save off addresses, if you wish…)

7/27 A Simple Linked List We will insert items and delete items at the beginning of the list and display items in a linked list. Heres the Link Class class Link { public intiData; public double dData; Lets make these public for now… public Link next; // public Link (int id, double dd)// Constructor { iData = id; dData = dd; } // end Constructor // public void displayLink() { System.out.print({ + iData +, + dData + } ); } // end displayLink() } // end class Link Clearly the Constructor builds the data in the link (but not the next field) and the display() prints it out. Why does the Constructor not build the next field??

8/27 The LinkList Class Special class. Note: your authors call this class, LinkList. Only contains the address of the first element in the linked list. Class LinkList { private Link first; // reference to an object (special object) of type Link. Name is first. // public void LinkList() // Constructor { first = null; // Can see the value of first is null – until we start building links. // It doesnt point to anything yet. } // end constructor public boolean isEmpty() { return (first==null); } // end isEmpty() // other methods. } // end LinkList

9/27 The insertFirst Method We are now going to build and insert nodes into an existing linked list. See next slide: First points to next link (value of 42) which points to next link (value of 7), etc. Last link points to null. Now insert first() a new Link w/value of 33 at first part of the Linked List which then points to the previous first link, who has value of 42… Consider the drawing and then insertfirst():

10/ null Linked List – already built First null rear First rear Identify the parts: links, next, values, first, … Linked List Data Next (link) Insert a node into the linked list.

11/27 insertFirst () Be sure to understand what is going on here Accomplish this with an insertFirst() method: public void insertFirst (int id, double dd) { Link newLink = new Link (id,dd); // creates a new object with initial data values. newLink.next = first; // newLink old first // new link now points to what used to be the first link first = newLink; // Now, first points to the new link we just inserted. first newLink } // end insertFirst() means: links to or points to or references Lets look at all the necessary classes and objects… class Link, class LinkedList and class LinkedListApp. (Think of this design like State, StateDriver or StateArr, and the Main app.)

12/27 class Link (Note: this is only the link (node) itself. The object….) { public int iData; // data item public double dData; // data item public Link next; // next link in list note: self-referential // public Link(int id, double dd) // constructor (Note only the data is built here). // Handling next was handled by the client. { iData = id; // initialize data Notice this only dData = dd; // ('next' is automatically set to null) } public void displayLink() // display yourself { System.out.print("{" + iData + ", " + dData + "} "); } } // end class Link (Like an entity class….. It is the data item – has data and pointer to next data…) The Link class

13/27 class LinkList { private Link first; // ref to first link on list public LinkList() // constructor { first = null; // no links on list yet } public boolean isEmpty() // true if list is empty { return (first==null); } public void insertFirst(int id, double dd) // make new link // insert at start of list {Link newLink = new Link(id, dd); // creates new Link with data. newLink.next = first; // newLink --> old first // next pointer in this new links points to null. How can it access next?? first = newLink; // first --> newLink// first now points to this new link } public Link deleteFirst() // deletes first item // (assumes list not empty) {Link temp = first; // save reference to link first = first.next; // delete it: first-->old next return temp; // return deleted link } public void displayList() { System.out.print("List (first-->last): ");// Note: current is a reference to a node. It contains an address! Link current = first; // start at beginning of list //Note: we set up a current link which has a value of first while(current != null) // until end of list, // As long as current is not = null, there is an additional Link to display. { current.displayLink(); // print data// current is the identity of the current link. current = current.next; // move to next link// set new current to the current of the current link for looping. } System.out.println(""); } } // end class LinkList The Linked List

14/27 class LinkListApp { public static void main(String[] args) { LinkList theList = new LinkList(); // make new list; but establishes first link & null. theList.insertFirst(22, 2.99); // insert four items theList.insertFirst(44, 4.99); theList.insertFirst(66, 6.99);SEE NEXT PAGE. theList.insertFirst(88, 8.99); theList.displayList(); // display list while( !theList.isEmpty() ) // until it's empty, { Link aLink = theList.deleteFirst(); // delete link What is aLink? Used for?? System.out.print("Deleted "); // display it aLink.displayLink(); // display the link you deleted. System.out.println(""); } theList.displayList(); // display the remaining linked list } // end main() } // end class LinkListApp The Linked List Application (driver…)

15/27 null 22; 2.99 first null 22; 2.99 first null 22; 2.99 first ; 6.99 (This pointer moved to forward pointer (next) in link) Conceptual: Linked List

16/27 null 22; 2.99 first null 22; 2.99 first null 22; 2.99 first ; 6.99 (This pointer moved to forward pointer (next) in link) Using Memory Addresses 5A4 5AC 6B4 Sequence of actions below for the links is critical: Allocate new link; fill in data items; Move first to newlink.next. Move address of newLink to first. (The 5A4 represents three hexadecimal digits (12 bits) that symbolizes the last 12 bits of a real 24-bit memory address)

17/27 Finding and Deleting Specified Links Here, we will only add methods to find() and delete() specific links.

18/27... public Link find(int key) // find link with given key (assumes non-empty list) // start at 'first' { Link current = first; // current is a reference to a Link and points to first. while(current.iData != key) // while no match, { if(current.next == null) // if end of list, didn't find it return null; else // not end of list, go to next link current = current.next; }// end while return current; // found it; returns a pointer to the link. }// end find() Add find() routine in LinkList Just adding another method to this object. This method has access to all the instance variables of an object.

19/27 public Link delete(int key) // delete link with given key // (assumes non-empty list) { Link current = first; // search for link Start at head of list. Link previous = first; // Note!! For the delete(), we need to hang on to previous! while(current.iData != key) { if(current.next == null) return null; // didn't find it else { previous = current; // go to next link // Need to save fwd pointer of current cell before current = current.next; // advancing to next one. } // end else } // end while // if we fall through, weve found the item to delete. if(current == first) // if first link is the hit right off the bat! first = first.next; // change first // if first link, move fwd link (first.next) to first (value of current) else // otherwise, previous.next = current.next; // bypass it // move fwd link of current link (one to delete) to return current;// fwd link of previous link to bypass link to be deleted. }// end delete() // public void displayList() // display the list //no changes here. Add delete() routine in LinkList Know This! Can you draw this??

20/27 class LinkList2App { public static void main(String[] args) { LinkList theList = new LinkList(); // make list theList.insertFirst(22, 2.99); // insert 4 items theList.insertFirst(44, 4.99); theList.insertFirst(66, 6.99); theList.insertFirst(88, 8.99); theList.displayList(); // display list Link f = theList.find(44); // find item Requires an integer argument here in this context.. // Creates a pointer to a link into which the hit is moved, if hit is true. if( f != null) System.out.println("Found link with key " + f.iData); // print out data, if good hit. else System.out.println("Can't find link"); Added Link d = theList.delete(66); // delete item// delete returns a reference, as usual. if( d != null ) System.out.println("Deleted link with key " + d.iData); // print out item deleted. else System.out.println("Can't delete link"); theList.displayList(); // display list// After all is said and done, redisplay list. } // end main() } // end class LinkList2App Main Application (client) for LinkList

21/27 Pictures for the Soul… Memory Addresses null 22; 2.99 first ; A4 5AC 6B4 Delete: say looking for the link with value of 44.99: null 22; 2.99 first ; A4 5AC 6B4 5A4 Searching first making 6B4 current; then 5AC current and then, Bingo! We want to delete link at 5AC: Link at 5AC is logically deleted. logically deleted? What does that mean?? ALGORITHM: Current = first; Is current.id = search key? If yes, return current (a reference) No. Hold current in previous Move current.next to new current. Loop. Continue until not found or hit. When hit, move current.next to previous.next. Return current to client. Logically deleted link!

22/27 Double-Ended Lists (not doubly-linked list) Actually, I find this more useful in MANY situations. It allows you to go directly to the end of the list and add a link at the end of the list rather than search the entire list at times to insert at the end… Note: sometimes inserting at the front is oftentimes NOT what we want to do. Concept is quite simple: We have two references: First Last These in turn point to their respective link.

23/27 Pictures: null 22; 2.99 first ; A4 5AC 6B4 Last 5A4 null 22; 2.99 first ; 6.99 Equivalently, Last Unfortunately, one can still only go forward through the linked list… Note: What we have been discussing is called a singly-linked list for obvious reasons.

24/27 Coding Differences class FirstLastList { private Link first; // ref to first link private Link last; // ref to last link// note the extra link // public FirstLastList() // constructor { first = null; // no links on list yet last = null;// must initialize both references to null to begin with. } public boolean isEmpty() // true if no links { return first==null; } public void insertFirst(long dd) // insert at front of list a bit more complicated….. { Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list,// no real changes here. last = newLink; // newLink <-- last newLink.next = first; // newLink --> old first first = newLink; // first --> newLink } // public void insertLast(long dd) // insert at end of list // ah, but here are the changes. Additional method. { Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, first = newLink; // first --> newLink ok… and then the statement (ahead) last = newlink; is necessary too. newLink last. elseotherwise, last.next = newLink; // old last --> newLink // get last fwd link and have it point to new cell, which is the new last; last = newLink; // newLink <-- last // Then, last (up front?) points to the new last cell. } public long deleteFirst() // delete first link { } //no real differentces public void displayList() { // no differences } } // end class FirstLastList Code for Double-Ended Lists (Changed Methods)

25/27 Linked-List Efficiency Inserting and deleting at beginning of linked-list is fast. Only need to change a couple of references: O(1) time. Finding and Deleting or Inserting next to a specific item requires searching (average) half of list which O(n) comparisons. An array = O(n) comparisons too. But LL is faster to insert once place is found because nothing needs to be moved (does take time to find, however) This is significant! (but requires dynamic memory allocation, which means system calls.)\ A Linked List only uses memory it needs. Can expand as needed. Arrays are fixed size. Vectors can help, but usually involve doubling the size of an array that is about to be overfilled and then copying data from the original array into the expanded array.

26/27 Questions?