SlideShare ist ein Scribd-Unternehmen logo
1 von 41
Downloaden Sie, um offline zu lesen
IB
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                                                                                                                      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
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                                                                                                                                                  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
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                                                                                                           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
1.8             :                                                               9

  2. (                               )
                    void delByName(char *);
                    void delByPhone(char *);
                    void delByNumber(int);
                        delByName()
           delByPhone(), delByNumber()
  3. (                         )
                    void findByName(char *);
                    void findByPhone(char *);
                    void findByNumber(int);
                        findByName()
           findByPhone(), findByNumber()


  4. (                         )
                    void printAll();




      •
      •                                   strcmp
      •                                   name phone number

          1.5           1.4                                      main()

      •                                  main()           1.4(   1.7)
                                                                          *11

      •                        1.4
           main()




          1.6 1.8.1

          1.7            1.4
                                                   1.6


          1.8           1.7                                      main()




 *11                1
10          1



1.9
      1.9
11




          2




2.1
                                                 0                                               *1

                   [a1 , a2 , · · · , an ]                                                                                   *2

                                                                                                                        []         *3

                                                                    (total order)
          2
                                                                                                                       [1, 3, 2]



      •                                  : 1(     1       ), 3(      2       ), 2(   3       )
      •                           : 1(       1        ), 2(   3          ), 3(   2       )



2.2




                                                              2.1
                                                                                     ML               2.2




2.3


                                                                                                            Lisp, ML, Prolog, Java
                             C


  *1
  *2                         I           ML
  *3   Lisp   ML                          “nil”
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
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                                                                                                                            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
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
16                                                                                     2
                                                                                          
                a


 /*
  * Return pointer to the next cell.
  */
 struct cell *nextCell(struct cell *target, struct cell *list)
 {
   struct cell *next = (struct cell*)NULL;



          if (target != (struct cell*)NULL) {
            next = target-next;
          }

          return next;
 }

           a

                                                                                          

          2.4

           struct cell *firstCell(struct cell *);
           struct cell *endCell(struct cell *);



                    NULL

          2.5                                                    previousCell     *6


           struct cell *previousCell(struct cell*, struct cell*);

     1                                           2

          2.6                                                        insertCell

           struct cell *insertCell(int, struct cell *, struct cell *);

     1                         2                                            3
                                                     2


                                                         (   2   )         NULL   *7

                     malloc

          2.7                                                     deleteCell


           struct cell *deleteCell(struct cell*, struct cell*);


     *6
     *7   nextCell(endCell(list), list)   NULL
2.5           :                                                                                    17

  1                                                2


       2.8                                                retrieveCell                      NULL
                  ,0

        int retrieveCell(struct cell*, struct cell*);

  1                                                                2



       2.9                                                                locateCell
                         NULL                                                           1



        struct cell *locateCell(int, struct cell*);

  1                                       2
                                                                                                   


  /*
   * return position of the cell has data equals to specified as iData
   */
  struct cell *locateCell(int data, struct cell *list)
  {
    struct cell *current; /* current cell’s pointer */


      /* set pointer to current cell as pointer to first cell */
      current = firstCell(list);

      /* while current cell’s data is not data, pursue the next cell */
      while ((current != (struct cell*)NULL)
            (retrieveCell(current, list) != data)) {
        current = nextCell(current, list);
      }

      return current;   /* return end cell’s pointer */
  }
                                                                                                   

       2.10                           deleteCell
deleteList

        void deleteList(struct cell*);

  1

       2.11                                                  printList                              [
1, 2, 3, 4, 5 ]

        void printList(struct cell*);

  1

       2.12                                                                    main()
18           2



2.6
      2.13
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                                                                                  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
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                                                                           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
3.4            :        (     )                                                        23

          int popStack(struct cell*);



      3.5

          void pushStack(int, struct cell*);



      3.6


          void deleteStack(struct cell*);



      3.7                                                          main()



