2011年6月17日 星期五

EX11. 鏈結串列DELETENODE

修改程式範例: Ch4-3-2.c 為 Ch4-3-2e.c
1.修改 Ch4-3-2.c 中include的 “deleteNode.c”程式
2.將”情況2: 刪除最後一個節點”及”情況3: 刪除中間節點 “的程式碼判
斷改為同時指向 ptr->next.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* Ch4-3.h */
struct Node           /* Node節點結構 */
 {         
   int data;          /* 結構變數宣告 */ 
   struct Node *next; /* 指向下一個節點 */
 };
typedef struct Node LNode;   /* 串列節點的新型態 */
typedef LNode *List;         /* 串列的新型態 */
List first = NULL;           /* 串列的開頭指標 */
/* 抽象資料型態的操作函數宣告 */
extern void creatList(int len, int *array);
extern int isListEmpty();
extern void printList();
extern List searchNode(int d);
extern int deleteNode(List ptr);
extern void insertNode(List ptr, int d);




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* createList.c */
/* 函數: 建立串列 */
void createList(int len, int *array) 
  {
   int i;
   List newnode;
   for ( i = 0; i < len; i++ )
     {
      /* 配置節點記憶體 */
      newnode = (List) malloc(sizeof(LNode));
      newnode->data = array[i]; /* 建立節點內容 */ 
      newnode->next = first;
      first = newnode;
     }
  }
/* 函數: 檢查串列是否是空的 */
int isListEmpty() 
  {
   if ( first == NULL ) return 1;
   else                 return 0;
  }
/* 函數: 顯示串列資料 */
void printList() 
 {
   List current = first;  /* 目前的串列指標 */
   while ( current != NULL ) /* 顯示主迴圈 */
   { 
      printf("[%d]", current->data);
      current = current->next;  /* 下一個節點 */
   }
   printf("\n");
 }
/* 函數: 搜尋節點資料 */
List searchNode(int d) 
  {
   List current = first;   /* 目前的串列指標 */
   while ( current != NULL ) /* 搜尋主迴圈 */
   { 
      if ( current->data == d ) /* 是否找到資料 */
         return current; /* 找到 */
      current = current->next;  /* 下一個節點 */
   }
   return NULL;          /* 沒有找到 */
  }




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* deleteNode.c */
/* 函數: 刪除節點 */
int deleteNode(List ptr)
  {
   List current = first;   /* 指向前一節點 */
   int value = ptr->data;  /* 取得刪除的節點值 */
   if ( isListEmpty() )    /* 檢查串列是否是空的 */
      return -1;
   if (ptr==first || ptr==NULL) {/* 串列開始或NULL */
      /* 情況1: 刪除第一個節點 */
      first = first->next;       /* 刪除第1個節點 */
   } else {     
      while (current->next!=ptr) /* 找節點ptr的前節點 */
         current = current->next;      
         current->next = ptr->next; /* 刪除中間或最後一個節點 */
   }
   free(ptr);                     /* 釋放節點記憶體 */
   return value;                  /* 傳回刪除的節點值 */
  }






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* Ch4-3-2e.c */
#include <stdio.h>
#include <stdlib.h>
#include "Ch4-3.h"
#include "createList.c"
#include "deleteNode.c"
/* 主程式 */
int main() 
 {
   int temp;  /* 宣告變數 */
   int data[6]={ 1, 2, 3, 4, 5, 6 };/* 建立串列的陣列 */
   List ptr;
   createList(6, data);   /* 建立串列 */
   printf("原來的串列: ");
   printList();  /* 顯示串列 */ 
   /* 4-3-2: 節點刪除 */
   temp = 0; 
   while ( temp != -1 ) 
   {
      printf("請輸入刪除的郵寄編號(-1結束) ==> ");
      scanf("%d", &temp);     /* 讀取郵寄編號 */
         if ( temp != -1 )    /* 搜尋節點資料 */
      { 
           ptr = searchNode(temp);  /* 找尋節點 */
           if ( ptr != NULL ) 
         {
            temp = deleteNode(ptr); /* 刪除節點 */
            printf("刪除節點: %d\n", temp); 
            printf("刪除後串列: ");
            printList();        /* 顯示刪除後串列 */
         }         
      }
   }
   system("PAUSE");
   return 0; 
 }

EX10. 指標與字串

