# 投影片 1 by dandanhuanghuang

VIEWS: 22 PAGES: 30

• pg 1
```									資料結構 (Data Structure)

資訊工程學系副教授
http://www.twcrypt.org
wcyang@isu.edu.tw
2007.09~2008.01
CSIE: Data Structure, Introduction

Outline
1.   Singly Linked Lists and Chains
2.   Representing Chains in C
4.   Polynomials
6.   Equivalence Classes
7.   Sparse Matrices

Sunday, March 04, 2012                           Wu-Chuan Yang, 2/30
CSIE: Data Structure, Introduction

Top

…

Rear
Front

…

NULL

Sunday, March 04, 2012                                  Wu-Chuan Yang, 3/30
CSIE: Data Structure, Introduction

top

…

typedef struct stack *stack_pointer;
typedef struct {                       typedef struct stack {
int key;                               element item;
/* other fields */                     stack_pointer link;
} element;                             };
stack_pointer top;
Sunday, March 04, 2012                                            Wu-Chuan Yang, 4/30
CSIE: Data Structure, Introduction

Example: Push and Pop in a linked stack

top

10                20                 30   …   70

Push:
top = temp;
XXX                          Pop:
temp = top;

Sunday, March 04, 2012                                        Wu-Chuan Yang, 5/30
CSIE: Data Structure, Introduction

4. Polynomials
Polynomial Representation
typedef struct poly_node *poly_pointer;
typedef struct poly_node {
int coef;
int expon;
};
poly_pointer a, b, c;

A( x)  am 1 x            em1
 am  2 x    em2
    a0 x    e0

Sunday, March 04, 2012                                             Wu-Chuan Yang, 6/30
CSIE: Data Structure, Introduction

Example: Polynomial Representations
a  3x14  2 x8  1
b  8x14  3x10  10 x6

3 14               2    8            1   0

a                                                  NULL

8 14               -3 10            10 6

b                                                 NULL

Sunday, March 04, 2012                                         Wu-Chuan Yang, 7/30
CSIE: Data Structure, Introduction

 a->expon == b->expon
   d->coef = a->coef + b->coef;
   d = d->link;              a  3x14  2 x8  1
 a->expon < b->expon                         b  8 x14  3x10  10 x 6
 d->coef = b->coef;                     d  11x14  3x10  2 x8  10 x 6  1
 a->expon > b->expon
 d->coef = a->coef;

Sunday, March 04, 2012                                                 Wu-Chuan Yang, 8/30
CSIE: Data Structure, Introduction

 Invert (Reverse) a list
 Concatenate two list
 Operations for Circular linked list

Sunday, March 04, 2012                          Wu-Chuan Yang, 9/30
CSIE: Data Structure, Introduction

NULL

10                 20              30   …    70

middle
temp = middle;

Sunday, March 04, 2012                                   Wu-Chuan Yang, 10/30
CSIE: Data Structure, Introduction

4)

10              20               30

1) temp = middle;
1) temp                       2)             4) middle->link = temp;

Sunday, March 04, 2012                                           Wu-Chuan Yang, 11/30
CSIE: Data Structure, Introduction

 Example: In compute e(x) =a(x)*b(x)+d(x)
 Circular linked list may be used.
• Chain: A list with NULL ending (not a circular list).
 The operations in circular linked list should be modified.

b          a

10                20          …       70

a

-                10              20       …          70

Sunday, March 04, 2012                                              Wu-Chuan Yang, 12/30
CSIE: Data Structure, Introduction

 All operations in linked list should be modified for
   create
   insert (insert_front)
   delete
   length
   …

Sunday, March 04, 2012                             Wu-Chuan Yang, 13/30
CSIE: Data Structure, Introduction

6. Equivalence Classes
 Equivalence relations
 A relation over a set S, is to be an equivalence relation over S,
iff it is symmetric, reflexive, and transitive over S.
• Symmetric  x = x
• Reflexive  if x = y, then y = x.
• Transitive  if x = y and y = z, then x = z.
 Example
• 0=4, 3=1, 6=10, 8=9, 7=4, 6=8, 3=5, 2=11, 11=0
• three equivalent classes {0,2,4,7,11}, {1,3,5}, {6,8,9,10}

 What kind of data structure should be adopted?

Sunday, March 04, 2012                                           Wu-Chuan Yang, 14/30
CSIE: Data Structure, Introduction

Example of equivalence classes
• 0=4, 3=1, 6=10, 8=9, 7=4, 6=8, 3=5, 2=11, 11=0
• three equivalent classes {0,2,4,7,11}, {1,3,5}, {6,8,9,10}

[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

Sunday, March 04, 2012                                                    Wu-Chuan Yang, 15/30
CSIE: Data Structure, Introduction

7. Sparse Matrices
 Sparse matrices
 # of non-zeros is very less than # of zeros.
 Circular linked list can be used.

0  0 11 0 
12 0 0  0 
           
0 4 0  0 
           
 0 0 0  15

Sunday, March 04, 2012                                    Wu-Chuan Yang, 16/30
CSIE: Data Structure, Introduction

Data structure of sparse matrix

First column                         next

down entry        row      col    right

value

entry       i        j

aij

Sunday, March 04, 2012                                                    Wu-Chuan Yang, 17/30
CSIE: Data Structure, Introduction

Example of sparse matrices

Matrix                            H0          H1    H2          H3

0 2
H0
11

1 0
H1
12

2 1
H2
-4

3 3
H3
-15

Sunday, March 04, 2012                                       Wu-Chuan Yang, 18/30
CSIE: Data Structure, Introduction

 Move in forward and backward direction.
• Singly linked list is in forward direction only.
 Every node should use 2 links (left and right).
 The operations of list should be modified.

data

Sunday, March 04, 2012                                           Wu-Chuan Yang, 19/30
CSIE: Data Structure, Introduction

Data Structure of doubly linked list

typedef struct node *node_pointer;
typedef struct node {
element item;
}

empty

Sunday, March 04, 2012                          Wu-Chuan Yang, 20/30
CSIE: Data Structure, Introduction

a

b

3)
a                      4)

1)            b        2)

Sunday, March 04, 2012                                   Wu-Chuan Yang, 21/30
CSIE: Data Structure, Introduction

a                      b    c

1)
a                           c
2)

b

3) free(b)

Sunday, March 04, 2012                                   Wu-Chuan Yang, 22/30
CSIE: Data Structure, Introduction

Example: Recursive
 Definition
 functions in which the function being defined(called) is applied
within its own definition.
• Direct: A call A
• Indirect: A call B, B call A
 Ending condition should be defined.
 Source code is very compress

A Sierpinski triangle
Sunday, March 04, 2012                                              Wu-Chuan Yang, 23/30
CSIE: Data Structure, Introduction

Example: Tower of Hanoi
 move the entire stack to another peg, obeying the
following rules:
 Only one disk may be moved at a time.
 Each move consists of taking the upper disk from one of the
pegs and sliding it onto another peg, on top of the other disks
that may already be present on that peg.
 No disk may be placed on top of a smaller disk.

The figure is from wikipedia
Sunday, March 04, 2012                                          Wu-Chuan Yang, 24/30
CSIE: Data Structure, Introduction

Solution of Tower of Hanoi (1/3)
 Top (n-1) disks are from A to B

A                                 B   C

Sunday, March 04, 2012                              Wu-Chuan Yang, 25/30
CSIE: Data Structure, Introduction

Solution of Tower of Hanoi (2/3)
 Top (n-1) disks are from A to B
 The n-th disk is from A to C.

A                                 B   C

Sunday, March 04, 2012                              Wu-Chuan Yang, 26/30
CSIE: Data Structure, Introduction

Solution of Tower of Hanoi (3/3)
 Top (n-1) disks are from A to B
 The n-th disk is from A to C.
 Top (n-1) disks are from B to C

A                                 B   C

Sunday, March 04, 2012                              Wu-Chuan Yang, 27/30
CSIE: Data Structure, Introduction

Source code of Tower of Hanoi
#include <stdio.h>

void hanoi(int n, char a, char b, char c) {
if(n==1)
printf("Disk %d: %c -->%c\n",n,a,c);
else {
hanoi(n-1,a,c,b);
printf("Disk %d: %c -->%c\n",n,a,c);
hanoi(n-1,b,a,c);
}
}

main() {
hanoi(3,'A','B','C');
}

Sunday, March 04, 2012                                 Wu-Chuan Yang, 28/30
CSIE: Data Structure, Introduction

Analysis of Tower of Hanoi
Hanoi
3, A B C

4

Hanoi                   3: AC              Hanoi
2, A C B                                    2, B A C

2                                          6

Hanoi                      Hanoi              Hanoi                  Hanoi
2: AB                                      2: BC
1, A B C                   1, C B A           1, B C A               1, A B C
1                           3               5                         7

1: AC                     1: CB             1: BA                  1: AC

Sunday, March 04, 2012                                                   Wu-Chuan Yang, 29/30
CSIE: Data Structure, Introduction

How many steps of Tower of Hanoi?

A model set of the Towers of Hanoi (with 8 disks)

 2n–1 steps

Sunday, March 04, 2012                                              Wu-Chuan Yang, 30/30

```
To top