Analysis of Algorithms - City University of New York

Download Report

Transcript Analysis of Algorithms - City University of New York

More on Linked List
Yumei Huo
Department of Computer Science
College of Staten Island, CUNY
Spring 2009
2015/7/17
Linked list
1
Outline
Variants of Singly linked list
Doubly linked list
2015/7/17
Linked list
2
Outline
Variants of Singly linked list
Doubly linked list
2015/7/17
Linked list
3
Variants of Singly linked list
Singly linked circular list
Singly linked list with head node
Singly linked circular list with head node
2015/7/17
Linked list
4
Singly linked circular list
Link the last node back to the first
first
A
2015/7/17
B
C
Linked list
D
E
5
Print all the elements in a singly
linked circular list
first
A
B
C
D
E
Node * tmp=first;
if (tmp != 0)
{
cout << tmp->data;
while (tmp->next !=first)
{
tmp=tmp->next;
cout << tmp->data;
}
}
2015/7/17
Linked list
6
Singly linked list with head node
Add an additional node,
called head node, at the
front
first

Head node
Empty list
A
first
B
C

Head node
Singly linked list with head node
2015/7/17
Linked list
7
Singly linked circular list with
head node
first
Head node
Empty list
A
first
B
C
Head node
Circular list with head node
2015/7/17
Linked list
8
Outline
Variants of Singly linked list
Doubly linked list
2015/7/17
Linked list
9
Doubly Linked List
Nodes implement Position and
store:



prev
data
data
link to the previous node
link to the next node
Head
2015/7/17
A
B
node
C
Linked list
next
D
Tail
10
Operations Comparison
Operations
Singly Linked Doubly Linked
List
List
Determine the length
O(n)
O(n)
Find the kth element
O(k)
O(k)
Search for a given element
O(n)
O(n)
Delete the kth element
O(k)
O(k)
Insert a new element just after the kth O(k)
O(k)
Delete a node pointed by p
O(n)
O(1)
Insert a new element before the node O(n)
pointed by p
O(1)
2015/7/17
Linked list
11
Delete a node pointed by p
Head
Head
A
q
B
p
C
D
Tail
C
D
Tail
q
B
D
Doubly Linked List
A
Head
2015/7/17
A
q
B
Head
A
Head
A
Head
A
q
B
q
B
q
B
Singly Linked
Linked list
Tail
p
C
D
C
D
D
List
12
Delete a node pointed by p in a
doubly linked list
Head
Head
Head
A
q
B
A
q
B
A
p
C
D
Tail
C
D
Tail
q
B
D
Doubly Linked List
Tail
p->prev->next=p->next;
p->next->prev=p->prev;
delete p;
2015/7/17
Linked list
13
Delete a node pointed by p in
singly linked list
Head
A
Head
A
Head
A
q
B
q
B
q
B
Singly Linked
p
C
D
C
D
D
List
//search p’s previous node q, assume p exists and is not the 1st node
//(what if p is the 1st node?):
Node * q=head;
while (q->next != p)
q=q->next;
//let q point to p’s next node
q->next=p->next;
delete p;
2015/7/17
Linked list
14
Insert a new element ‘X’ before the
node pointed by p
Head
A
Head
A
X
q
X
t
B
A
Head
p
C
t
B
t
B
Tail
p
C
Tail
p
C
Tail
q
Doubly Linked List
Head
A
B
Head
A
B
Head
2015/7/17
A
t
t
t
B
Singly Linked List
Linked list
p
C
X
q
X
C
q
p
p
C
15
Insert a new element ‘X’ before the node
pointed by p in a doubly linked list
Head
A
Head
A
Head
A
p
C
t
B
t
B
X
q
X
t
B
Tail
p
C
Tail
p
C
Tail
q
Doubly Linked List
//create a new node containing ‘X’:
Node *q = new Node;
q->data=‘X’;
//insert q before p:
q->prev=p->prev;
q->next=p;
p->prev->next=q;
p->prev=q;
2015/7/17
Linked list
16
Insert a new element ‘X’ before the node
pointed by p in a singly linked list
Head
A
B
Head
A
B
Head
A
t
t
t
B
Singly Linked List
p
C
X
q
X
C
q
p
p
C
//create a new node containing ‘X’:
Node * q= new Node;
Q->data = ‘X’;
//search p’s previous node t, assume p exists and is not the 1st node
//(what if p is the 1st node?):
Node * t= head;
while (q->next != p)
q=q->next;
//insert q between t and p
q->next=p;
t->next=q;
2015/7/17
Linked list
17
Thank you!
2015/7/17
Linked list
18