linked lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • a singly-linked list is a...

55
Linked Lists Chapters 5

Upload: trandang

Post on 14-Apr-2019

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Linked Lists

Chapters 5

Page 2: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Fundamentals

• A singly-linked list is a sequence of data

elements (of the same type) arranged

one after another conceptually.

• Each element is stored in a node.

• Each node also contains a link that

connects this node to the next node in

the list.

Page 3: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Fundamentals (cont’d)

• A special link called the head references the first node in a list.

• Some lists may also have a special link called the tail that references the last node in a list.

• A cursor is a link that points to one of the nodes of the list.

• A list may be empty. (i.e. head = tail = null).

Page 4: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Conceptual Picture

12 31 20

head

Nodenull

data link

Page 5: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Actual Picture

WORD ADDRESS CONTENTS

1000 31 (2nd number)

1002 1008

1004 12 (1st number)

1006 1000

1008 20 (3rd number)

1010 0

1012 1004head

Page 6: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Defining a Node

public class IntNode

{

private int data;

private IntNode link;

// IntNode methods

}

Page 7: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Constructor

public IntNode(int initialData)

{

data = initialData;

link = null;

}

Page 8: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Accessor Methods

public int getData()

{

return data;

}

public IntNode getLink()

{

return link;

}

Page 9: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Mutator Methods

public void setData(int newData)

{

data = newData;

}

public void setLink(IntNode newLink)

{

link = newLink;

}

Page 10: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Defining a Linked List

public class IntList

{

private IntNode head;

private IntNode tail;

private IntNode cursor;

// IntList methods

}

Page 11: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Constructor

public IntList()

{

head = null;

tail = null;

cursor = null;

}

Page 12: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Add to head of list

12 31 20

head

10

newNode

newNode = new IntNode(element);

X

newNode.setLink(head);

X

head = newNode;

Page 13: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Add a new head of list

public void addNewHead(int element) {

IntNode newNode =

new IntNode(element);

newNode.setLink(head);

head = newNode;

if (tail == null) tail = head;

cursor = head;

}

Page 14: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Add after cursor (general)

15

newNode

X

newNode.setLink(cursor.getLink());

X

cursor.setLink(newNode);

12 31 20

cursor

Page 15: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Add after cursor (general)

15

newNode

X

newNode.setLink(cursor.getLink());

X

cursor.setLink(newNode);

cursor = newNode;

12 31 20

cursor

X

Page 16: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Add integer after cursor

public void addIntAfter(int element)

{

IntNode newNode =

new IntNode(element);

if (cursor == null)

{

head = newNode;

tail = newNode;

cursor = newNode;

}

Page 17: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Add integer after cursor

(cont’d)else {

newNode.setLink

(cursor.getLink());

cursor.setLink(newNode);

cursor = newNode;//advance cursor

if (cursor.getLink() == null)

tail = cursor;

}

}

Page 18: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Watch out!

15

newNode

X

cursor.setLink(newNode);

X

newNode.setLink(cursor.getLink());

12 31 20

cursor

Page 19: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Remove after cursor (general)

X

cursor.setLink(cursor.getLink().getLink());

Why do we have to remove AFTER

the cursor? Can’t we just remove

the cursor node ?

12 31 20

cursor

Page 20: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Remove after cursor

public void removeIntAfter()

{

if (cursor != tail) {

cursor.setLink(

cursor.getLink()

.getLink());

if (cursor.getLink() == null)

tail = cursor;//last node

}

}

Page 21: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Remove head of list

public void removeHead()

{

if (head != null)

head = head.getLink();

if (head == null)

tail = null;

cursor = head;

}

Page 22: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Working with cursor

public boolean advanceCursor() {

if (cursor != tail) {

cursor = cursor.getLink();

return true;

}

else

return false;

}

Page 23: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Working with cursor (cont’d)

public void resetCursor() {

cursor = head;

}

public boolean isEmpty() {

return (cursor == null);

}

Page 24: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

“Traverse” a list

12 31 20

head nodePtr

nodePtr = head;

X

nodePtr = nodePtr.getLink();

X X

Page 25: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Length of List

public int listLength()

{

IntNode nodePtr = head;

int answer = 0;

while (nodePtr != null) {

answer++;

nodePtr = nodePtr.getLink();

}

return answer;

}

Page 26: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Point of Caution

• Why didn’t we define nodePtr this way in

listLength?IntNode nodePtr = new IntNode(0);

nodePtr = head;

• Use “new” only when you actually need a new

node!

• If you are defining a variable to reference a

node that already exists, don’t use “new”!

Page 27: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Search the listpublic boolean listSearch(int target) {

IntNode nodePtr = head;

while (nodePtr != null) {

if (target == nodePtr.getData()) {

cursor = nodePtr;

return true;

}

nodePtr = nodePtr.getLink();

}

return false;

}

Page 28: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Set cursor to specific positionpublic boolean listPosition(int position)

{

IntNode nodePtr = head;

int i = 1;

if (position <= 0) throw ...etc...

while (i<position && nodePtr != null){

nodePtr = nodePtr.getLink();

i++;

}

if (nodePtr != null) cursor = nodePtr;

return (nodePtr != null);

}

Page 29: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Copy a list

public static IntList listCopy

(IntList source) {

IntList newList = new IntList();

IntNode nodePtr = source.head;

while (nodePtr != null) {

newList.addIntAfter

(nodePtr.getData());

nodePtr = nodePtr.getLink();

}

return newList;

}

Page 30: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Additional IntList Methodspublic int getNodeData() throws

EmptyListException {

if (cursor == null)

throw new EmptyListException(...);

return (cursor.getData());

}

public void setNodeData(int element)