修改程式範例: Ch3-4-1.c 為 Ch3-4-1e.c
1.將字串複製: 於while程式段中的ptr1修改為如ptr的遞增運算方式
2.如:






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* Ch3-4-1e.c */
#include <stdio.h>
#include <stdlib.h>
#define LEN 16
/* 主程式 */
int main() 
{
   /* 字元陣列宣告 */
   char str[LEN] = "This is a book.";
   char str1[LEN];
   /* 字元指標 */
   char *ptr  = str;
   char *ptr1;
   
   /* 顯示字串內容 */
   ptr1 = "This is a pen.";
   printf("str = %s\n", str);
   printf("ptr = %s\n", ptr);
   printf("ptr1 = %s\n", ptr1);
   
   /* 字串複製的迴圈 */
   printf("將字串str複製到str1: \n");  
   ptr1 = str1;
   while ( *ptr != '\0' ) 
   {
      *ptr1++ = *ptr++; 
   }
   *ptr1++ = '\0';
   printf("str1 = %s\n", str1);
   printf("ptr1 = %s\n", ptr1);
   system("PAUSE");
   return 0; 
}
 
 

執行結果:
str = This is a book.
ptr = This is a book.
ptr1 = This is a pen.
將字串str複製到str1:
str1 = This is a book.
ptr1 = This is a book.
請按任意鍵繼續 . . .

EX09. 指標與結構

 
修改程式範例: Ch3-3.c 為 Ch3-3e.c
1.增加電話結構的宣告
struct phone /* phone結構 */
{
char phone1[15];
char phone2[15];
}
2.將 struct label增加phone結構成員:
struct phone callno;
3.使用結構變數和指標來存取



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* Ch3-3e.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct phone             /* 結構phone的宣告 */ 
  {
    char phone1[15];
    char phone2[15];  
  }; 

struct label             /* 結構label的宣告 */ 
  {
    char name[20];
    int age;
    struct phone callno;
  };

/* 函數: 顯示結構指標的成員變數 */ 
void showlabel(struct label *ptr)
{
   printf("員工名牌----------\n"); 
   printf("姓名: %s\n", ptr->name);
   printf("年齡: %d\n", ptr->age);
   printf("電話: %s\n", ptr->callno.phone1);
   printf("手機: %s\n", ptr->callno.phone2);
   printf("------------------\n");
}

/* 主程式 */
int main()
{
   /* 宣告變數 */
   struct label worker;
   struct label *ptr;

   /* 將結構指標指向結構 */
   ptr = &worker;

   /* 指定結構的成員變數 */             
   strcpy(worker.name, "陳會安"); 
   ptr->age = 30;
   strcpy(worker.callno.phone1, "04-23376297");
   strcpy(worker.callno.phone2, "0926-152153");

   /* 顯示結構的成員變數 */
   printf("姓名: %s\n", worker.name);
   printf("年齡: %d\n", worker.age);
   printf("電話: %s\n", worker.callno.phone1);
   printf("手機: %s\n", worker.callno.phone2);
   
   /* 呼叫函數 */
   showlabel(ptr); 
   
   system("PAUSE");
   return 0; 
}


執行結果:
姓名: 陳會安
年齡: 30
電話: 04-23376297
手機: 0926-152153
員工名牌----------
姓名: 陳會安
年齡: 30
電話: 04-23376297
手機: 0926-152153
------------------
請按任意鍵繼續 . . .

2011年4月3日 星期日

EX08指標與二維陣列

修改程式範例: Ch3-2-2.c 為 Ch3-2-2e.c
1.將程式改為完整的九九乘法表
2.增加顯示 i, j, i+1, j+1 及 i*COLS+j 的值如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* 程式範例: Ch3-2-2.c */
#include <stdio.h>
#include <stdlib.h>
#define ROWS      9
#define COLS      9
/* 主程式 */
int main()
{
   /* 宣告變數 */
   int i, j;
   /* 建立int的二維陣列 */
   int tables[ROWS][COLS];
   int *ptr;
   /* 指定二維陣列的元素值 */ 
   for ( i=0; i < ROWS; i++)
      for ( j=0; j < COLS; j++)
         tables[i][j] = (i+1)*(j+1);
   /* 顯示二維陣列的元素值 */ 
   ptr = &tables[0][0];
   
   for ( i=0; i < ROWS; i++) 
   {
      for ( j=0; j < COLS; j++)
       printf("%d*%d=%2d ", (i+1), (j+1),*(ptr+(i*COLS)+j));
       printf("\n");
   } 
   printf("\n");
   
   printf("-----------------------------------------------------\n");  
   printf("i\tj\ti+1\tj+1\ti*COLS+j\n");
   ptr = &tables[0][0];
   for ( i=0; i < ROWS; i++) 
   {
     for ( j=0; j < COLS; j++)
       printf("%d\t%d\t%d\t%d\t%d\n",i,j,(i+1),(j+1),((i*COLS)+j));
       printf("\n");
   }
   system("PAUSE");
   return 0; 
}