3.4                 :          (              )
3.4.1

      •
      •
      •
      •              int
      •                                 MAKENULL                       FRONT
          DEQUEUE                       ENQUEUE                    EMPTY
          6

                                                                               queue.h
                                                  queue.c   queue-test.c        make




      3.8

          struct cell *makeNullQueue(void);



      3.9                          1      0           emptyQueue


          int emptyQueue(struct cell*);
24                                                                        3

      3.10                                             frontQueue


       int frontQueue(struct cell*);

      3.11                                                            dequeue


       int dequeue(struct cell*);

      3.12                                             enqueue


       void enqueue(int, struct cell*);

      3.13

       void deleleteQueue(struct cell*);



      3.14                                                   main()



3.5              :
      3.15



       struct array_stack *makeNullStack(void);
       int emptyStack(struct array_stack*);
       int topStack(struct array_stack*);
       int popStack(struct array_stack*);
       void pushStack(int, struct array_stack*);
       void deleteStack(struct array_stack*);

              struct array_stack

#define MAX_STACK_SIZE 10
struct array_stack {
  int length;                       /*             (      ) */
  int elements[MAX_STACK_SIZE];     /*                           */
};

                             int                                 0
MAX_STACK_SIZE

      3.16
3.6           :                                                                                                                      25

           struct array_queue *makeNullQueue(void);
           int emptyQueue(struct array_queue*);
           int frontQueue(struct array_queue*);
           int dequeue(struct array_queue*);
           void enqueue(int, struct array_queue*);
           void deleteQueue(struct array_queue*);

                      struct array_queue

#define MAX_QUEUE_SIZE 10
struct array_queue {
  int front;                                         /*                                             */
  int length;                                        /*                       (       ) */
  int elements[MAX_QUEUE_SIZE];                      /*                                        */
};

                                        int                                                         MAX_QUEUE_SIZE-1
                         0                                          MAX_QUEUE_SIZE
MAX_QUEUE_SIZE



3.6               :

                             2                                  2                                        32 + 9, 4 / 2
                             infix notation                                                                                   2
                             prefix notation         2                                           postfix notation


                         E

      1. E                                      E                        E
      2.          2              op                 E1 op E2                        E1 E2 op                  E1 , E2            E1 , E2


      3. (E)                     E                          E   E

       3.17                           32 + 3, 9 + 4 ∗ 3, (1 + 2) ∗ 4

                                                        1                                                           1


      1.                                                                     push
      2.                         op                                  2               x, y                     pop   x op y




       3.18           3.17
26                                                                 3




         3.19

     •                                      1
                                          +, −, ∗, /   1
          1             1
                 1



     •
     •                      /

                                                           a.out


          % cat testdata.txt
          16 3 + 3 /
          -2 -
          % ./a.out testdata.txt
          8

                                3.1 3.6




3.7
         3.20
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                                                                                           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;
  }
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)
30                                                                                                                       4

          4.4                      list                         list1 list2


           void divideList(struct cell *list, struct cell *list1, struct cell *list2);

                list                           list1                             list2
                                                                  *3

                                           main()
           ) pcList = [1, 2, 3, 4, 5]
                            
                             list1 =
                                            [1]@([3, 4, 5]    divideList                       list1)
                            
                                      =      [1]@([3]@([5]      divideList                       list1))
                             list2 =
                                            [2]@([3, 4, 5]    divideList                       list2)
                            
                                      =      [2]@([4]@([5]      divideList                       list2))

          4.5               2

           void mergeList(struct cell *list1, struct cell *list2, struct cell *merged);

                list1            list2                                                               merged
                                                    list1         list2                                    *4

                                           main()
           ) list1 = [3, 6, 2] list2 = [4, 5, 7]

                         merged     = [3]@([6, 2] [4, 5, 7]   mergeList                          merged)
                                    = [3]@([4]@([6, 2] [5, 7]   mergeList                          merged))
                                    = ···

          4.6             divideList       mergeList
                   mergeSort

  1.                              divideList
  2.                                  mergeSort
  3.                         2      mergeList



                          list                                               sorted


           void mergeSort(struct cell *list, struct cell *sorted);

                                           main()
           ) list = [ 4, 7, 2, 3, 9, 1 ]                               mergeSort                                sorted       sorted