throws EmptyListException {

if (cursor == null)

throw new EmptyListException(...);

cursor.setData(element);

}

Page 31: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

The Bag ADT using Lists

public class IntLinkedBag

implements Cloneable

{

private IntList data;

private int manyItems;

Page 32: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Implementation (cont’d)

public IntLinkedBag() {

manyItems = 0;

data = new IntList();

}

No need to worry about the bag’s capacity

since a linked list has no maximum

capacity! Only one constructor is needed.

Page 33: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Implementation (cont’d)

public int getCapacity() {

return Integer.MAX_VALUE;

}

public int size() {

return manyItems;

}

Page 34: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Implementation (cont’d)

public void ensureCapacity

(int minimumCapacity) {

// no work is needed

}

Page 35: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Implementation (cont’d)

public void add(int element) {

data.addNewHead(element);

manyItems++;

}

Order of Complexity?

Page 36: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Implementation (cont’d)public int countOccurrences(int target) {

int answer = 0;

int index;

data.resetCursor();

for (index=0; index<manyItems; index++)

{

if (target == data.getNodeData())

answer++;

data.advanceCursor();

}

return answer;

} Order of Complexity?

Page 37: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Removing an element

• Other methods can be implemented in a similar

manner.

• Watch out!remove isn’t easy with singly linked lists!

• How can we remove an element with the

structure we have?

• Use listSearch to move cursor to location of

target

• BUT we can’t remove that node!

Page 38: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Trailing pointers

• Use a trailing pointer to keep track of the

previous node to the current node we’re

examining.

• Once we find the node we want to remove,

the trailing pointer will point to the

previous node which we can connect to

the next node after the current node.

Page 39: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Example (remove 9)

12 31 20

head

9

prevPtr nodePtr

Step 1:

nodePtr = head

prevPtr = null

X

Step 2 :

prevPtr = nodePtr

nodePtr = nodePtr.getLink()

X

Step 2

(again)

X X

Step 3: prevPtr.setLink(nodePtr.getLink())

X

Page 40: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Implementationpublic boolean remove(int target) {

IntNode nodePtr = head;

IntNode prevPtr = null;

while (nodePtr != null &&

nodePtr.getData() != target) {

prevPtr = nodePtr;

nodePtr = nodePtr.getLink();

}

if (nodePtr != null)

prevPtr.setLink(nodePtr.getLink());

return (nodePtr != null);

} Order of Complexity?

Page 41: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Linked List Variations

• Doubly-linked list

head

2012 31

tail

prev data next

Page 42: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Linked List Variations

• Circular-linked list

12 31 20

head

Page 43: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Linked List Variations

• Linked list with dummy head node

12 31 20

head

nulldummy node

- always the first node of a list

- never stores any data of the list

- thus, head is never null

Page 44: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Arrays vs. Linked Lists

Arrays

• Better for random access to any data value

• Better if number of elements is known and doesn’t vary much

Linked Lists

• Better for additions and removals (other data elements do not need to be moved)

• Better if number of elements varies greatly and is not known at runtime

Page 45: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

The Object Data Type

• A variable of type Object is capable of holding a reference to any kind of object.

• Let ObjectB be a subclass of ObjectA.ObjectA a;

ObjectB b;

• a = b;

OK, widening conversion is automatic

• b = a;

NO, narrowing conversion is not automatic

b = (ObjectB) a;

Page 46: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Wrapper Classes

• Primitive data types (not objects):byte short int long

float double char boolean

• Wrapper classes are object classes:Byte Short Integer Long

Float Double Character Boolean

• To perform arithmetic on data in a wrapper class, you need to extract the data firstexample: Integer intObject =

new Integer(214);

int i = intObject.intValue();

Page 47: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Generic Node

public class Node

{

private Object data;

private Node link;

// Node methods

}

Page 48: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Constructor

public Node(Object initialData)

{

data = initialData;

link = null;

}

Using the constructor:

Integer intObject = new Integer(214);

Node newNode = new Node(intObject);

Page 49: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

getData()

public Object getData()

{

return data;

}

Using the accessor:

Integer I =

(Integer)newNode.getData();

System.out.println(I.intValue());

Page 50: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

setData()

public void setData(Object newData)

{

data = newData;

}

Using the mutator:

Integer J = new Integer(220);

newNode.setData(J);

Page 51: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

A generic linked list

public class List

{

private Node head;

private Node tail;

private Node cursor;

// List methods

}

Page 52: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

A sample method

public void addNewHead(Object element)

{

Node newNode =

new Node(element);

newNode.setLink(head);

head = newNode;

if (tail == null) tail = head;

cursor = head;

}

Page 53: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Another methodpublic boolean listSearch(Object target) {

Node nodePtr = head;

while (nodePtr != null) {

if (target.equals(nodePtr.getData()))

{

cursor = nodePtr;

return true;

}

nodePtr = nodePtr.getLink();

}

return false;

}CAREFUL! target could be null

(code not shown here)

Page 54: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Using a generic data structure

• To store data in the list, put it in a wrapper

(if necessary) before you insert it into the

list.

• If you extract data from the list, remove it

from the wrapper (if necessary) before

processing it.

• REMEMBER: If you extract data from the list, the accessor will return an Object

which has to be typecast (narrowed).

Page 55: Linked Lists - cs.stonybrook.educse214/lecture_slides/unit2.pdf · • A singly-linked list is a sequence of data elements (of the same type) arranged one after another conceptually

Iterators (optional)

public class List implements Iterator

If a class implements the Iterator interface,

it must provide the following methods:

public boolean hasNext()

public Object next()

public void remove()

• Using a loop, iterators allow you to step through

a collection, like a list, just as an index allows

you to step through an array.