4-1 Been-Chian Chien,Wei-Pang Yang and Wen-Yang L in Introduction to Data Structure CHAPTER 4 LINKED LISTS 4.1 Pointers 4.2 Singly Linked Lists 4.3 Dynamic Linked Stacks and Queues 4.4 Polynomials 4.5 Additional List Operations 4.6 Equivalence Relations 4.7 Sparse Matrices 4.8 Doubly Linked Lists
57
Embed
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 4-1 Introduction to Data Structure CHAPTER 4 LINKED LISTS 4.1 Pointers 4.2 Singly Linked Lists 4.3 Dynamic.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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
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); }}