* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Download **** 1 - Postech
Survey
Document related concepts
Transcript
CSED233: Data Structures (2014F)
Lecture5: Linked Lists
Bohyung Han
CSE, POSTECH
bhhan@postech.ac.kr
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
2
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Singly Linked Lists
• A singly linked list is a concrete data structure consisting of a
sequence of nodes
Node
• Each node stores
next
Element(s)
Link to the next node
element
A
B
C
3
D
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
The Node Class for Singly Linked List
// Node class
public class Node
{
private Object element;
private Node next;
// Constructors
public Node()
{
this(null, null);
}
Defines element and link
Creates a node with null, which references
to its element and next node
public Node(Object e, Node n)
{
element = e;
next = n;
}
Creates a node with given element
and next node
4
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
The Node Class for Singly Linked List
public Object getElement()
{
return element;
}
Accessor methods
public Node getNext()
{
return next;
}
public void setElement(Object newElem)
{
element = newElem;
}
public void setNext(Node newNext)
{
next = newNext;
}
Modifier methods
}
5
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Inserting a Node at the Head
• Operation sequence
head
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
head = v;
B
C
D
head
A
B
C
D
A
B
6
C
D
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Removing a Node from the Head
• Operation sequence head
Update head to point
to next node in the list.
Allow garbage collector
to reclaim the former
first node.
A
B
C
D
head
head = head.getNext();
A
• Java: garbage collection
B
C
D
head
Unused memory is reclaimed
automatically for reuse.
No need to free or delete
B
7
C
D
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Inserting a Node at the Tail
• Operation sequence
tail
head
Allocate a new node.
Insert new element.
Have new node point to null.
Have old last node point to
head
new node.
Update tail to point to new
node.
B
D
tail
B
Node v = new Node();
v.setElement(“A”);
v.setNext(null);
head
tail.setNext(v);
tail = v;
C
C
D
A
tail
B
C
8
D
A
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Removing a Node at the Tail
• Operation sequence head
tail
Move tail to the second
last node.
Have tail node point to null. A
Allow garbage collector
head
to reclaim the former
last node.
B
C
D
tail
• Issues
B
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!
C
D
tail
head
A
B
9
C
D
CSED233: Data Structures
by 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: 𝑂 1
nodes
t
elements
10
CSED233: Data Structures
by 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: 𝑂 1
r
nodes
f
elements
11
CSED233: Data Structures
by 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)
12
CSED233: Data Structures
by 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
Node
prev
next
element
13
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Doubly Linked List
nodes/positions
header
trailer
elements
14
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion
• Insertion operation
We visualize insertAfter(p, X), which returns position q.
A
B
C
p
A
q
B
C
X
p
A
q
B
15
X
C
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Insertion Algorithm
Algorithm addAfter(p,e):
Create a new node v
v.setElement(e)
v.setPrev(p)
v.setNext(p.getNext())
(p.getNext()).setPrev(v)
p.setNext(v)
return v
// link v to its predecessor
// link v to its successor
// link p’s old successor to v
// link p to its new successor, v
// the position for the element e
16
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Deletion
• Deletion in doubly linked list
We visualize remove(p), where p = last().
p
A
B
C
A
B
C
D
p
D
A
B
17
C
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Deletion Algorithm
Algorithm remove(p):
t = p.element
(p.getPrev()).setNext(p.getNext())
(p.getNext()).setPrev(p.getPrev())
p.setPrev(null)
p.setNext(null)
return t
// a temporary variable to hold the return value
// linking out p
// invalidating the position p
18
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
19