been-chian chien,wei-pang yang and wen-yang lin 4-1 introduction to data structure chapter 4 linked...
TRANSCRIPT
4-1Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Introduction to Data Structure
CHAPTER 4
LINKED LISTS
4.1 Pointers
4.2 Singly Linked Lists
4.3 Dynamic Linked Stacks and Queues4.4 Polynomials
4.5 Additional List Operations
4.6 Equivalence Relations
4.7 Sparse Matrices
4.8 Doubly Linked Lists
4-2Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 1 Basic Concepts
Chapter 2 Arrays
Chapter 3 Stacks and Queues
Chapter 4 Linked Lists
Chapter 5 Trees
Chapter 6 Graph
Chapter 7 Sorting
Chapter 8 Hashing
Chapter 9 Heap Structures
Chapter 10 Search Structures
Contents
4-3Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Pointer in C
int i, *pi;pi = &i; i=10 or *pi=10
pi = malloc(sizeof(int)); /* assign to pi a pointer to int */
10
i
pi
pi
4.1 Pointer
i piSome integer
int i, *pi;
4-4Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4.2 Singly Linked Lists
Ordered Lists Seq.
eg.
Non-seq. mapping: Insert “D”
Data
Link
Sequential mapping (not suitable for insertion & deletion)Non-sequential: linked
A C E G X Y
insert D
Operations - length- read all - retrieve i-th- store i-th- Insert- delete A C D E G X Y
A C E G X Y D
2 3 4 5 6 0 3
insert 1 2 3 4 5 6 7
1 2 3 4 5 6 7
7
head=1
4-5Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Non-seq. mapping: Delete “G”
Data
Link
Singly Linked Lists: Delete “G”
A C E G X Y D
1 2 3 4 5 6 7
head=1
2 7 4 5 6 0 3
1 2 3 4 5 6 7
delete
delete G
5
4-6Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Singly Linked Lists: Arrow Notation Arrow notation
Insert “D”
Delete “G”
1 2A C 0Y
head
1 2 6
……
2A 3C E
1 2 3
3D
7
71
head
G
4-7Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
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
Singly Linked Lists vs. Array
4-8Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
typedef struct list_node *list_pointer;typedef struct list_node { char data[3]; list_pointer link;};
data link
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
Example: ThreeLetterNode -- Class definition
data link
pointerdata type: array or char or …
data[0] data[1] data[2]
Some ThreeLetterNode
ThreeLetterNode
Structure
Defining a Linked List Node in C
4-9Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Example: A complex List Structure
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.1422
list_nodeblist_nodea
A possible configuration
The definition of Node A The definition of Node B
4-10Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
typedef struct list_nodea *nodea;typedef struct list_nodea { int data1; char data2; float data3; nodea linka; nodeb linkb;};
Example: A complex List Structure (cont.)
55
‘c’
3.14
list_nodea
Another possible configuration
The definition of Node A The definition of Node B
23
list_nodeb
22
typedef struct list_nodeb *nodeb; typedef struct list_nodeb { int data; nodeb linkb;};
4-11Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Creating a New Node in C Assume the following node structure
list_pointer ptr = NULL;
ptr = (list_pointer) malloc(sizeof(list_node));
ptrptr NULL
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
4-12Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Freeing a Node in C Assume the following node structure
free(ptr);
ptr
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
4-13Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Assume we have classes: ListNode and List
Some example list manipulation operations: create2( ) // Create two nodes insert( ) delete( ) Inverting a list concatenating two lists …
typedef struct list_node *list_pointer;typedef struct list_node { int data; list_pointer link;};list_pointer ptr = NULL; ptr …
List
list_node
Example List Manipulation Operations in C
4-14Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
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 20 0
10 0first
20 NL
20
20 NULL
20
p.143
4-15Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
void insert(list_pointer *ptr, list_pointer 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); } temp->data = 50; if (*ptr) { temp->link = node->link; node->link = temp; } else { /* list ptr is empty */ temp->link = NULL; *ptr = temp; }}
Example: Inserting a Node
node
50
10 20 NULL
temp
ptr . . .
50 NULLtemp
ptr
/* insert a new node with data = 50 into list ptr after node */p.144
4-16Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Example: Deleting a Node
void delete(list_pointer *ptr, list_pointer trail, list_pointer node) { if (trail) trail->link = node->link; else *ptr = (*ptr)->link; free(node); /* return the node to the system */}
10 20 NULL 50 ptr
nodetrail
/* delete the node after trail */
p.145
How about if trail = NULL?
4-17Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
BAT CAT SAT VAT NULLMAT
A dangling reference
ptr
trail
dangling reference
Delete a Node: A Dangling Reference
4-18Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
NULL
topelement link
(a) Linked Stack
NULL
frontelement link
(b) Linked Queue
rear
Stacks and Queues in Linked lists
F R
Note: Compare with sequence queue
4.3 Linked Stacks and Queues
4-19Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
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 10typedef 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 emptyIS_FULL(temp) iff the memory is full
top
top0
i.e. NULL = 0 null value
4-20Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Linked m Stacks and m Queues (cont.)
Representing m queues
#define MAX_QUEUES 10typedef 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 emptyIS_FULL(temp) iff the memory is full
front rear
front0
i.e. NULL = 0 null value
4-21Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
void add(stack_pointer *top, element item){ stack_pointer temp = (stack_pointer) malloc(sizeof (stack));
if (IS_FULL(temp)) { fprintf(stderr, “The memory is full\n”); exit(1); } temp->item = item; temp->link = *top;}
Add to a linked stack
Linked Stacks: Operations
p.149
top
temp
4-22Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Delete from a linked stack
Linked Stacks: Operations (cont.)
element delete(stack_pointer *top){ stack_pointer temp = *top; element item;
if (IS_EMPTY(temp)) { fprintf(stderr, “The memory is empty\n”); exit(1); } item = temp->item; *top = temp->link; free(temp); return item;}
p.149
top
temp
4-23Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Add into a linked queue
Linked Queues: Operations
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; *rear = temp;} p.151
front rear
temp
4-24Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Delete from a linked queue
Linked Queues: Operations (cont.)
element deleteq(queue_pointer *front){ queue_pointer temp = *front; element item;
if (IS_EMPTY(temp)) { fprintf(stderr, “The memory is empty\n”); exit(1); } item = temp->item; *front = temp->link; free(temp); return item;}
p.151
front rear temp
4-25Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
4.4 Polynomials Consider a polynomial
Tackle the reasonable-sized problem Node structure
e.g. A = 3x14 + 2x8 + 1
Note: in Array
00)( 0210101
e...e, e axa...xaxA m-miee
mm
term: represented by a node
coef exp link
3 14 2 8 1 0 0
^
A^
3 0 0 0 0 0 0 0 2 0 0 0 …… 1
4-26Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
typedef struct poly_node *poly_pointer;typedef struct poly_node { int coef; int expon; poly_pointer link;};
poly_pointer a, b, d;
021021 ...)( ee
me
m xaxaxaxA mm
Representation
Type declaration
p.152
coef exp linkpoly
Polynomial Representation
4-27Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Examples
a x x 3 2 114 8
b x x x 8 3 1014 10 6
3 14 2 8 1 0a
null
8 14 -3 10 10 6b
null
Polynomial Representation: Example
4-28Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Adding Polynomials: Figure 4:12 d = a + b
Case 1: a->exp = b->exp
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14
a
b
d
Adding Polynomials : d = a + b
d
4-29Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Adding Polynomials : d = a + b (cont.)
Case 2: a->exp < b->exp
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 -3 10
a
b
d
d
4-30Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 -3 10 2 8
Case 3: a->exp > b->exp
Adding Polynomials: d = a + b (cont.)
d
4-31Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Algorithm for adding two polynomials
Adding Polynomials: Algorithm
(C in Program 4.10, p.154)
……// copy rest of a…// copy rest of b…
4-32Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
(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).
Adding Polynomials: Complexity Analysis
4-33Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Consider a Polynomial expression:
polynomial a, b, d, e;
a = read_poly(); // read and create polynomial ab = read_poly(); // read and create polynomial bd = read_poly(); // read and create polynomial c temp = pmult(a, b);e = padd(temp, d);print_poly(e) ;
e (x) = a (x) * b (x) + d (x)
Before and after the above procedure been executed
Free all nodes?
Erasing Polynomials
4-34Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
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); }}
Erasing Polynomials (cont.)
3 14 2 8 1 0ptr null
temp
Erasing complexity: O(n), n: nonzero termsnot efficient!!
4-35Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Representing Polynomials as Circularly Linked Lists
The operation for erasing polynomial would be very efficient by maintaining our own list of freed node, i.e., free pool (see next slide)
Circular List Representation
a x x 3 2 114 8
3 14 2 8 1 0a
4-36Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Concept of polynomial erasing with free storage pool
ptr 143 2 8 1 0
avail
1
temp2
3
avail
Erasing complexity: Circularly List: O(1) Chain List: O(n), n: nonzero terms
Circular List Representation (cont.)
1. temp = ptr->link;2. ptr->link = avail;3. avail = temp;
p.159
4-37Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
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
n
storage pool
AV
4-38Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
STORAGE POOL: Array Method
Method 1: Array Method
procedure GETNODE(I)if AV > n then call NO_MORE_NODE;I AV;
AV AV + 1;end GETNODE
1 2 n
storage pool
AV
4-39Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
STORAGE POOL: Linked Method
Method 2: Linked Methodthe set of all nodes not currently in use is linked together.
Procedure GETNODE(x)if AV=0 then call NO_MORE_NODE;
x AV; AV LINK(AV);end
procedure RET(x) LINK(x) AV; AV x; end
AV
AV
X
4-40Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Problem with circular linked lists: How to represent zero polynomial?
Solution: introducing an additional Head Node
-1a
Zero polynomial
a x x 3 2 114 8
(1) Zero (need a head node)
(2) Non-zero Polynomial
3 14 2 8 1 0a
-1
Circular List Representation with Head
exp
4-41Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Circular with Head: d = a + b
a x x 3 2 114 8
3 14 2 8 1 0a
-1exp
8 14 -3 10 10 6b
-1exp
b x x x 8 3 1014 10 6
11 14 1 0d=a+b
-1exp
…
See p. 161, Program 4.16
4-42Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Algorithm for adding two polynomials
Adding Polynomials: Algorithm Comparison
(C in Program 4.10, p.154)
……// copy rest of a…// copy rest of b…
??? when ???
(C in Program 4.16, p.161)
………
Circularly with Head Node Chain Data Structure
…
4-43Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Linked Lists Operations create insert delete get_node ret_node invert concatenate
4.5 Additional List Operations
4-44Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Inverting a Chain
list_pointer invert(list_pointer lead){ list_pointer middle, trail; 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;}
first 1 2 3 7 NULL
first 7 6 5 1 NULL
Chain Linked List: Invert
p.164, Program 4.17
…
…
4-45Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
first 1 2 3 7 NULL
lead
middle NULL
first 1 2 3 7 NULL
leadmiddle
trail NULL
first 1 2 3 7 NULL
leadmiddle
trail = middle; middle = lead
lead = first; middle = NULL;
lead = lead->link;middle->link = trail;trail NULL
Chain Linked List: Invert (cont.)
…
…
…
4-46Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
trail
first 1 2 3 7 NULL
lead middle
lead = lead->link;middle->link = trail;
NULL trail = middle; middle = lead
trail = middle; middle = lead
trail
first 1 2 3 7 NULL
leadmiddleNULL
trail
first 1 2 3 7 NULL
lead middleNULL
Chain Linked List: Invert (cont.)
…
…
…
4-47Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
trail
first 1 2 3 7 NULL
leadmiddleNULL
lead = lead->link;middle->link = trail;
. . .
first 7 6 5 1 NULL
middle
lead NULL
first = middle;
Chain Linked List: Invert (cont.)
…
…
4-48Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
this 1 2 3 7 NULL
p
b 11 12 13 17 NULL
. . .
. . .
this 1 2 3 7
p
b 11 12 13 17 NULL
. . .
. . .
Chain Linked List: Concatenate
4-49Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
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. . .0
New x
first 1 2 3 7. . . 0
New xrearfront
first 1 2 3 7. . .
4-50Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Circular lists: with pointer to last node
last1 2 3 7. . .
1 2 3 7. . .0 last
New x
x->link = last->link;last->link = x;
Circular Lists: Adding New Node (cont.)
Add to front
Add to rear ???
4-51Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Inserting at the front [Program 4.19, p.165]
void insert_front(list_pointer *ptr, list_pointer node){ if (IS_EMPTY(*ptr) { /* empty List */ *ptr = node ; node->link = node ; } else { node->link = (*ptr)->link ; (*ptr)->link = node ; }}
ptr0
node
Circular Linked Lists (cont.)
1 2 3 7. . .0 ptrnode
ptr1 2 3 7. . .
4-52Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
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 rightnext value
down tag row col right
Head node Entry node
f i j
Setup for aij
aij
15000
0040
00012
01100
A 4x4 sparse matrix
4-53Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Linked List for Sparse Matrix (cont.)
4 4
0 2
1 0
2 1
3 3
-4
12
11
-15
H0
H1
H2
H3
H0 H1 H2 H3Matrix head
4-54Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Doubly Linked List
Motivation: Problem of singly linked lists To efficiently delete a node, we need to know its precedi
ng 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)
rlinkllink data
4-55Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Doubly Linked List (cont.)
A head node is also used to implement operations more easily.
llink item rlinkEmpty List
llink item rlinkHead Node
4-56Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Doubly Linked List (cont.)
Deletion from a doubly linked circular list
llink item rlinkHead Node