linklisr

7
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 Each node is an instance of a class (i.e. an object) Head Example Declarations public class IntNode { private int data; private IntNode link; } OR public class Part { private int partNo; private double price; private Part link; }

Transcript of linklisr

Page 1: linklisr

Linked Lists

An Alternative Collections Data Structure

What Is A Linked List?

A data structure using memory that expands and shrinks as neededRelies on identical nodes “pointing” or “linked” to other nodesEach node is an instance of a class (i.e. an object)

Head

Example Declarations

public class IntNode{

private int data;private IntNode link;…

}OR

public class Part{

private int partNo;private double price;private Part link;…

}

Page 2: linklisr

Special Nodes

Head nodeThe first node in a linked listAlmost all programs using linked lists will maintain a pointer to the head nodeAn empty list is represented by storing Null in the head variable

Tail nodeThe last node in a linked listMany programs maintain a pointer to the tail nodeA tail node points to a “null” referenceIn an empty list tail will also be Null

Necessary Methods

Methods to access and modify data:e.g. getData(), getLink(), setData(int

newData), setLink(??? newLink)Methods for creating nodesi.e. constructors

Methods for adding and removing nodes

Caution!

Always make sure that your linked list methods work correctly with an empty list.

Page 3: linklisr

Adding a Node to the Front of a List1. Create a new node with the desired data, and

the link pointing to the current head node2. Make the “head” pointer point to the new nodeWhat happens if we started with an empty list?

Head

remove

Removing the First Node of a List

Make the head pointer point to what the current head node is pointing to

What happens to the original head node?What if there was only one node in the list?

Head

remove

Adding a Node After a Selected Node

1. Create a new node pointing to whatever the selected node is currently pointing to

2. Make the selected node point to the new nodeWhat happens if the selected node was the last node

in the list?

Head

remove

Selected

Page 4: linklisr

Removing a Selected Node

Head

SelectedCan we remove this?

Removing the Node After a Selected Node

HeadSelected Can we remove this?

HeadSelected

Desired Result:

What happens if we removed the tail node?

Manipulating Entire Linked Lists

Traverse the entire list in a loop that terminates when the null pointer is reachedIn the body of the loop perform the necessary operation(s)

Page 5: linklisr

Manipulating Entire Linked Lists

Examples: count the elements of the listPrint the elements of the listUpdate the values in the list (e.g. increase all prices by 10%)

If implemented in the same class as the node (i.e. no separate list class), it is better to use static methods, this way the method can be used for empty lists

Sample Code

IntNode current;for (current = head; current != null; current = current.link){

…}

Or:IntNode current;current = head;while (current != null){

…current = current.link;

}

Copying an Entire List

public static IntNode copyList (IntNode head){

if (head == null) return null;IntNode copyHead = new IntNode (head.data,null);IntNode copyTail = copyHead;IntNode source = head;while (source.link != null){

source = source.link;copyTail.addNodeAfter(source.data);copyTail = copyTail.link;

}return copyHead;

}

Page 6: linklisr

Copying a Part of a List

Given a start node and an end node make a copy of that part of the listCan we return both the head and tail of the copy?How does the method work? Is it that much different from copying the full list?

Linked List Application –IntLinkedBag

Keeps track of integer elementsCan add or delete elementsAnswers questions about its elementsCan add all the contents of another bagGrows and shrinks in size as needed

IntLinkedBag - Methods

public void add (int element)public void addAll (IntLinkedBag addend)public int countOccurences(int target)public boolean remove (int target)public int size()public void trimToSize()public static IntLinkedBag union (IntLinkedBag b1,

IntLinkedBag b2)public int grab()public void ensureCapacity (int minimumCapacity) ???public int getCapacity() ???

Page 7: linklisr

Another Linked List Application - DoubleLinkedSeq

Similar to bag but sequence is preservedAn additional instance variable is needed to hold the position of the “current” elementTwo additional instance variables “tail” and “precursor” may be used for convenienceSome methods can be preserved, others must be modifiedAdditional methods are neededImplementation: assignment 5

Doubly Linked Lists

Similar to linked lists with pointers in both directions

Head Tail

Array vs. Linked Lists vs. Doubly Linked Lists

Which is better for the following operations?:

Random accessFrequent additions/removals in the middle of a sequenceFrequent forward and backward searching of a sequenceFrequent size changes