2011年4月2日 星期六

EX07結構練習

修改程式範例: Ch2-4.c 為Ch2-4e.c
1.假設有一個6 X 9的稀疏矩陣,如下圖所示:
2.請使用結構陣列的壓縮表示法建立稀疏矩陣的內容。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* 程式範例: Ch2-4e.c */
#include <stdio.h>
#include <stdlib.h>

#define MAX_TERMS    10           /* 稀疏矩陣的最大元素數 */
struct Term 
{                                 /* 稀疏矩陣的元素結構 */ 
   int row;                       /* 元素的列數 */ 
   int col;                       /* 元素的欄數 */ 
   int value;                     /* 元素的值 */ 
};

struct sMatrix 
{                                 /* 稀疏矩陣的結構 */
   int rows;                      /* 矩陣的列數 */ 
   int cols;                      /* 矩陣的欄數 */ 
   int numOfTerms;                /* 矩陣的元素數 */ 
   struct Term smArr[MAX_TERMS];  /* 壓縮陣列的宣告 */
};

typedef struct sMatrix Matrix;    /* 建立稀疏矩陣的新型態 */ 
Matrix m;                         /* 建立稀疏矩陣 */
 
/* 抽象資料型態的操作函數宣告 */
extern void createMatrix(int r,int c,int *arr);
extern void printMatrix();


/* 函數: 建立稀疏矩陣 */
void createMatrix(int r,int c,int *arr)
{
   int i, j, count;
   m.rows = r;                           /* 初始結構的成員變數 */
   m.cols = c; 
   count = 0;
   for ( i = 0; i < r; i++ )             /* 二維陣列的走訪 */
      for ( j = 0; j < c; j++ )
         if ( arr[i*c+j] != 0 ) {        /* 元素有值 */
            m.smArr[count].row = i;      /* 列數 */
            m.smArr[count].col = j;      /* 欄數 */
            /* 元素值 */
            m.smArr[count].value = arr[i*c+j];
            count++;
         }
   m.numOfTerms = count;
}


/* 函數: 顯示稀疏矩陣 */
void printMatrix() 
{
   int i;
   /* 顯示稀疏矩陣尺寸和項目數 */
   printf("尺寸: %d X %d", m.rows, m.cols);
   printf(" 項目數: %d\n", m.numOfTerms);
   printf("列row\t欄col\t值value\n");
   /* 顯示稀疏矩陣的各項目座標與值 */
   for ( i = 0; i < m.numOfTerms; i++) 
   {
     printf(" %d\t%d",m.smArr[i].row,m.smArr[i].col);
     printf("\t%d\n", m.smArr[i].value);
   }  
}


/* 主程式 */ 
int main() 
{   
   /* 稀疏矩陣 */
   int sparse[6][9] = {  0, 7, 0, 0, 0, 0, 0, 0, 0,
                         0, 0, 0, 5, 0, 0, 0, 0, 3, 
                         0, 0, 0, 0, 0, 1, 0, 0, 0,
                         2, 0, 0, 0, 4, 0, 0, 0, 0,
                         0, 0, 8, 0, 0, 0, 0, 0, 0, 
                         0, 0, 0, 0, 0, 0, 0, 9, 0, };
   int *fp = &sparse[0][0];        /* 取得陣列的指標 */ 
   /* 建立稀疏矩陣物件 */
   createMatrix(6, 9, fp); 
   printMatrix();                  /* 顯示稀疏矩陣 */
   system("PAUSE");
   return 0;  
}







2011年3月26日 星期六

EX05結構練習

修改程式範例: Ch2-3-3.c  Ch2-3-3e.c
1.struct test 增加1項成員:
n會計分數 int accounting  
2.使用新型態變數宣告, 增加宣告學生結構變數 john, 且指定數值


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* 程式範例: Ch2-3-3e.c */
#include <stdio.h>
#include <stdlib.h>

