0% found this document useful (0 votes)
4 views

ch04 LinkedLists Nosolution

ch04_LinkedLists_noSolution

Uploaded by

ww03975
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

ch04 LinkedLists Nosolution

ch04_LinkedLists_noSolution

Uploaded by

ww03975
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 50

Data Structures

Chapter 4: Linked Lists

4-1
Singly Linked List

0 1 2 3 4 5 6 7 8
D A B C data
-1 5 6 1 link(pointer, address)
以 -1 代表結尾
(null pointer)

first = 2 header, external pointer


2 5 6 1
**
first null
node
data link

4-2
Operation on the First Node
first A B C nul
l
(1) adding ‘F’ to the front of the list
first
F A B C nul
l
p = getnode(); // create a new node
data(p) = ‘F’;
link(p) = first;
first = p;

(2) removing the first node of the list


first B C nul
l

4-3
**
Normal Way to Draw a Linked List

c a e d b

first
first
N
U
a b c d e L
L

link field ( 鏈結欄位 ) of a node, points to


next node
data field ( 資料欄位 ) of a node
4-4
Chain/Singly Linked List
• A chain ( 鏈 ), also called a singly linked list
( 單一鏈結串列 ), is a linked list in which each
node ( 節點 ) represents one element.
• Each node has exactly one link or pointer
from one element to the next.
• The last node has a NULL (or 0) pointer
first
N
U
a b c d e L
4-5
L
Node Representation
// in C++ // in C
class ChainNode struct ChainNode
{ {
char data; char data;
ChainNode *link; struct ChainNode
} *link;
}

data link data link

4-6
Linked List Represented by C++

4-7
Construction of a 2-Node List

**

4-8
Inserting a Node after Node x

4-9
Deleting Node x

**

first y x
60 29 35 23 18 27
4-10
Template Class
• A chain (singly linked list) is a container
class ( 容器類別 ), and is therefore a good
candidate for implementation with
templates.
• Member functions of a linked list should be
general so that they can be applied to all
types of objects.
• The template mechanism can be used to
make a container class more reusable.

4-11
Template Definition of Class Chain

4-12
Inserting at the Tail of a List

4-13
Concatenation of Two Lists

4-14
Reversing a List

4-15
When Not to Reuse a Class
• Reusing a class is sometimes less efficient
than direct implementation.
• If the operations required by the application
are complex and specialized, and therefore
not offered by the class, then reusing
becomes difficult.

4-16
Circular List
first

BAT CAT EAT FAT GAT HAT

first
last
last

4-17
Inserting at the Front of a Circular List

4-18
Linked Stacks and Queues

top

front rear
0

Linked Queue
0

Linked Stack
4-19
Push and Pop of a Linked Stack

4-20
Push (Inserting Rear) of a Linked Queue

4-21
Pop (Deleting Front) of a Linked Queue

4-22
Revisit Polynomials

a.first 3 1 2 8 1 0 0
4
a 3 x14  2 x 8  1

b.first **

b 8 x14  3 x10  10 x 6
4-23
Definition of Class Polynomial

4-24
Addition of Two Polynomials (1)
• It is an easy way to represent a polynomial
by a linked list.
• Example of adding two polynomials a and
b
a.first 3 14 2 8 1 0 0

b.first 8 14 -3 10 10 6 0

q
c.first 11 14 0 (i) p->exp == q->exp

4-25
Addition of Two Polynomials (2)

a.first 3 14 2 8 1 0 0

b.first 8 14 -3 10 10 6 0

c.first 11 14 -3 10 0

(ii) p->exp < q->exp

4-26
Addition of Two Polynomials (3)

a.first 3 14 2 8 1 0 0

b.first 8 14 -3 10 10 6 0

q
c.first 11 14 -3 10 2 8 0

(iii) p->exp > q->exp

4-27
Properties of Relations
• For any polygon ( 多邊形 ) x, x ≡ x. Thus, ≡ is
reflexive ( 反身的,自反的 ).
• For any two polygons x and y, if x ≡ y, then
y ≡ x. Thus, the relation ≡ is symmetric ( 對
稱的 ).
• For any three polygons x, y, and z, if x ≡ y
and y ≡ z, then x ≡ z. The relation ≡ is
transitive ( 遞移的 ).

4-28
Equivalence Class ( 等價類 )
• Definition: A relation ≡ over a set S, is said
to be an equivalence relation over S iff it is
symmetric, reflexive, and transitive over S.
• Example: Suppose, there are 12 polygons 0
≡ 4, 3 ≡ 1, 6 ≡ 10, 8 ≡ 9, 7 ≡ 4, 6 ≡ 8, 3 ≡ 5,
2 ≡ 11, and 11 ≡ 0. Then they can be
partitioned into three equivalence classes:
{0, 2, 4, 7, 11}; {1, 3, 5}; {6, 8, 9, 10}

4-29
Pseudo Code for Equivalence Algorithm

4-30
Linked List Representation
Input: 0 ≡ 4, 3 ≡ 1, 6 ≡ 10, 8 ≡ 9, 7 ≡ 4,
6 ≡ 8, 3 ≡ 5, 2 ≡ 11, 11 ≡ 0

[0 [1 [2 [3 [4 [5 [6 [7 [8 [9 [10 [11
] ] ] ] ] ] ] ] ] ] ] ]

data 11 3 11 5 7 3 8 4 6 8 6 0
link 0 0 0 0 0 0

data 4 1 0 10 9 2
link 0 0 0 0 0 0

