19
Lecture5: Linked Lists Bohyung Han CSE, POSTECH [email protected] CSED233: Data Structures (2014F)

Lecture5: Linked Lists Bohyung Han CSE, POSTECH [email protected] CSED233: Data Structures (2014F)

Embed Size (px)

Citation preview

Page 1: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

Lecture5: Linked Lists

Bohyung HanCSE, POSTECH

[email protected]

CSED233: Data Structures (2014F)

Page 2: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

2 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Linked Lists

• What is the linked list? A data structure consisting of a sequence of nodes Each node is composed of data and link(s) to other nodes.

• Properties Linked lists can be used to implement several other common abstract

data types, such as stacks and queues The elements can easily be inserted or removed without reallocation

or reorganization of the entire structure Linked lists allow insertion and removal of nodes at any point in the list

in constant time. Simple linked lists do not allow random access to the data. http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html

Page 3: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

3 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Singly Linked Lists

• A singly linked list is a concrete data structure consisting of a sequence of nodes

• Each node stores Element(s) Link to the next node

A B C D

next

element

Node

Page 4: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

4 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

The Node Class for Singly Linked List

// Node classpublic class Node{ private Object element; private Node next;

// Constructors public Node() { this(null, null); }

public Node(Object e, Node n) { element = e; next = n; }

Defines element and link

Creates a node with null, which referencesto its element and next node

Creates a node with given elementand next node

Page 5: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

5 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

The Node Class for Singly Linked List public Object getElement() { return element; }

public Node getNext() { return next; }

public void setElement(Object newElem) { element = newElem; }

public void setNext(Node newNext) { next = newNext; }}

Accessor methods

Modifier methods

Page 6: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

6 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Inserting a Node at the Head

• Operation sequence Allocate a new node. Insert new element. Have new node point to

old head. Update head to point to

new node.

Node v = new Node();v.setElement(“A”);v.setNext(head);head = v;

B C D

B C D

B C D

A

A

head

head

head

Page 7: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

7 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Removing a Node from the Head

• Operation sequence Update head to point

to next node in the list. Allow garbage collector

to reclaim the former first node.

• Java: garbage collection Unused memory is reclaimed

automatically for reuse. No need to free or delete

head = head.getNext();

B C D

A

B C D

A

B C D

head

head

head

Page 8: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

8 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Inserting a Node at the Tail

• Operation sequence Allocate a new node. Insert new element. Have new node point to null. Have old last node point to

new node. Update tail to point to new

node.

Node v = new Node();v.setElement(“A”);v.setNext(null);tail.setNext(v);tail = v;

B C D

B C D

A

C D A

B

head

head

head

tail

tail

tail

Page 9: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

9 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Removing a Node at the Tail

• Operation sequence Move tail to the second

last node. Have tail node point to null. Allow garbage collector

to reclaim the former last node.

• Issues There is no constant time

way to make the tail to point to the previous node.

Removing at the tail of a singly linked list is not efficient!

B C D

A

B C D

A

B C D

head tail

head tail

tailhead

Page 10: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

10 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Stack as a Linked List

• We can implement a stack with a singly linked list.• The top element is stored at the first node of the list.• Complexity of the Stack ADT

Space complexity: Time complexity of each operation:

t

nodes

elements

Page 11: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

11 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Queue as a Linked List

• We can implement a queue with a singly linked list. The front element is stored at the first node. The rear element is stored at the last node.

• Complexity of the Queue ADT Space complexity: Time complexity of each operation:

f

r

nodes

elements

Page 12: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

12 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Node List ADT

• The Node List ADT models a sequence of positions storing arbitrary objects.

• It establishes a before/after relation between positions.• Methods

Generic methods: size(), isEmpty() Accessor methods

• first(), last()• prev(p), next(p)

Modifier methods:• set(p, e)• addBefore(p, e), addAfter(p, e),• addFirst(e), addLast(e)• remove(p)

Page 13: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

13 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Doubly Linked List

• A doubly linked list provides a natural implementation of the Node List ADT.

• Nodes implement position and store: Element Link to the previous node Link to the next node

• Special trailer and header nodes

prev next

element

Node

Page 14: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

14 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Doubly Linked List

trailerheader nodes/positions

elements

Page 15: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

15 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion

• Insertion operation We visualize insertAfter(p, X), which returns position q.

A B X C

A B C

A B C

p

X

q

p q

Page 16: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

16 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion Algorithm

Algorithm addAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p) // link v to its predecessor v.setNext(p.getNext()) // link v to its successor (p.getNext()).setPrev(v) // link p’s old successor to v p.setNext(v) // link p to its new successor, v return v // the position for the element e

Page 17: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

17 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Deletion

• Deletion in doubly linked list We visualize remove(p), where p = last().

A B C D

p

A B C

D

p

A B C

Page 18: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

18 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Deletion Algorithm

Algorithm remove(p): t = p.element // a temporary variable to hold the return value (p.getPrev()).setNext(p.getNext()) // linking out p (p.getNext()).setPrev(p.getPrev()) p.setPrev(null) // invalidating the position p p.setNext(null) return t

Page 19: Lecture5: Linked Lists Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

19