Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introduction to Data Structure
CHAPTER 4
LINKED LISTS
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
Pointers
Singly Linked Lists
Dynamic Linked Stacks and Queues
Polynomials
Additional List Operations
Equivalence Relations
Sparse Matrices
Doubly Linked Lists
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-1
Contents
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Basic Concepts
Arrays
Stacks and Queues
Linked Lists
Trees
Graph
Chapter 7 Sorting
Chapter 8 Hashing
Chapter 9 Heap Structures
Chapter 10 Search Structures
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-2
4.1 Pointer
Pointer in C
int i, *pi;
pi = &i;
int i, *pi;
pi
i
Some integer
i=10 or *pi=10
i
pi
10
pi = malloc(sizeof(int));
/* assign to pi a pointer to int */
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
pi
4-3
4.2 Singly Linked Lists
Sequential mapping (not suitable for insertion & deletion)
Non-sequential: linked
Ordered Lists
Operations
- length
- read all
- retrieve i-th
- store i-th
- Insert
- delete
Seq.
eg.
A
C
E
G
X
Y
E
G
X
insert D
A
C
D
Y
Non-seq. mapping: Insert “D”
head=1
Data
Link
insert
1
2
3
4
5
6
7
A
C
E
G
X
Y
D
2
73
4
5
6
0
3
1
2
3
4
5
6
7
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-4
Singly Linked Lists: Delete “G”
Non-seq. mapping: Delete “G”
head=1
Data
1
2
3
4
5
6
7
A
C
E
G
X
Y
D
delete
Link
2
7
1
2
54
3
5
6
0
3
4
5
6
7
delete G
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-5
Singly Linked Lists: Arrow Notation
Arrow notation
head
1
A
2
1
…
…
C
2
0
6
Insert “D”
head
7
1
A
2
1
C
3
Delete “G”
E
2
3
D
Y
3
7
G
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-6
Singly Linked Lists vs. Array
Array
Successive items locate a fixed distance
Disadvantages:
• data movements during insertion and deletion
Possible solution
Linked list
More Examples:
Figure 4.1 vs. Figure 4.2 (p.140)
Figure 4.3
Figure 4.4
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-7
Defining a Linked List Node in C
To represent a single node in C
To define the structure of a node, we need to know the type of
each of its field
data
link
Structure
data type: array or char or …
pointer
Example: ThreeLetterNode -- Class definition
typedef struct list_node *list_pointer;
typedef struct list_node {
char data[3];
list_pointer link;
};
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
ThreeLetterNode
data
link
Some
ThreeLetterNode
data[0] data[1] data[2]
4-8
Example: A complex List Structure
The definition of Node A
The definition of Node B
typedef struct list_nodeb *nodeb;
typedef struct list_nodeb {
typedef struct list_nodea *nodea;
typedef struct list_nodea {
int data;
nodeb linkb;
int data1;
char data2;
float data3;
nodea linka;
nodeb linkb;
};
};
55
‘c’
A possible configuration
list_nodea
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
3.14
22
list_nodeb
4-9
Example: A complex List Structure (cont.)
The definition of Node A
The definition of Node B
typedef struct list_nodea *nodea;
typedef struct list_nodea {
int data1;
char data2;
float data3;
nodea linka;
nodeb linkb;
};
typedef struct list_nodeb *nodeb;
typedef struct list_nodeb {
int data;
nodeb linkb;
};
55
‘c’
3.14
list_nodea
22
23
Another possible configuration
list_nodeb
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-10
Creating a New Node in C
Assume the following node structure
typedef struct list_node *list_pointer;
typedef struct list_node {
char data[3];
list_pointer link;
};
Invoke malloc to obtain a storage to hold the node
list_pointer ptr = NULL;
ptr = (list_pointer) malloc(sizeof(list_node));
ptr
NULL
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
ptr
4-11
Freeing a Node in C
Assume the following node structure
typedef struct list_node *list_pointer;
typedef struct list_node {
char data[3];
list_pointer link;
};
Invoke function free to return the node’s storage
free(ptr);
ptr
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-12
Example List Manipulation Operations in C
Assume we have classes: ListNode and List
typedef struct list_node *list_pointer;
typedef struct list_node {
int data;
list_pointer link;
};
ptr
list_pointer ptr = NULL;
List
…
list_node
Some example list manipulation operations:
create2( ) // Create two nodes
insert( )
delete( )
Inverting a list
concatenating two lists
…
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-13
Example: Create a Two-node List
list_pointer create2( )
{
list_pointer first, second;
first = (list_pointer)malloc(sizeof(list_node));
second = (list_pointer)malloc(sizeof(list_node));
second->link = NULL;
second->data = 20;
first->data = 10;
first->link = second;
return first;
10
first
}
0
p.143
20
NULL
20
NL
20
first
10
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
20
0
20
4-14
Example: Inserting a Node
void insert(list_pointer *ptr, list_pointer node)
p.144
{ /* insert a new node with data = 50 into list ptr after node */
list_pointer temp;
temp = (list_pointer)malloc(sizeof(list_node)); /* create a new node */
if (IS_FULL(temp)) {
fprintf(stder, “The memory is full\n”);
exit(1);
ptr
}
temp->data = 50;
50 NULL
temp
if (*ptr) {
temp->link = node->link;
node->link = temp;
} else { /* list ptr is empty */
node
temp->link = NULL;
*ptr = temp;
ptr
10
20 NULL
...
}
}
temp
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
50
4-15
Example: Deleting a Node
void delete(list_pointer *ptr, list_pointer trail, list_pointer node)
{
/* delete the node after trail */
if (trail)
trail->link = node->link;
p.145
else
*ptr = (*ptr)->link;
free(node);
/* return the node to the system */
}
trail
ptr
10
node
50
20 NULL
How about if trail = NULL?
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-16
Delete a Node: A Dangling Reference
A dangling reference
trail
ptr
BAT
CAT
MAT
SAT
VAT NULL
dangling reference
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-17
4.3 Linked Stacks and Queues
Stacks and Queues in Linked lists
top
element link
NULL
(a) Linked Stack
front
rear
element link
NULL
(b) Linked Queue
F
R
Note: Compare with sequence queue
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-18
Linked m Stacks and m Queues
Several stacks and queues co-exist:
Sequence – not easy to handle “insert” and “delete”
Linked – a good choice
Representing n stacks
#define MAX_STACKS 10
typedef struct {
int key;
/* other fields */
} element;
typedef struct stack *stack_pointer;
typedef struct stack {
element item;
stack_pointer link;
};
stack_pointer top[MAX_STACKS];
Initial condidtion:
top[i] = NULL, 0 ≤ i < MAX_STACKS
Boundary conditions:
top[i] = NULL iff ith stack is empty
IS_FULL(temp) iff the memory is full
top
0
top
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
i.e. NULL = 0 null value
4-19
Linked m Stacks and m Queues (cont.)
Representing m queues
#define MAX_QUEUES 10
typedef struct queue *queue_pointer;
typedef struct queue {
element item;
queue_pointer link;
};
queue_pointer front[MAX_QUEUES],
rear[MAX_QUEUES];
Initial condidtion:
front[i] = NULL, 0 ≤ i < MAX_QUEUES
Boundary conditions:
front[i] = NULL iff ith queue is empty
IS_FULL(temp) iff the memory is full
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
rear
front
0
front
i.e. NULL = 0 null value
4-20
Linked Stacks: Operations
Add to a linked stack
void add(stack_pointer *top, element item)
p.149
{
stack_pointer temp = (stack_pointer) malloc(sizeof (stack));
if (IS_FULL(temp)) {
fprintf(stderr, “The memory is full\n”);
exit(1);
}
temp
temp->item = item;
temp->link = *top;
}
top
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-21
Linked Stacks: Operations (cont.)
Delete from a linked stack
element delete(stack_pointer *top)
{
stack_pointer temp = *top;
element item;
p.149
if (IS_EMPTY(temp)) {
fprintf(stderr, “The memory is empty\n”);
exit(1);
temp
}
item = temp->item;
*top = temp->link;
free(temp);
top
return item;
}
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-22
Linked Queues: Operations
Add into a linked queue
void addq(queue_pointer *front, queue_pointer *rear, element item)
{
queue_pointer temp = (queue_pointer) malloc(sizeof (queue));
if (IS_FULL(temp)) {
fprintf(stderr, “The memory is empty\n”);
exit(1);
}
temp->item = item;
temp->link = NULL;
if (*front) (*rear)->link = temp;
else *front = temp;
front
*rear = temp;
}
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
temp
rear
p.151
4-23
Linked Queues: Operations (cont.)
Delete from a linked queue
element deleteq(queue_pointer *front)
{
queue_pointer temp = *front;
element item;
p.151
if (IS_EMPTY(temp)) {
fprintf(stderr, “The memory is empty\n”);
exit(1);
}
item = temp->item;
*front = temp->link;
temp front
free(temp);
return item;
rear
}
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-24
4.4 Polynomials
Consider a polynomial
A( x ) am1 x em 1 ... a0 x e0
ai 0, em1 em-2 ... e0 0
term: represented by a node
Tackle the reasonable-sized problem
Node structure
coef
exp
link
e.g. A = 3x14 + 2x8 + 1
A
^
3
14
2
8
1
0
0
^
Note: in Array
3
0
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
0
0
0
0
0
0
2
0
0
0
……
1
4-25
Polynomial Representation
Representation
A( x) am1 x em1 am2 x em2 ... a0 x e0
Type declaration
typedef struct poly_node *poly_pointer;
typedef struct poly_node {
int coef;
int expon;
poly_pointer link;
};
poly
coef
poly_pointer a, b, d;
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
p.152
exp
link
4-26
Polynomial Representation: Example
Examples
a 3x 14 2 x 8 1
a
3
14
8
1
0
null
-3 10
10
6
null
2
b 8 x 14 3x 10 10 x 6
b
8
14
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-27
Adding Polynomials : d = a + b
Adding Polynomials: Figure 4:12 d = a + b
Case 1: a->exp = b->exp
a
3
14
2
8
14
-3 10
1 0
a
b
8
10
6
b
d
11 14
d
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-28
Adding Polynomials : d = a + b (cont.)
Case 2: a->exp < b->exp
a
3
14
2
8
1
0
a
b
8
14
-3 10
10 6
b
d
11 14
-3 10
d
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-29
Adding Polynomials: d = a + b (cont.)
Case 3: a->exp > b->exp
3
14
2
8
1
0
-3 10
10
6
a
8 14
b
11 14
-3 10
2
8
d
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-30
Adding Polynomials: Algorithm
Algorithm for adding two polynomials
(C in Program 4.10, p.154)
…
…
// copy rest of a
…
// copy rest of b
…
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-31
Adding Polynomials: Complexity Analysis
(1) Coefficient additions
0 additions min(m, n)
where m (n) denotes the number of terms in A (B).
(2) Exponent comparisons
extreme case
em-1 > fm-1 > em-2 > fm-2 > … > e0 > f0
m+n-1 comparisons
(3) Creation of new nodes
extreme case
m + n new nodes
Summary: O(m+n), where m (n) denotes the number of terms in A (B).
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-32
Erasing Polynomials
Consider a Polynomial expression:
e (x) = a (x) * b (x) + d (x)
polynomial a, b, d, e;
a = read_poly(); // read and create polynomial a
b = read_poly(); // read and create polynomial b
d = read_poly(); // read and create polynomial c
temp = pmult(a, b);
e = padd(temp, d);
print_poly(e) ;
Before and after the above procedure been executed
Free all nodes?
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-33
Erasing Polynomials (cont.)
C program
void earse(poly_pointer *ptr)
{
/* erase the polynomial pointed to by ptr */
poly_pointer temp;
while (*ptr) {
temp = *ptr;
*ptr = (*ptr)->link;
free(temp);
}
}
ptr
3
14
2
8
Erasing complexity:
O(n), n: nonzero terms
not efficient!!
1
0
null
temp
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-34
Circular List Representation
Representing Polynomials as Circularly Linked Lists
a
3 14
2
8
1
0
a 3x 14 2 x 8 1
The operation for erasing polynomial would be very
efficient by maintaining our own list of freed node, i.e.,
free pool (see next slide)
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-35
Circular List Representation (cont.)
Concept of polynomial erasing with free storage pool
avail
3
ptr
3
14
2
8
1
2
temp
1
0
1. temp = ptr->link;
2. ptr->link = avail;
3. avail = temp;
p.159
avail
Erasing complexity:
Circularly List: O(1)
Chain List: O(n), n: nonzero terms
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-36
STORAGE POOL
The STORAGE POOL (available pool/space, free space pool)
the set of all node, which can be allocated
a free space available for a new node
Method 1
Method 2
AV
AV
storage pool
n
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-37
STORAGE POOL: Array Method
Method 1: Array Method
1
2
n
storage pool
AV
procedure GETNODE(I)
if AV > n then call NO_MORE_NODE;
I AV;
AV AV + 1;
end GETNODE
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-38
STORAGE POOL: Linked Method
Method 2: Linked Method
the set of all nodes not currently in use is linked together.
AV
Procedure GETNODE(x)
if AV=0 then call NO_MORE_NODE;
x AV;
AV LINK(AV);
end
AV
procedure RET(x)
LINK(x) AV;
AV x;
end
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
X
4-39
Circular List Representation with Head
Problem with circular linked lists:
How to represent zero polynomial?
Solution: introducing an additional Head Node
(1) Zero (need a head node)
-1
a
Zero polynomial
(2) Non-zero Polynomial
a
exp
-1
3 14
2
8
1
0
a 3x 14 2 x 8 1
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-40
Circular with Head: d = a + b
a
exp
-1
3 14
2
8
1
0
a 3x 14 2 x 8 1
b
exp
-1
8 14
-3 10
10 6
b 8 x 14 3x 10 10 x 6
d=a+b
exp
-1
11 14
…
1 0
See p. 161, Program 4.16
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-41
Adding Polynomials: Algorithm Comparison
Algorithm for adding two polynomials
Chain Data Structure
Circularly with Head Node
(C in Program 4.10, p.154)
…
…
// copy rest of a
…
// copy rest of b
…
??? when ???
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
(C in Program 4.16, p.161)
…
…
…
…
4-42
4.5 Additional List Operations
Linked Lists Operations
create
insert
delete
get_node
ret_node
invert
concatenate
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-43
Chain Linked List: Invert
Inverting a Chain
first
1
2
3
…
7
NULL
first
7
6
5
…
1
NULL
list_pointer invert(list_pointer lead)
{
list_pointer middle, trail;
p.164, Program 4.17
middle = NULL;
while (lead) {
trail = middle;
middle = lead;
lead = lead->link; /* lead moves to next node */
middle->link = trail; /* link middle to preceding node */
}
return middle;
}
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-44
Chain Linked List: Invert (cont.)
first
1
2
3
lead
middle
first
7
NULL
lead = first; middle = NULL;
NULL
1
2
3
middle lead
trail NULL
first
…
1
…
7
NULL
trail = middle; middle = lead
2
middle
lead
trail
NULL
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
3
…
7
NULL
lead = lead->link;
middle->link = trail;
4-45
Chain Linked List: Invert (cont.)
first
…
3
2
1
trail
lead middle
NULL
first
trail middle
NULL
first
1
2
trail
NULL
trail = middle; middle = lead
…
3
2
1
7
lead
3
7
NULL
lead = lead->link;
middle->link = trail;
…
7
NULL
lead middle trail = middle; middle = lead
NULL
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-46
Chain Linked List: Invert (cont.)
first
… 7
3
2
1
trail middle
lead
NULL
lead = lead->link;
middle->link = trail;
NULL
...
first
7
6
middle
lead
5
…
1
NULL
first = middle;
NULL
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-47
Chain Linked List: Concatenate
this
1
...7
3
2
NULL
p
b
this
11
12
13
. . . 17
1
2
3
... 7
NULL
p
b
11
12
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
13
. . . 17
NULL
4-48
Circular Linked Lists: Adding New Node
Adding new node to front or rear
Problem: move down the whole list when add an item to the front.
first
1
2
3
... 7
New x
first
0
1
2
rear
front
first
1
... 7
3
New x
2
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
3
... 7
0
4-49
Circular Lists: Adding New Node (cont.)
Circular lists: with pointer to last node
1
2
3
... 7
1
2
3
... 7
last
New x
Add to front
0
last
x->link = last->link;
last->link = x;
Add to rear
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
???
4-50
Circular Linked Lists (cont.)
Inserting at the front [Program 4.19, p.165]
void insert_front(list_pointer *ptr, list_pointer node)
{
if (IS_EMPTY(*ptr) { /* empty List */
0
*ptr = node ;
node
node->link = node ;
}
else {
node->link = (*ptr)->link ; (*ptr)->link = node ;
}
}
...
3
2
1
ptr
7
ptr
node
0
1
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
2
3
... 7
ptr
4-51
Linked List for Sparse Matrix
Circular linked list representation of a sparse matrix has two
types of nodes:
head node: tag, down, right, and next
entry node: tag, down, row, col, right, value
Head node i is the head node for both row i and column i.
down
tag
next
Head node
right
down tag row col right
f
value
Entry node
0 0 11 0
12 0 0
0
0 4 0
0
0
0
0
15
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
i j
aij
Setup for aij
A 4x4 sparse
matrix
4-52
Linked List for Sparse Matrix (cont.)
Matrix
head
4 4
H0
H1
H3
0 2
11
H0
H1
H2
1 0
12
H2
H3
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
2 1
-4
3 3
-15
4-53
Doubly Linked List
Motivation: Problem of singly linked lists
To efficiently delete a node, we need to know its
preceding node
It’s hard to find the node precedes a node ptr.
Solution: using doubly linked list
Each node in a doubly linked list has at least three fields
left link field (llink)
data field (item)
right link field (rlink)
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
llink data
rlink
4-54
Doubly Linked List (cont.)
A head node is also used to implement operations
more easily.
llink item rlink
Head Node
llink item rlink
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Empty List
4-55
Doubly Linked List (cont.)
Deletion from a doubly linked circular list
Head Node
llink item rlink
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-56
Doubly Linked List (cont.)
Insertion into a doubly linked circular list
node
node
newnode
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4-57