Linked Lists - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Linked Lists

Description:

Title: 1 Author: lik2 Last modified by: Ki-Joune Li Created Date: 1/12/2004 8:00:17 AM Document presentation format: (4:3) – PowerPoint PPT presentation

Number of Views:109
Avg rating:3.0/5.0
Slides: 28
Provided by: lik2
Category:
Tags: circular | linked | lists | queue

less

Transcript and Presenter's Notes

Title: Linked Lists


1
Linked Lists
  • 2008, Fall
  • Pusan National University
  • Ki-Joune Li

2
Problems 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

3
Linked List Example
Insert (??? ???)
4
Linked 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
5
Linked List Operations
6
Stacks by Linked List
Class Stack private LinkedList list
public push(DataType data) DataType
pop()
Top
Top
Time Complexity O(1)
7
Queues 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 ?
8
Circular List
O(n) operations to reach to the last node
first
last
O(1)
O(1)
9
Circular List with Head Node
Empty List
Head
Head
Empty node with ptr to next
10
Application of List Polynomials
a 3 x 14 2 x 8 3
b 12 x 14 7
11
Adding Polynomials
a 3 x 14 2 x 8 3
b 12 x 14 7 x 5
c a b
12
Erasing Linked List
first
Class LinkedList private Node first
public LinkedList() LinkedList()
Become garbage
void LinkedListLinkedList()
Node ptrfirst while(first!NULL)
ptrfirst-gtnext delete first
13
Maintaining 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
14
Application of List Sparse Matrix
row
col
down
right
value
down
right
next
15
List for Sparse Matrix Insert
O (maxr,c)
16
Doubly Linked List
Linked List inefficient to go back
Head
Why not double links ?
17
Doubly 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
18
Representation 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 ?
19
A 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
20
Generalized 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
21
Implementation of Generalized List
Data of Node
Node
Node / List
Flag
Next
Data
DLink
Pointer to List
Class GenList private GenListNode
first public ...
22
Generalized List Example
P ( (x10 2 x8 ) y3 3 x8 y2 ) z2 ( (
x4 6 x3 ) y4 2 y ) z
23
Generalized 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
24
Operation 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)
25
Operation 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
26
Shared 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
27
Example Design Shape List
Total Area of P ?
Write a Comment
User Comments (0)
About PowerShow.com