3. 3
1
1.1
(structure)
(member) 1
struct p o i n t {
int x ;
int y ;
};
y x
1
tgif
1 &
1.1
1.2
int char
point struct point
/∗ s t r u c t p o i n t pt ∗/
struct p o i n t pt ;
/∗ s t r u c t p o i n t maxpt
point
x 320 y 200
4. 4 1
x y
1.1 point
∗/
struct p o i n t maxpt = { 3 2 0 , 2 0 0 } ;
/∗ ∗/
struct f l o a t p o i n t {
float x ;
float y ;
} float pt ;
(variable) (
) (value) (type) *1 int i; ( )32
*2 i i
i int
pt 1.1 struct point pt struct
point ( 32 +32 =64 ) pt
struct point struct point
2 (320, 200)
1.3
x
(structure member operator) “ . ”
• 1 *3
• 2
struct p o i n t maxpt = { 3 2 0 , 2 0 0 } ;
*1 (domain)
*2 32
*3 {320, 200}.x
5. 1.4 5
/∗ x ∗/
p r i n t f ( ”%dn” , maxpt . x ) ;
/∗ y ∗/
maxpt . y = 2 4 0 ;
1.4
struct p o i n t p t s [ 1 0 0 ] ;
/∗ 3 pts1 ∗/
struct p o i n t p t s 1 [ ] =
{{320 , 200} ,
{150 , 500} ,
{0 , 0}};
/∗ p t s 1 struct point ∗/
pts1 [ 1 ] . y = 240;
1.5
1.5.1
( )
1 1 *4
C 2
• (address operator) &
• (indirection operator) * *5
X X X* int *pi; pi
int *6 ( 1.2)
*4 +
*5 C
segmentation fault bus error
*6 C segmentation fault bus error
6. 6 1
pi
8632
8632 132 (int )
1.2 int *pi;
1.5.2
2
• struct point pt; pt struct point (64 )
(320, 200) 2
• struct point *p; p ( 32 )
struct point
• struct point *p = (struct point *)NULL;
C NULL 0 32
p 0 0 int p
(struct point *)NULL 0 ( , cast)
• struct point *p = (struct point *)malloc(sizeof(struct point));
p
malloc()
– *7 ( )
– void *
– ( )
sizeof ( !)
sizeof(struct point) = 8(8 =64 )
malloc() sizeof(struct point)
struct point * p ( 1.3) malloc()
1.5.3
1.3 x
8236 *p x (*p).x
. *
*7
7. 1.6 7
p (struct point *
8632
8632
malloc(sizeof(struct point))
1.3
1.4 point2
C *8 C
(*p).x p -> x 2
-> a -> b -> c (a -> b) -> c
1.6
( )
struct p o i n t 2 {
int x ;
int y ;
struct p o i n t 2 ∗ next ;
};
struct p o i n t 2 ∗pp ;
1.4 next struct point2
pp p ->
next x (pp -> next) -> x pp -> next
-> x *9
*8 Lisp
*9 ->
8. 8 1
1.7 :
1.2 C strcpy, strcmp, strcat
•
•
1.3 1.7 strcpy, strcmp, strcat
my strcpy, my strcmp, my strcat
strcpy my strcpy
my strcpy, my strcmp, my strcat main()
1.8 :
1.8.1
• 3
1. char[256]
2. char[16]
3. int
• 5
•
•
• 0
•
1.8.2
1.4 1.8.1 3 name, phone,
number *10
1. ( )
void add(char *, char *, int);
*10 name, phone, number 0 0 (
) 1
12. 12 2
F IRST (L) L
EN D(L) L
IN SERT (x, p, L) x L p L p 1
L p
LOCAT E(x, L) L x 2
RET RIEV E(p, L) p L L p
DELET E(p, L) p L L p
N EXT (p, L), P REV IEOU S(p, L) p L
N EXT (EN D(L), L) P REV IOU S(F IRST (L), L) L
p N EXT , P REV IOU S
M AKEN U LL()
P RIN T LIST (L) L
2.1
nil
cons(x, L) L x
hd(L) L
tl(L) L
L@M L M
2.2 ML
2.3.1
2.3
[ a1, a2, ...., an]
2.3
1
13. 2.4 13
2.3.2 (1)
( ) 2.4
header
a1 a2 a3
header
2.4
2.3.3 (2)
Lisp ML [[1, 2, 3], [4, 5], nil]
ML 2.5
head tail
(car) (cdr)
cell
1 3 5
2.5 (2)
*4
2.4
*4 I
14. 14 2
1 ( )
personal_data next
struct personal_data {
char name[256];
char phone[16];
int number;
};
2 struct personal_data 2
struct personal_data * a, b
struct personal_data *a, *b;
/* malloc()
a, b
*/
a( ) b( )
3 2 a( ) printf()
( b )
2.5 :
2.5.1
C C
• ( )
( 2.4 )
• int 1
• NULL
NULL
• 1 ( )
C
1
• 11
2.1
15. 2.5 : 15
•
list.h list.c list-test.c
make
next
2.5.2
2.1 int 1 (struct cell)
2.2
struct cell *makeNullList(void);
malloc
*5
NULL ,
/*
* Make null list and return pointer to list.
*/
struct cell *makeNullList(void)
{
struct cell *list; /* pointer to new list */
/* allocate memory for new list entry */
list = (struct cell*)malloc(sizeof(struct cell));
/* check the allocation succeeded or failed */
if (list == (struct cell*)NULL) {
fprintf(stderr, makeNullList: can not allocate memory for new list.n);
exit(1);
}
/* set next cell’s position to NULL */
list-next = (struct cell*)NULL;
return list; /* return pointer to list */
}
2.3 nextCell
struct cell *nextCell(struct cell *, struct cell *);
1 2
*5
19. 19
3
( )
3.1
3.1.1
*1
LIFO(Last-In First-Out)
3.1
M AKEN U LL(S) S
T OP (S) S
a
P OP (S) S ( )
P U SH(x, S) x S ( )
EM P T Y (S) S
a
3.1
3.1.2
T OP (S)
RET RIEV E(F IRST (S), S) P U SH(x, S)
*1
20. 20 3
x IN SERT (x, F IRST (S), S)
II
3.1.3
elements[0 · · · 99] length
T OP (S) elements[length − 1] P U SH(x, S)
push(x, S) {
if (length == 100) {
;
} else {
elements[length++] = x;
}
}
3.2 ( )
3.2.1
( )
FIFO (First-In First-Out)
3.2
M AKEN U LL(Q) Q
F RON T (Q) Q
EN QU EU E(x, Q) x Q
DEQU EU E(Q) Q Q
EM P T Y (Q) Q
3.2
21. 3.3 21
3.2.2
F RON T (Q) RET RIEV E(F IRST (Q), Q)
EN QU EU E(x, Q) x IN SERT (x, EN D(Q), Q)
3.2.3
EN QU EU E(x, Q)
DEQU EU E(Q)
elements[0 · · · 99]
elements[99] elements[0]
f ront length EN QU EU E(x, Q)
ENQUEUE(x, Q) {
if (length == 100) {
;
} else {
elements[(front + length) % 100] = x;
length ++;
}
}
3.3
3.3.1
•
•
•
• int
• (MAKENULL (TOP (POP
(PUSH (EMPTY 6
22. 22 3
stack.h
stack.c stack-test.c make
3.3.2
3.1 (
struct cell *makeNullStack(void);
/*
* Make null stack and return pointer to stack
*/
struct cell *makeNullStack(void)
{
return makeNullList(); /* call makeNullList() */
}
3.2 (1 (0
int emptyStack(struct cell*);
3.3
int topStack(struct cell*);
/*
* Return data of the top stack
*/
int topStack(struct cell *stack)
{
return retrieveCell(firstCell(stack), stack);
}
3.4
27. 27
4
4.1
f f f (recursive function)
f f
f 1 f (linear recursive) 2 f
(non-linear recursive)
2
1. (basis)
2. (induction step)
*1
4.1.1
ML
n
• n=0 1
• n≥1 n ∗ (n − 1)!
ML
fun factorial(n) =
if n = 0 then 1 else n * factorial(n - 1);
L
• L
• L tl(L) hd(L)
*1
28. 28 4
hd(L), tl(L) L
ML
fun reverse(L) =
if L = nil then nil
else reverse(tl(L)) @ [hd(L)];
4.1.2
•
0( 1) nil
•
n−1 tl(L)
( )
4.2
4.2.1
4.1 1 int sigma(int);
sigma(n) = n + sigma(n − 1)
= n + (n − 1) + sigma(n − 2)
.
.
.
= n + (n − 1) + · · · + sigma(1)
= n + (n − 1) + · · · + 1
int sigma(int num)
{
int result; /* calculation result */
if (num = 1) {
result = num;
} else {
result = num + sigma(num - 1);
}
return result;
}
29. 4.2 29
main
int main(int argc, char *argv[])
{
/* check arguments */
if (argc != 2) {
fprintf(stderr, Usage: %s numn, argv[0]);
exit(1);
}
/* check argument sign */
if (atoi(argv[1]) 0) {
fprintf(stderr, Error: The argument must be a positive value.n);
exit(1);
}
/* calculation and print result */
printf(sigma(%d) = %dn, atoi(argv[1]), sigma(atoi(argv[1])));
return 0;
}
4.1 ( )
4.2 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, . . . 2 1
2 5 2 3, 4
2 3 2+3=5
int fibonacci(int);
main()
f ibonacci(n) = f ibonacci(n − 1) + f ibonacci(n − 2)
= {f ibonacci(n − 2) + f ibonacci(n − 3)} + {f ibonacci(n − 3) + f ibonacci(n − 4)}
.
.
.
4.2.2
4.3
struct cell *reverseList(struct cell*); main()
*2
reverseList([3, 6, 4, 5, 2]) = reverseList([6, 4, 5, 2])@[3](@ )
= reverseList([4, 5, 2])@[6]@[3]
.
.
.
= [2, 5, 4, 6, 3]
*2 printList(list)
33. 33
5
( )
(depth first search)
(preorder) (inorder) (postorder) 3
5.1 2
2 2 2
( 0 1 2)
2 5.1
N ode CREAT EN ODE(Label label, N ode lef t, N ode right) label lef t
right 2
N ode LEF T N ODE(N ode n) n
N ode RIGHT N ODE(N ode n) n
5.1 2
2 2
•
• NULL
• NULL
37. 37
6
2
10,000 1 10,000
1 *1
2 (binary search tree) 2
2
6.1
2 1 2 *2
• x x x
• x x x
6.1 2
ML
as types
a in
6.1 2
*1 O(n) ( n )
*2 (total order)
38. 38 6 2
6.2
2 6.2
M EM BER(x, A) x 2 A
IN SERT (x, A) x 2 A 2
M IN (A) 2 A
DELET E(x, A) x 2 A 2
6.2 2
M EM BER(x, A)
boolean MEMBER(x, A) {
if (A == nil) {
return false; /* 2 */
} else if (x == A.iData) {
return true; /* A x */
} else if (x A.iData) {
return MEMBER(x, A.leftTree); /* x */
} else { /* x A.iData */
return MEMBER(x, A.rightTree); /* x */
}
}
IN SERT (x, A), DELET E(x, A) 2
DELET E(x, A)
39. 6.3 39
1. A (A x )
2. A
a x A.iData x A x
2 A
b x A.iData x A x
2 A
c x == A.iData A
i. A ( ) A
ii. A A 2 A
iii. A A 2 A
iv. A y
x 2 y A.lef tT ree
DELET E(y, A.rigthT ree)
6.3
6.3.1
6.3.2
6.1
int BS member(int, struct node*);
1 0
/***
*** test that a node that has specified label is in the tree
***/
int BS_member(int data, struct node *tree)
{
int ret = 0; /* result (0: not member, 1: member) */
if (tree != (struct node*)NULL) {
/* tree
data */
}
return ret;
}
40. 40 6 2
6.2
struct node *BS insert(int, struct node*);
*3
/***
*** insert a node that has specified label
***/
struct node *BS_insert(int data, struct node *tree)
{
if (tree == (struct node*)NULL) {
/* tree */
} else {
/* tree */
}
return tree;
}
6.3
struct node *BS min(struct node*);
/***
*** get a node that has minimum label in the tree
***/
struct node *BS_min(struct node *tree)
{
struct node *min = (struct node*)NULL; /* pointer to minimum node (initially NULL) */
if (tree != (struct node*)NULL) { /* check tree is exist or not */
if (leftNode(tree) == (struct node*)NULL) {
/* tree */
} else {
/* tree */
}
}
return min;
}
6.4
struct node *BS delete(int, struct node*);
*3 NULL
41. 6.4 41
/***
*** delete a node that has a label equals to specified label
***/
struct node *BS_delete(int data, struct node *tree)
{
int tmp; /* temporary data */
if (tree != (struct node*)NULL) { /* check tree is exist or not */
if (labelNode(tree) data) {
/* data tree */
} else if (labelNode(tree) data) {
/* data tree */
} else {
/* data is equal to this node, then delete this node */
if ((leftNode(tree) == (struct node*)NULL)
(rightNode(tree) == (struct node*)NULL)) {
/* tree */
} else if (leftNode(tree) == (struct node*)NULL) {
/* tree */
} else if (rightNode(tree) == (struct node*)NULL) {
/* tree */
} else {
/* tree */
}
}
}
return tree;
}
6.5
void BS print(struct node*);
6.6
void BS destroy(struct node*);
6.7 main()
6.4
6.8
6.9 6 ( { | } )
6