Top Banner
Introduction to Data Introduction to Data Structures and Structures and Algorithms Algorithms Chapter 5 Chapter 5 Linked List Linked List
25

Introduction to Data Structures and Algorithms Chapter 5 Linked List.

Jan 17, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

Introduction to Data Introduction to Data Structures and Structures and

AlgorithmsAlgorithms

Chapter 5Chapter 5

Linked ListLinked List

Page 2: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

Lower-Level Data Structures

Linked lists are:• Used to implement higher-level data

structures (e.g., stacks, queues, etc)• Dynamic in structure

Page 3: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

Links

• Each data item in a linked list is embedded in an object called link or node.

• Each link object has a reference to the next link object in the list

Page 4: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

Simple Linked ListThe operations allowed in simple

linked list are:• Inserting an item at the beginning of

the list• Deleting the item at the beginning of

a list• Traversing the list to display the data

items in the list

Page 5: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

Linked list

• a list of items connected together as a chain• Each item contains data part and reference

part– Data part: individual or compound data– Reference part: point to the next item on

the linked list (location information of next item)

• Operations: – Insertion– Find– Delete (element with a target value or the

head element)

Page 6: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

Comparison to array:• Similarity:

– linear data structure– Operations: insertion, deletion, find

• Difference: – dynamic size vs. static size in array– items are linked by references (based on

many isolated but related memory spaces; array: a big piece of memory space)

– referenced by relationship not by position (array: index)• To find an element, always start from the first

item (no quick access) !!!

Page 7: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

class Link //represents one item on the list {

public int iData; // data item public double dData; // data item public Link next; // next link in list// ------------------------------------------------------------- public Link(int id, double dd) // constructor { iData = id; // initialize data dData = dd; // ('next' is automatically } // set to null)// ------------------------------------------------------------- public void displayLink() // display ourself { System.out.print("{" + iData + ", " + dData + "} "); } } // end class Link

Page 8: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

What is the meaning in the following Java codes?

Link someLink = new Link();Link aLink = someLink;

– Figure 5.2– Two Link reference variables refer to the

same object of type Link

Page 9: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

class LinkList //represents the LinkedList {

private Link first; // ref to first item 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) // insert at start of list { // make new link }// ------------------------------------------------------------- public Link deleteFirst() // delete first item { // (assumes list not empty) }// ------------------------------------------------------------- public void displayList() { }

} // end class LinkList

Page 10: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

Insert a new item at the beginning of the linked list

• The new item links to the original first item• The first reference points to the new item• See Figure 5.5

public void insertFirst(int id, double dd) { // make new link Link newLink = new Link(id, dd); newLink.next = first; // newLink --> old first first = newLink; // first --> newLink }

Page 11: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

Delete the item at the beginning of the linked list

• store a copy the first item• The first reference points to the next item• Figure 5.6

public Link deleteFirst() // delete 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 }

Page 12: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

Walk through all the elements on the linked list• Keep updating the current reference• See Figure 5.7

public void displayList() { System.out.print("List (first-->last): "); Link current = first; // start at beginning of list while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println("");}

Page 13: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

class LinkListApp { public static void main(String[] args) { LinkList theList = new LinkList(); // make new list

theList.insertFirst(22, 2.99); // insert four items theList.insertFirst(44, 4.99); theList.insertFirst(66, 6.99); theList.insertFirst(88, 8.99);

theList.displayList(); // display list

while( !theList.isEmpty() ) // until it's empty, { Link aLink = theList.deleteFirst(); // delete link System.out.print("Deleted "); // display it aLink.displayLink(); System.out.println(""); } theList.displayList(); // display list } // end main() } // end class LinkListApp

Page 14: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

Find the first item with the target key on the list

public Link find(int key) // find link with given key { // (assumes non-empty list) Link current = first; // start at 'first' while(current.iData != key) // while no match, { if(current.next == null) // if end of list, return null; // didn't find it else // not end of list, current = current.next; // go to next link } return current; // found it }

Page 15: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

Delete the first item with the target key in the linked list

•See Figure 5.8•Shift all the elements down after

deletion?

Page 16: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

public Link delete(int key) // delete link with given key { // (assumes non-empty list) Link current = first; // search for link Link previous = first; while(current.iData != key) { if(current.next == null) return null; // didn't find it else { previous = current; // go to next link current = current.next; } } // found it if(current == first) // if first link, first = first.next; // change first else // otherwise, previous.next = current.next; // bypass it return current; }

Page 17: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

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 if( f != null) System.out.println("Found link with key " + f.iData); else System.out.println("Can't find link");

Link d = theList.delete(66); // delete item if( d != null ) System.out.println("Deleted link with key " + d.iData); else System.out.println("Can't delete link");

theList.displayList(); // display list } // end main() } // end class LinkList2App

Page 18: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

How to insert an item at the end of the linked list?

Double-Ended Linked list• Figure 5.9• Maintain two references

– One reference to the first item– Another reference to the last item

• Perform insertion and deletion at both ends of the list– Improve insertion and deletion

operation efficiency

Page 19: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

class FirstLastList { private Link first; // ref to first link private Link last; // ref to last link// ------------------------------------------------------------- public FirstLastList() // constructor { first = null; // no links on list yet last = null; }// ------------------------------------------------------------- public boolean isEmpty() // true if no links { return first==null; }// ------------------------------------------------------------- public void insertFirst(long dd) // insert at front of list { }// ------------------------------------------------------------- public void insertLast(long dd) // insert at end of list { }// ------------------------------------------------------------- public long deleteFirst() // delete first link { // (assumes non-empty list) }// ------------------------------------------------------------- public void displayList() { }// -------------------------------------------------------------

} // end class FirstLastList

Page 20: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

// ------------------------------------------------------------- public void insertFirst(long dd) // insert at front of list { Link newLink = new Link(dd); // make new link

if( isEmpty() ) // if empty list, last = newLink; // newLink <-- last newLink.next = first; // newLink --> old first first = newLink; // first --> newLink }// ------------------------------------------------------------- public void insertLast(long dd) // insert at end of list { Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, first = newLink; // first --> newLink else last.next = newLink; // old last --> newLink last = newLink; // newLink <-- last }// -------------------------------------------------------------

Figure 5.10

Page 21: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

public long deleteFirst() // delete first link { // (assumes non-empty list) long temp = first.dData; if(first.next == null) // if only one item last = null; // null <-- last first = first.next; // first --> old next return temp; }// -------------------------------------------------------------

public long deleteLast() // delete last link{ ?}

Page 22: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

public void displayList() { System.out.print("List (first-->last): "); Link current = first; // start at beginning while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); }

Page 23: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

Running time analysis

• Insertion, deletion– O(N) or O(1)?

• Compare to operations in the array

Page 24: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

Stack Linked List Implementation

• The push operation on a stack, inserts the new node (with data) to the top (front) of the stack myStack.insertFirst(data)

• The pop operation deletes the first node of the stackmyStack.deleteFirst()

• See Listing 5.4

Page 25: Introduction to Data Structures and Algorithms Chapter 5 Linked List.

Queue Linked List Implementation

• A node with data is inserted at the tail of the queue (last node)myQueue.insertLast(data)

• A node is removed from the front of the queue (the first node)myQueue.deleteFirst()

• See Listing 5.5