/* 主程式 */ 
int main() {
struct test
{                              /* 宣告結構 */
   int math;
   int english;
   int computer;
   int accounting;  
};

   typedef struct test score;  /* 定義新型態 */
   score joe, jane, john;      /* 使用新型態變數宣告 */
   
   joe.math = 80;              /* 指定成員變數 */ 
   joe.english = 85;
   joe.computer = 83;
   joe.accounting= 83;
      
   jane.math = 78;             /* 指定成員變數 */ 
   jane.english = 65;
   jane.computer = 55;
   jane.accounting = 55;
   
   john.math = 88;             /* 指定成員變數 */ 
   john.english = 98;
   john.computer = 85;
   john.accounting = 95;
   
   /* 顯示成績 */
   printf("姓名: Joe\n");
   printf("數學: %d\n", joe.math);
   printf("英文: %d\n", joe.english);
   printf("數學: %d\n", joe.computer);
   printf("會計: %d\n", joe.accounting);
   printf("=================\n");
   
   printf("姓名: Jane\n");
   printf("數學: %d\n", jane.math);
   printf("英文: %d\n", jane.english);
   printf("數學: %d\n", jane.computer);
   printf("會計: %d\n", jane.accounting);
   printf("=================\n");
   
   printf("姓名: John\n");
   printf("數學: %d\n", john.math);
   printf("英文: %d\n", john.english);
   printf("數學: %d\n", john.computer);
   printf("會計: %d\n", john.accounting);
      
   system("PAUSE");
   return 0;  
}
執行結果:
姓名: Joe
數學: 80
英文: 85
數學: 83
會計: 83
=================
姓名: Jane
數學: 78
英文: 65
數學: 55
會計: 55
=================
姓名: John
數學: 88
英文: 98
數學: 85
會計: 95
請按任意鍵繼續 . . .

EX04結構練習

修改程式範例: Ch2-3-1.c Ch2-3-1e.c
1.struct student 增加2項成員:
n班級         char Class[10]  
n會計分數  int accounting  
2.增加宣告學生結構變數 std4,且用指定值的方式
3.total增列會計分數



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* 程式範例: Ch2-3-1e.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student 
{                               /* 學生資料 */
    char Class[10];
    int id;  
    char name[20];    
    int math;
    int english;
    int computer;
    int accounting;
};
/* 主程式 */ 
int main() 
{
   struct student std1;         /* 宣告結構變數 */
   struct student std2 = {"資一1" ,2, "江小魚", 45, 78, 66, 80};
   struct student std3;
   struct student std4;
   int total; 
   
   strcpy(std1.Class,"資一1"); 
   std1.id = 1;                 /* 指定結構變數的值 */
   strcpy(std1.name, "陳會安");
   std1.math = 78;
   std1.english = 65;
   std1.computer = 90;
   std1.accounting = 60;
   std3 = std2;                 /* 指定敘述 */ 
   
   strcpy(std4.Class,"資一1"); 
   std4.id = 4;                 /* 指定結構變數的值 */
   strcpy(std4.name, "林琦閔");
   std4.math = 90;
   std4.english = 90;
   std4.computer = 95;
   std4.accounting = 95;
    
   /* 顯示學生資料 */
   printf("班級: %s\n", std1.Class);
   printf("學號: %d\n", std1.id);
   printf("姓名: %s\n", std1.name);
   total = std1.math + std1.english + std1.computer + std1.accounting;
   printf("成績總分: %d\n", total);
   printf("=================\n"); 
   
   printf("班級: %s\n", std2.Class);
   printf("學號: %d\n", std2.id);
   printf("姓名: %s\n", std2.name);
   total = std2.math + std2.english + std2.computer + std2.accounting;
   printf("成績總分: %d\n", total);
   printf("=================\n"); 
   
   printf("班級: %s\n", std3.Class);
   printf("學號: %d\n", std3.id);
   printf("姓名: %s\n", std3.name);
   total = std3.math + std3.english + std3.computer + std3.accounting;
   printf("成績總分: %d\n", total);
   printf("=================\n");  
   
   printf("班級: %s\n", std4.Class); 
   printf("學號: %d\n", std4.id);
   printf("姓名: %s\n", std4.name);
   total = std4.math + std4.english + std4.computer + std4.accounting;
   printf("成績總分: %d\n", total);
      
   system("PAUSE");
   return 0; 
}


執行結果:
班級: 資一1
學號: 1
姓名: 陳會安
成績總分: 293
=================
班級: 資一1
學號: 2
姓名: 江小魚
成績總分: 269
=================
班級: 資一1
學號: 2
姓名: 江小魚
成績總分: 269
=================
班級: 資一1
學號: 4
姓名: 林琦閔
成績總分: 370
請按任意鍵繼續 . . .