Title: Linked Lists
1Linked Lists
- 2008, Fall
- Pusan National University
- Ki-Joune Li
2Problems of Array
- Lack of Dynamic Properties
- Insertion
- Deletion
- Moving elements expensive operation
- Max. Size of Array
- Linked List
- More dynamic data structure than Array
- No subscript (or index)
- Only Linear Search is possible
3Linked List Example
Insert (??? ???)
4Linked List Data Structures
Node
Data
Link to the next node
Class LinkedList private Node first
public insert(DataType data,Node q)
insert(Node p) delete(Node p) Node
search(condition)
Class Node friend class LinkedList
private DataType data Node next
5Linked List Operations
6Stacks by Linked List
Class Stack private LinkedList list
public push(DataType data) DataType
pop()
Top
Top
Time Complexity O(1)
7Queues by Linked List
Insert Insert at last
Class Queue private Node list
public insert(DataType data) DataType
delete()
LinkedListinsert(DataType data) Node
newNodenew Node(data) if(firstNULL)
firstnewNode return Node pfirst
Node q while(p!NULL) qp
pp-gtnext q-gtnextnewNode
first
Time Complexity O(n)
Why not pointer to the last ?
8Circular List
O(n) operations to reach to the last node
first
last
O(1)
O(1)
9Circular List with Head Node
Empty List
Head
Head
Empty node with ptr to next
10Application of List Polynomials
a 3 x 14 2 x 8 3
b 12 x 14 7
11Adding Polynomials
a 3 x 14 2 x 8 3
b 12 x 14 7 x 5
c a b
12Erasing Linked List
first
Class LinkedList private Node first
public LinkedList() LinkedList()
Become garbage
void LinkedListLinkedList()
Node ptrfirst while(first!NULL)
ptrfirst-gtnext delete first
13Maintaining Available Node List
first
Class CircularList private Node first
static Node avNULL public
CircularList() CircularList()
Available Node List
av
void CircularListCircularList()
if(first!NULL) Node secondfirst-gtnext
first-gtnextav firstNULL avsecond
void CircularListgetNode() Node newNode
if(avNULL) newNodenew Node() else
newNodeav avav-gtnext return
newNode
For add operation
14Application of List Sparse Matrix
row
col
down
right
value
down
right
next
15List for Sparse Matrix Insert
O (maxr,c)
16Doubly Linked List
Linked List inefficient to go back
Head
Why not double links ?
17Doubly Linked List Insertion and Deletion
Insertion
x
p
void DoublyLinkedListInsert(DblNode p,x)
p-gtleftLinkx p-gtrightLinkx-gtrightLink
x-gtrightLink-gtleftLinkp x-gtrightLinkp
O (1)
Deletion
18Representation of Polynomial
P x10 y3 z2 2 x8 y3 z2 3 x8 y2 z2 x4 y4 z
6 x3 y4 z 2 y z
P x10 y3 2 x8 y3 3 x8 y2 x4 y4 6 x3 y4
2 y
Depends on the number of variables
NOT a General Representation
How to represent it in more general way ?
19A General Way to Represent Polynomial
P x10 y3 z2 2 x8 y3 z2 3 x8 y2 z2 x4 y4 z
6 x3 y4 z 2 y z
P ( (x10 2 x8 ) y3 3 x8 y2 ) z2 ( ( x4
6 x3 ) y4 2 y ) z
Nested Polynomial
Nested Linked List
20Generalized Lists
- Definition
- A (a0, a1, a2, an-1) where ai is ATOMIC NODE or
a LIST - When ai is a list, it is called SUBLIST.
- Linear List
- Example
- D() NULL
- A(a, (b, c)) Finite
- B(A, A, ()) ((a, (b, c)), (a, (b, c)), ())
- C(a, C) (a, (a, (a, )))) Infinite
Reusability of Generalized List Shared List
21Implementation of Generalized List
Data of Node
Node
Node / List
Flag
Next
Data
DLink
Pointer to List
Class GenList private GenListNode
first public ...
22Generalized List Example
P ( (x10 2 x8 ) y3 3 x8 y2 ) z2 ( (
x4 6 x3 ) y4 2 y ) z
23Generalized List Example
D() A(a, (b, c)) B(A, A, ()) C(a, C)
N
a
L
A
N
b
N
c
L
L
B
L
N
a
L
C
24Operation of Generalized List Copy
A((a, b), ((c, d), e))
L
L
A
L
N
e
N
a
N
b
void GenListcopy(const GenList l)
firstcopy(l.first)
L
c
N
d
void GenListcopy(const GenListNode p)
GenListNode qNULL if(p!NULL) qnew
GenListNode q-gtflagp-gtflag
if(p-gtflatNODE) q-gtdatap-gtdata else
q-gtdlinkcopy(p-gtdlink) q-gtlinkcopy(p-gtlink
) return q
One visit per node Linear Scan O(m )
Not Circular like C(a, C)
25Operation of Generalized List Equal
s
l((a, b), ((c, d), e))
L
L
l
m((a, b), ((c, f), e))
L
N
e
N
a
N
b
int operator(const GenList l,m) return
equal(l.first,m.first) int equal(GenListNode
s, t) xFALSE if(s and t are null),
return TRUE if(s and t are not null),
if(s and p are node) if(s-gtdatat-gtdata)
xTRUE else xFALSE else
xequal(s-gtdlink,t-gtdlink) if(xTRUE)
return(s-gtnext,t-gtnext) return FALSE
L
c
N
d
t
L
L
m
L
N
e
N
a
N
b
L
c
N
f
26Shared Linked List Reference Counter
D() A(a, (b, c)) B(A, A, ()) C(a, C)
Deletion of A with care
N
a
L
A
N
b
N
c
L
L
B
L
Delete list when reference counter 0
N
a
L
C
27Example Design Shape List
Total Area of P ?