640 likes | 653 Views
Explore the design, variations, and advantages of linked list implementations in this chapter. Learn to write algorithms and define classes using linked-based structures. Dive into Node structures and LinkedList implementation details.
E N D
Objectives • After studying this chapter you should understand the following: • the design and structure of linked list implementations; • variations in designs for linked lists, including doubly linked lists; • advantages and disadvantages of linked vs. array-based implementations. • Also, you should be able to: • write algorithms for linked-based structures; • define classes implemented with linked-based structures. NH-Chapter 20
Linked implementations • Structure built from collection of nodes that reference each other. • A node • contains reference to a list element, • one or more variables referencing other structure nodes. NH-Chapter 20
node A B C D First linked implementation • LinkedList: Each node references node containing next list element. NH-Chapter 20
Class Node • Class Node, local to LinkedList, models nodes. • Node structure: • must know an element of the List, • must know the next Node in sequence. • As local class of LinkedList has no methods. NH-Chapter 20
Class Node implementation public class LinkedList<Element> extends AbstractList<Element> { … private class Node { private Element element; private Node next; // Create a Node containing specified element. public Node (Element element) { this.element = element; this.next = null; } } // end of class Node } // end of class LinkedList NH-Chapter 20
LinkedList implementation • public class LinkedList<Element> extends AbstractList<Element> { • private int size; • private Node first; • // Create an empty LinkedList<Element>. • public LinkedList () { • size = 0; • first = null; • } • … • } NH-Chapter 20
An empty linked list NH-Chapter 20
Linked list with three entries NH-Chapter 20
Method to get the i-th node /** * The i-th node of this LinkedList. * The LinkedList must be non-empty. * require 0 <= i && i < this.size() */ private Node getNode (int i) { Node p = first; int pos = 0; // p is pos-th Node while (pos != i) { p = p.next; pos = pos + 1; } return p; } NH-Chapter 20
pos 0 Method to get the i-th node : i == 2 LinkedList size 4 first p Node Node Node Node next next next next element É element É element É element É NH-Chapter 20
pos 1 Method to get the i-th node : i == 2 LinkedList size 4 first p Node Node Node Node next next next next element É element É element É element É NH-Chapter 20
pos 2 Method to get the i-th node : i == 2 LinkedList size 4 first p Node Node Node Node next next next next element É element É element É element É NH-Chapter 20
Implementing LinkedList methods public Element get (int index) { Node p = getNode(index); return p.element; } • Note: method get • uses getNode • get(i) will take i+1 steps to get the element. NH-Chapter 20
Method remove(int) • Given an index referencing a node to be removed: • Find previous node referencing it. • Update that node’s next. NH-Chapter 20
Method remove(int) • Find previous node. LinkedList Element to be removed size 4 first Node Node Node Node next next next next element É element É element É element É NH-Chapter 20
Method remove(int) LinkedList Element to be removed size 4 first p Node Node Node Node next next next next element É element É element É element É NH-Chapter 20
LinkedList Element to be removed size 4 first p Node Node Node Node next next next next element É element É element É element É Method remove(int) NH-Chapter 20
LinkedList Element to be removed size 4 first p Node Node Node Node next next next next element É element É element É element É Copy Method remove(int) NH-Chapter 20
Method remove(int) LinkedList Element to be removed size 4 first p Node Node Node Node next next next next element É element É element É element É NH-Chapter 20
Method remove(int) LinkedList Element to be removed size 3 first p Node Node Node Node next next next next element É element É element É element É NH-Chapter 20
Method remove(int), special case • Recall getNode requires argument >= 0. • If index is 0, it’s not legal to write Node p = getNode(index-1); • Special case: index 0 removes first linked element. • instance variable first of LinkedList must be modified. NH-Chapter 20
Method remove(int) LinkedList size 4 node to be removed first Node Node Node Node next next next next element É element É element É element É NH-Chapter 20
Method remove(int) LinkedList size 4 node to be removed first Node Node Node Node next next next next element É element É element É element É copy NH-Chapter 20
LinkedList size 4 first Method remove(int) node to be removed Node Node Node Node next next next next element É element É element É element É NH-Chapter 20
LinkedList size 3 first Method remove(int) node to be removed Node Node Node Node next next next next element É element É element É element É NH-Chapter 20
Implementation of remove(int) public void remove (int index) { if (index == 0) first = first.next; else { Node p = getNode(index-1); p.next = p.next.next; } size = size - 1; } NH-Chapter 20
Method add (int, Element) • To add an element at the i-th position • must find the element with index i-1 • special case to consider when i is 0. NH-Chapter 20
LinkedList Node size 4 Node Node Node Node Element to add first next next next next next element É element É element É element É element É Method add(int, Element) • Add an element at index 2. • Must find previous node. NH-Chapter 20
p LinkedList Node size 4 Node Node Node Node Element to add first next next next next next element É element É element É element É element É Method add(int, Element) NH-Chapter 20
p LinkedList Node size 4 Node Node Node Node Element to add first next next next next next element É element É element É element É element É Method add(int, Element) • New node next is p’s next NH-Chapter 20
p LinkedList Node size 4 Element to add first next element É Method add(int, Element) • p node next is new node Node Node Node Node next next next next element É element É element É element É NH-Chapter 20
p LinkedList Node size 5 Element to add first next element É Method add(int, Element) • LinkedList size is incremented. Node Node Node Node next next next next element É element É element É element É NH-Chapter 20
LinkedList Node size 4 Element to add first next element É Method add(int, Element) • add at index 0 Node Node Node Node next next next next element É element É element É element É NH-Chapter 20
LinkedList Node size 4 Element to add first next element É Method add(int, Element) • add at index 0 Node Node Node Node next next next next element É element É element É element É NH-Chapter 20
LinkedList Node size 5 Element to add first next element É Method add(int, Element) • add at index 0 Node Node Node Node next next next next element É element É element É element É NH-Chapter 20
Method add (int, Element) public void add (int index, Element element) { Node newNode = new Node(element); if (index == 0) { newNode.next = first; first = newNode; } else { Node p = getNode(index-1); newNode.next = p.next; p.next = newNode; } size = size + 1; } NH-Chapter 20
Linked list variations • Adding a reference to last node makes adding to end of list constant. NH-Chapter 20
LinkedList: adding a last reference • Creates special cases in other methods. For instance, remove public void remove (int index) { if (size == 1) { // remove only element first = null; last = null; }else if (index == 0) {//remove first element first = first.next; }else { Node p = getNode(index-1); p.next = p.next.next; if (index == size-1) //last element removed last = p; } size = size - 1; } NH-Chapter 20
Header nodes • “dummy” node • Always present at front of list, • contains no element, • first always references header node, • first is never null. • Reduces special cases in the implementation of some methods. NH-Chapter 20
Header nodes NH-Chapter 20
Circular lists • Last node references the first. NH-Chapter 20
Doubly-linked lists NH-Chapter 20
Doubly-linked list constructor • Assuming Header extends NodeDoublyLinkedList constructor creates header Node, and links it to itself. public DoublyLinkedList () { size = 0; header = new Header(); header.next = header; header.previous = header; } NH-Chapter 20
Doubly-linked list constructor NH-Chapter 20
Doubly-linked lists: add(Element) Header A B C New NH-Chapter 20
Doubly-linked lists: add(Element) • new node previous is node C Header A B C New NH-Chapter 20
Doubly-linked lists: add(Element) • new node next is header Header A B C New NH-Chapter 20
Doubly-linked lists: add(Element) • Header previous is new node Header A B C New NH-Chapter 20
Doubly-linked lists: add(Element) • C node next is new node Header A B C New NH-Chapter 20