= [ 1, 2, 3, 4, 7, 9 ]
                                                    8                               9
                                                    [4, 2, 9]→ mergeSort →[2, 4, 9]
                                                                                   =
                [4, 7, 2, 3, 9, 1] → divideList →                                       → mergeList → [1, 2, 3, 4, 7, 9]
                                                    
                                                    :[7, 3, 1]→ mergeSort →[1, 3, 7]
                                                                                    ;


     *3             printList(list)
     *4             printList(list1) printList(list2)
4.3                                                                                                          31

            [4, 2, 9]    mergeSort
                                                   8                           9
                                                    [4, 9]→ mergeSort →[4, 9] 
                                                                              =
                        [4, 2, 9] → divideList →                                   → mergeList → [2, 4, 9]
                                                   
                                                   :                           
                                                                               ;
                                                       [2] → mergeSort → [2]



4.3
      4.7
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
34                                                                              5



5.2

                                       make

      5.1                       struct node                               int   1


      5.2



       struct node *createNode(int, struct node*, struct node*);

                                                                   NULL
                   malloc

      5.3                                             /

       struct node *leftNode(struct node*);
       struct node *rightNode(struct node*);

                                               NULL

      5.4

       int labelNode(struct node*);



      5.5


       void preOrder(struct node*);



      5.6


       void inOrder(struct node*);



      5.7


       void postOrder(struct node*);



      5.8
5.3                                                                                                      35

       int heightTree(struct node*);


                                                                                                         
       )                                                                                            +1


  heigthTree(node) = MAX( heigthTree(node             ), heigthTree(node                  )) + 1;

                                                                                          height
                                                  tree
                                                          4                                   4



                                          tree1                       tree3
                                                  3              8                            3



                                  node1                       node2           tree2
                                            1             2             7                     2



                                                         node4                        node3
                                                                 6              5             1

                                                                                                         

      5.9



       struct node *locateNode(int, struct node*);

                                                         NULL



      5.10

       void deleteTree(struct node*);


       )


      5.11                                                                               main()




5.3
      5.12
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                                                                              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)
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                                                                                              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
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

Weitere ähnliche Inhalte

Was ist angesagt?

C++11 smart pointer
C++11 smart pointerC++11 smart pointer
C++11 smart pointerLei Yu
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Ali Aminian
 
Functional Concepts for OOP Developers
Functional Concepts for OOP DevelopersFunctional Concepts for OOP Developers
Functional Concepts for OOP Developersbrweber2
 
Part 3-functions
Part 3-functionsPart 3-functions
Part 3-functionsankita44
 
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1Ganesh Samarthyam
 
Writing Node.js Bindings - General Principles - Gabriel Schulhof
Writing Node.js Bindings - General Principles - Gabriel SchulhofWriting Node.js Bindings - General Principles - Gabriel Schulhof
Writing Node.js Bindings - General Principles - Gabriel SchulhofWithTheBest
 

Was ist angesagt? (11)

C++11 smart pointer
C++11 smart pointerC++11 smart pointer
C++11 smart pointer
 
OpenGL ES 3 Reference Card
OpenGL ES 3 Reference CardOpenGL ES 3 Reference Card
OpenGL ES 3 Reference Card
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2
 
Functions12
Functions12Functions12
Functions12
 
Functions123
Functions123 Functions123
Functions123
 
glTF 2.0 Reference Guide
glTF 2.0 Reference GuideglTF 2.0 Reference Guide
glTF 2.0 Reference Guide
 
Functional Concepts for OOP Developers
Functional Concepts for OOP DevelopersFunctional Concepts for OOP Developers
Functional Concepts for OOP Developers
 
Part 3-functions
Part 3-functionsPart 3-functions
Part 3-functions
 