**
4-31
Summary of Equivalence Algorithm
• Two phases to determine equivalence class
– Phase 1: Equivalence pairs (i, j) are read in and
adjacency (linked) list of each object is built.
– Phase 2: Trace (output) the equivalence class
containing object i with stack (depth-first
search). Next find another object not yet
output, and repeat.
• Time complexity: Θ(m+n)
– n: # of objects
– m: # of pairs (relations)

4-32
Node Structure for Sparse Matrix

Header node Element node


2 0 0 0
4 0 0 3

0 0 0 0 A 54 sparse matrix
 
8 0 0 1
 0 0 6 0 4-33
Linked List for Sparse Matrix

2 0 0 0
4 0 0 3

0 0 0 0
 
8 0 0 1
 0 0 6 0

4-34
Class Definition of Sparse Matrix (1)

4-35
Class Definition of Sparse Matrix (2)

4-36
Doubly Linked List
• In a singly linked list, if we want to delete a node
ptr, we have to know the preceding ( 前面的 ) node of
ptr. Then we have to start from the beginning of the
list and to search until the node whose next (link) is
ptr is found.
• To efficiently delete a node, we need to know its
preceding node. Therefore, a doubly linked list is
useful.
• A node in a doubly linked list has at least three
fields: left, data, right.
• A header node may be used in a doubly linked list.

4-37
Doubly Linked List

left data right


Header Node

left data right


Empty List
Header Node 4-38
Deleting Node x in a Doubly Linked
Circular List
left data right
Header Node

x -> left -> right = x -> right;


x -> right -> left = x -> left;

4-39
Inserting Node p to the Right of Node
x in a Doubly Linked Circular List
left data right
Header
Node

x q
p
q = x -> right;
p -> left = x;
p -> right = q;
q -> left = p;
x -> right = p; 4-40
Generalized Lists
• A generalized list, A, is a finite sequence of n ≥ 0
elements, a0, a1, a2, …, an-1, where i, is either an
atom or a list. The elements i,0 ≤ i ≤ n – 1, that
are not atoms are said to be the sublists of A.
• A list A is written as A = (a0, …, an-1 ), and the
length of the list is n.
• A list name is represented by a capital letter and
an atom is represented by a lowercase letter.
• a0 is the head of list A and the rest (a1, …,
an-1) is the tail of list A.

4-41
Examples of Generalized Lists
• A = ( ): the null, or empty, list; its length is zero.
• B = (a, (b, c)): a list of length two; its first
element is the atom a, and its second element is
the linear list (b, c).
• C = (B, B, ( )): A list of length three whose first
two elements are the list B, and the third element
is the null list.
• D = (a, D): is a recursive list of length two; D
corresponds to the infinite list D = (a, (a, (a,
…))).
• head(B) = ‘a’ and tail(B) = (b, c), head(tail(C))=B
and tail(tail(C)) = ( ).
• Lists may be shared by other lists.
• Lists may be recursive. 4-42
General Polynomial
p ( x, y, z )  x10 y 3 z 2  2 x 8 y 3 z 2  3 x 8 y 2 z 2  x 4 y 4 z  6 x 3 y 4 z  2 yz
• P(x, y, z)=
(( x10  2 x 8 ) y 3  3 x 8 y 2 ) z 2  (( x 4  6 x 3 ) y 4  2 y ) z
• Rewritten as Cz2 + Dz, where C and D are
polynomials.
• Again, in C, it is of the form Ey3 + Fy2, where E and
F are polynomials.
• In general, every polynomial consists of a variable
plus coefficient-exponent pairs. Each coefficient may
be a constant or a polynomial.
4-43
PolyNode Class in C++

4-44
3 Types of Nodes in PolyNode
• trio == var
– The node is a header node.
– vble indicates the name of the variable
– exp is set to 0.
• trio == ptr
– Coefficient is a list pointed by down.
– exp is the exponent of the variable on which that list is
based on.
• trio == no
– Coefficient is an integer, stored in coef.
– exp is the exponent of the variable on which that list is
based on.
4-45
Representation of 3x2y

trio vble exp next trio down exp next


var y 0 ptr 1 0

trio coef exp next


var x 0 no 3 2 0
First

4-46
Representation of P(x, y, z)
10 8 3 8 2 2 4 3 4
(( x  2 x ) y  3 x y ) z  (( x  6 x ) y  2 y ) z
P(x, y, z)

v z 0 p 2 p 1 0

v y 0 p 3 p 2 0 v y 0 p 4 p 1 0

v x 0 n 3 8 0 v x 0 n 2 0 0

v x 0 n 1 10 n 2 8 0 v x 0 n 1 4 n 6 3 0

4-47
Representations of Generalized Lists

A = 0 Empty list
B=(a, (b,
B f a t 0 c))

f b f c 0

C t t t 0 0 C=(B, B,
( ))
D f a t 0 D=(a, D)
**
4-48
Reference Counts in Shared Lists
• Lists may be shared by other lists for
storage saving.
• Add a header node to store the reference
count, which is the number of pointers to it.
• The reference count can be dynamically
updated. The list can be deleted only when
the reference count is 0.

4-49
Reference Counts in Shared Lists
header node
A f 1 0

B f 3 f a t 0 B=(a, (b, c))

f 1 f b f c 0

C f 1 t t t 0 C=(B, B,
( ))
D f 2 f a t 0 f 1 0 D=(a, D)
reference count in header node: # of
pointers to the list 4-50

You might also like