OpenGL 4.6 Reference Guide
OpenGL 4.6 Reference GuideOpenGL 4.6 Reference Guide
OpenGL 4.6 Reference Guide
 
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
 
Writing Node.js Bindings - General Principles - Gabriel Schulhof
Writing Node.js Bindings - General Principles - Gabriel SchulhofWriting Node.js Bindings - General Principles - Gabriel Schulhof
Writing Node.js Bindings - General Principles - Gabriel Schulhof
 

Ähnlich wie 平成22年度情報通信工学実験IB実施資料

Ähnlich wie 平成22年度情報通信工学実験IB実施資料 (9)

บทที่ 2
บทที่ 2บทที่ 2
บทที่ 2
 
Permute
PermutePermute
Permute
 
Permute
PermutePermute
Permute
 
Lecture5
Lecture5Lecture5
Lecture5
 
Cocos2d Performance Tips
Cocos2d Performance TipsCocos2d Performance Tips
Cocos2d Performance Tips
 
C Programming - Refresher - Part III
C Programming - Refresher - Part IIIC Programming - Refresher - Part III
C Programming - Refresher - Part III
 
8.7.2011 agml
8.7.2011 agml8.7.2011 agml
8.7.2011 agml
 
Pd Kai#2 Object Model
Pd Kai#2 Object ModelPd Kai#2 Object Model
Pd Kai#2 Object Model
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 

Mehr von Takeo Kunishima

20080702フレッシュマンセミナー
20080702フレッシュマンセミナー20080702フレッシュマンセミナー
20080702フレッシュマンセミナーTakeo Kunishima
 
20090616 フレッシュマンセミナー
20090616 フレッシュマンセミナー20090616 フレッシュマンセミナー
20090616 フレッシュマンセミナーTakeo Kunishima
 
20100615 フレッシュマンセミナー
20100615 フレッシュマンセミナー20100615 フレッシュマンセミナー
20100615 フレッシュマンセミナーTakeo Kunishima
 
20070627フレッシュマンセミナー
20070627フレッシュマンセミナー20070627フレッシュマンセミナー
20070627フレッシュマンセミナーTakeo Kunishima
 
フレッシュマンセミナー20060614
フレッシュマンセミナー20060614フレッシュマンセミナー20060614
フレッシュマンセミナー20060614Takeo Kunishima
 
フレッシュマンセミナー2005
フレッシュマンセミナー2005フレッシュマンセミナー2005
フレッシュマンセミナー2005Takeo Kunishima
 
フレッシュマンセミナー (2004)
フレッシュマンセミナー (2004)フレッシュマンセミナー (2004)
フレッシュマンセミナー (2004)Takeo Kunishima
 
Vldb2002 report-200210231500
Vldb2002 report-200210231500Vldb2002 report-200210231500
Vldb2002 report-200210231500Takeo Kunishima
 
20160723 オープンキャンパス資料
20160723 オープンキャンパス資料20160723 オープンキャンパス資料
20160723 オープンキャンパス資料Takeo Kunishima
 
20160511 全学講義講演資料
20160511 全学講義講演資料20160511 全学講義講演資料
20160511 全学講義講演資料Takeo Kunishima
 
20151224 吉備創生カレッジ第3回資料
20151224 吉備創生カレッジ第3回資料20151224 吉備創生カレッジ第3回資料
20151224 吉備創生カレッジ第3回資料Takeo Kunishima
 
20151210 吉備創生カレッジ第2回資料
20151210 吉備創生カレッジ第2回資料20151210 吉備創生カレッジ第2回資料
20151210 吉備創生カレッジ第2回資料Takeo Kunishima
 
20151126 吉備創生カレッジ第1回資料
20151126 吉備創生カレッジ第1回資料20151126 吉備創生カレッジ第1回資料
20151126 吉備創生カレッジ第1回資料Takeo Kunishima
 
20150725 オープンキャンパス資料
20150725 オープンキャンパス資料20150725 オープンキャンパス資料
20150725 オープンキャンパス資料Takeo Kunishima
 
20150603 全学講義講演資料
20150603 全学講義講演資料20150603 全学講義講演資料
20150603 全学講義講演資料Takeo Kunishima
 
岡山県立大学オープンキャンパス資料
岡山県立大学オープンキャンパス資料岡山県立大学オープンキャンパス資料
岡山県立大学オープンキャンパス資料Takeo Kunishima
 
20111102 研究室紹介(横田研)
20111102 研究室紹介(横田研)20111102 研究室紹介(横田研)
20111102 研究室紹介(横田研)Takeo Kunishima
 
20101004 研究室紹介(横田研)
20101004 研究室紹介(横田研)20101004 研究室紹介(横田研)
20101004 研究室紹介(横田研)Takeo Kunishima
 

Mehr von Takeo Kunishima (20)

20080702フレッシュマンセミナー
20080702フレッシュマンセミナー20080702フレッシュマンセミナー
20080702フレッシュマンセミナー
 
20090616 フレッシュマンセミナー
20090616 フレッシュマンセミナー20090616 フレッシュマンセミナー
20090616 フレッシュマンセミナー
 
20100615 フレッシュマンセミナー
20100615 フレッシュマンセミナー20100615 フレッシュマンセミナー
20100615 フレッシュマンセミナー
 
20070627フレッシュマンセミナー
20070627フレッシュマンセミナー20070627フレッシュマンセミナー
20070627フレッシュマンセミナー
 
フレッシュマンセミナー20060614
フレッシュマンセミナー20060614フレッシュマンセミナー20060614
フレッシュマンセミナー20060614
 
フレッシュマンセミナー2005
フレッシュマンセミナー2005フレッシュマンセミナー2005
フレッシュマンセミナー2005
 
フレッシュマンセミナー (2004)
フレッシュマンセミナー (2004)フレッシュマンセミナー (2004)
フレッシュマンセミナー (2004)
 
Vldb2002 report-200210231500
Vldb2002 report-200210231500Vldb2002 report-200210231500
Vldb2002 report-200210231500
 
20160723 オープンキャンパス資料
20160723 オープンキャンパス資料20160723 オープンキャンパス資料
20160723 オープンキャンパス資料
 
20160511 全学講義講演資料
20160511 全学講義講演資料20160511 全学講義講演資料
20160511 全学講義講演資料
 
20151224 吉備創生カレッジ第3回資料
20151224 吉備創生カレッジ第3回資料20151224 吉備創生カレッジ第3回資料
20151224 吉備創生カレッジ第3回資料
 
20151210 吉備創生カレッジ第2回資料
20151210 吉備創生カレッジ第2回資料20151210 吉備創生カレッジ第2回資料
20151210 吉備創生カレッジ第2回資料
 
20151126 吉備創生カレッジ第1回資料
20151126 吉備創生カレッジ第1回資料20151126 吉備創生カレッジ第1回資料
20151126 吉備創生カレッジ第1回資料
 
20150725 オープンキャンパス資料
20150725 オープンキャンパス資料20150725 オープンキャンパス資料
20150725 オープンキャンパス資料
 
20150603 全学講義講演資料
20150603 全学講義講演資料20150603 全学講義講演資料
20150603 全学講義講演資料
 
PCD作成手順
PCD作成手順PCD作成手順
PCD作成手順
 
PasQポスター
PasQポスターPasQポスター
PasQポスター
 
岡山県立大学オープンキャンパス資料
岡山県立大学オープンキャンパス資料岡山県立大学オープンキャンパス資料
岡山県立大学オープンキャンパス資料
 
20111102 研究室紹介(横田研)
20111102 研究室紹介(横田研)20111102 研究室紹介(横田研)
20111102 研究室紹介(横田研)
 
20101004 研究室紹介(横田研)
20101004 研究室紹介(横田研)20101004 研究室紹介(横田研)
20101004 研究室紹介(横田研)
 

Kürzlich hochgeladen

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 

Kürzlich hochgeladen (20)

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

平成22年度情報通信工学実験IB実施資料

  • 1. IB
  • 2.
  • 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
  • 9. 1.8 : 9 2. ( ) void delByName(char *); void delByPhone(char *); void delByNumber(int); delByName() delByPhone(), delByNumber() 3. ( ) void findByName(char *); void findByPhone(char *); void findByNumber(int); findByName() findByPhone(), findByNumber() 4. ( ) void printAll(); • • strcmp • name phone number 1.5 1.4 main() • main() 1.4( 1.7) *11 • 1.4 main() 1.6 1.8.1 1.7 1.4 1.6 1.8 1.7 main() *11 1
  • 10. 10 1 1.9 1.9
  • 11. 11 2 2.1 0 *1 [a1 , a2 , · · · , an ] *2 [] *3 (total order) 2 [1, 3, 2] • : 1( 1 ), 3( 2 ), 2( 3 ) • : 1( 1 ), 2( 3 ), 3( 2 ) 2.2 2.1 ML 2.2 2.3 Lisp, ML, Prolog, Java C *1 *2 I ML *3 Lisp ML “nil”
  • 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
  • 16. 16 2 a /* * Return pointer to the next cell. */ struct cell *nextCell(struct cell *target, struct cell *list) { struct cell *next = (struct cell*)NULL; if (target != (struct cell*)NULL) { next = target-next; } return next; } a 2.4 struct cell *firstCell(struct cell *); struct cell *endCell(struct cell *); NULL 2.5 previousCell *6 struct cell *previousCell(struct cell*, struct cell*); 1 2 2.6 insertCell struct cell *insertCell(int, struct cell *, struct cell *); 1 2 3 2 ( 2 ) NULL *7 malloc 2.7 deleteCell struct cell *deleteCell(struct cell*, struct cell*); *6 *7 nextCell(endCell(list), list) NULL
  • 17. 2.5 : 17 1 2 2.8 retrieveCell NULL ,0 int retrieveCell(struct cell*, struct cell*); 1 2 2.9 locateCell NULL 1 struct cell *locateCell(int, struct cell*); 1 2 /* * return position of the cell has data equals to specified as iData */ struct cell *locateCell(int data, struct cell *list) { struct cell *current; /* current cell’s pointer */ /* set pointer to current cell as pointer to first cell */ current = firstCell(list); /* while current cell’s data is not data, pursue the next cell */ while ((current != (struct cell*)NULL) (retrieveCell(current, list) != data)) { current = nextCell(current, list); } return current; /* return end cell’s pointer */ } 2.10 deleteCell deleteList void deleteList(struct cell*); 1 2.11 printList [ 1, 2, 3, 4, 5 ] void printList(struct cell*); 1 2.12 main()
  • 18. 18 2 2.6 2.13
  • 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
  • 23. 3.4 : ( ) 23 int popStack(struct cell*); 3.5 void pushStack(int, struct cell*); 3.6 void deleteStack(struct cell*); 3.7 main() 3.4 : ( ) 3.4.1 • • • • int • MAKENULL FRONT DEQUEUE ENQUEUE EMPTY 6 queue.h queue.c queue-test.c make 3.8 struct cell *makeNullQueue(void); 3.9 1 0 emptyQueue int emptyQueue(struct cell*);
  • 24. 24 3 3.10 frontQueue int frontQueue(struct cell*); 3.11 dequeue int dequeue(struct cell*); 3.12 enqueue void enqueue(int, struct cell*); 3.13 void deleleteQueue(struct cell*); 3.14 main() 3.5 : 3.15 struct array_stack *makeNullStack(void); int emptyStack(struct array_stack*); int topStack(struct array_stack*); int popStack(struct array_stack*); void pushStack(int, struct array_stack*); void deleteStack(struct array_stack*); struct array_stack #define MAX_STACK_SIZE 10 struct array_stack { int length; /* ( ) */ int elements[MAX_STACK_SIZE]; /* */ }; int 0 MAX_STACK_SIZE 3.16
  • 25. 3.6 : 25 struct array_queue *makeNullQueue(void); int emptyQueue(struct array_queue*); int frontQueue(struct array_queue*); int dequeue(struct array_queue*); void enqueue(int, struct array_queue*); void deleteQueue(struct array_queue*); struct array_queue #define MAX_QUEUE_SIZE 10 struct array_queue { int front; /* */ int length; /* ( ) */ int elements[MAX_QUEUE_SIZE]; /* */ }; int MAX_QUEUE_SIZE-1 0 MAX_QUEUE_SIZE MAX_QUEUE_SIZE 3.6 : 2 2 32 + 9, 4 / 2 infix notation 2 prefix notation 2 postfix notation E 1. E E E 2. 2 op E1 op E2 E1 E2 op E1 , E2 E1 , E2 3. (E) E E E 3.17 32 + 3, 9 + 4 ∗ 3, (1 + 2) ∗ 4 1 1 1. push 2. op 2 x, y pop x op y 3.18 3.17
  • 26. 26 3 3.19 • 1 +, −, ∗, / 1 1 1 1 • • / a.out % cat testdata.txt 16 3 + 3 / -2 - % ./a.out testdata.txt 8 3.1 3.6 3.7 3.20
  • 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)
  • 30. 30 4 4.4 list list1 list2 void divideList(struct cell *list, struct cell *list1, struct cell *list2); list list1 list2 *3 main() ) pcList = [1, 2, 3, 4, 5]   list1 =  [1]@([3, 4, 5] divideList list1)  = [1]@([3]@([5] divideList list1))  list2 =  [2]@([3, 4, 5] divideList list2)  = [2]@([4]@([5] divideList list2)) 4.5 2 void mergeList(struct cell *list1, struct cell *list2, struct cell *merged); list1 list2 merged list1 list2 *4 main() ) list1 = [3, 6, 2] list2 = [4, 5, 7] merged = [3]@([6, 2] [4, 5, 7] mergeList merged) = [3]@([4]@([6, 2] [5, 7] mergeList merged)) = ··· 4.6 divideList mergeList mergeSort 1. divideList 2. mergeSort 3. 2 mergeList list sorted void mergeSort(struct cell *list, struct cell *sorted); main() ) list = [ 4, 7, 2, 3, 9, 1 ] mergeSort sorted sorted = [ 1, 2, 3, 4, 7, 9 ] 8 9 [4, 2, 9]→ mergeSort →[2, 4, 9] = [4, 7, 2, 3, 9, 1] → divideList → → mergeList → [1, 2, 3, 4, 7, 9] :[7, 3, 1]→ mergeSort →[1, 3, 7] ; *3 printList(list) *4 printList(list1) printList(list2)
  • 31. 4.3 31 [4, 2, 9] mergeSort 8 9 [4, 9]→ mergeSort →[4, 9] = [4, 2, 9] → divideList → → mergeList → [2, 4, 9] : ; [2] → mergeSort → [2] 4.3 4.7
  • 32.
  • 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
  • 34. 34 5 5.2 make 5.1 struct node int 1 5.2 struct node *createNode(int, struct node*, struct node*); NULL malloc 5.3 / struct node *leftNode(struct node*); struct node *rightNode(struct node*); NULL 5.4 int labelNode(struct node*); 5.5 void preOrder(struct node*); 5.6 void inOrder(struct node*); 5.7 void postOrder(struct node*); 5.8
  • 35. 5.3 35 int heightTree(struct node*); ) +1 heigthTree(node) = MAX( heigthTree(node ), heigthTree(node )) + 1; height tree 4 4 tree1 tree3 3 8 3 node1 node2 tree2 1 2 7 2 node4 node3 6 5 1 5.9 struct node *locateNode(int, struct node*); NULL 5.10 void deleteTree(struct node*); ) 5.11 main() 5.3 5.12
  • 36.
  • 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