「C語言」「例題」指針與鏈表,c語言指針例題
本篇收集《C語言程序設計教程》第十章“指針與鏈表”的所有例題。

![]()
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 //使用malloc函數動態分配空間
5
6 int main()
7 {
8 int *iIntMalloc=(int *)malloc(sizeof(int));//分配空間
9 *iIntMalloc=100;//使用該空間保存數據
10 printf("%d\n",*iIntMalloc);
11 return 0;
12 }
10.1 使用malloc函數動態分配空間

![]()
1 #include <stdio.h>
2 #include <stdlib.h>
3 #define LEN sizeof(char[26])/* 定義的值為26個字符的字符數組所占的字節長度 */
4
5 //使用calloc分配數組內存
6
7 int main()
8 {
9 int i;/* 循環變量i */
10 /* 使用calloc動態分配一個長度為26字符的字符數組 */
11 char *ch1=(char *)calloc(26,sizeof(char));
12 /* 使用malloc動態分配一個長度為26字符的字符數組 */
13 char *ch2=(char *)malloc(LEN);
14 for(i=0;i<26;i++)/* 為兩個字符數組復賦值 */
15 {
16 ch1[i]=65+i;/* ch1是大寫字符數組 */
17 ch2[i]=97+i;/* ch2是小寫字符數組 */
18 }
19 printf("26個大寫字母:\n");
20 for(i=0;i<26;i++)/* 打印大寫字母 */
21 {
22 printf("%c ",ch1[i]);
23 if(i==12 || i==25)/* 格式控制,輸出換行 */
24 printf("\n");
25 }
26 printf("26個小寫字母");
27 for(i=0;i<26;i++)/* 打印小寫字母 */
28 {
29 printf("%c ",ch2[i]);
30 if(i==12 || i==25)/* 格式控制,輸出換行 */
31 printf("\n");
32 }
33 return 0;
34 }
10.2 使用calloc分配數組內存

![]()
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 //使用realloc函數重新分配內存
5
6 int main()
7 {
8 short *s;/* 定義短整型指針變量s */
9 double *f=(double *)malloc(sizeof(double));/* 申請double變量所占內存空間 */
10 printf("指針f指向內存空間的起始地址:%d\n",f);/* 打印首地址 */
11 printf("指針f指向內存空間的大小,%d字節\n",sizeof(*f));/* 打印空間大小 */
12 s=(short *)realloc(f,sizeof(short));/* 重新分配內存 */
13 printf("指針s指向內存空間的起始地址:%d\n",s);/* 打印首地址 */
14 printf("指針s指向內存空間的大小,%d字節\n",sizeof(*s));/* 打印空間大小 */
15 return 0;
16 }
10.3 使用realloc函數重新分配內存

![]()
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 //使用free函數釋放內存空間
5
6 int main()
7 {
8 int *pInt;
9 pInt=(int *)malloc(sizeof(pInt));
10 *pInt=100;
11 printf("%d\n",*pInt);
12 free(pInt);
13 printf("%d\n",*pInt);
14 return 0;
15 }
10.4 使用free函數釋放內存空間

![]()
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 //創建鏈表並將其輸出
5
6 struct Student
7 {
8 char cName[20];/* 姓名 */
9 int iNumber;/* 學號 */
10 struct Student *next;/* next的類型是指向本結構體類型的指針類型 */
11 };
12
13 int iCount;/* 全局變量表示鏈表長度 */
14
15 struct Student *Create()
16 {
17 struct Student *pHead=NULL;/* 初始化鏈表,頭指針為空 */
18 struct Student *pEnd,*pNew;
19 iCount=0;/* 初始化鏈表長度 */
20 pEnd=pNew=(struct Student *)malloc(sizeof(struct Student));
21 printf("請輸入學生的姓名和學號:\n");
22 scanf("%s",pNew->cName);
23 scanf("%d",&pNew->iNumber);
24 while(pNew->iNumber!=0)
25 {
26 iCount++;
27 if(iCount==1)
28 {
29 pNew->next=pHead;/* 使得指針指向為空 */
30 pEnd->next=pNew;/* 跟蹤新加入的節點 */
31 pHead=pNew;/* 頭指針指向首節點 */
32 }
33 else
34 {
35 pNew->next=NULL;/* 新節點的指針為空 */
36 pEnd->next=pNew;/* 原來的節點指向新節點 */
37 pEnd=pNew;/* pEnd指向新節點 */
38 }
39 pNew=(struct Student *)malloc(sizeof(struct Student));/* 再次分配節點的內存空間 */
40 scanf("%s",pNew->cName);
41 scanf("%d",&pNew->iNumber);
42 }
43 free(pNew);/* 釋放節點空間 */
44 return pHead;
45 }
46
47 void print(struct Student *pHead)
48 {
49 struct Student *pTemp;/* 循環所用的臨時指針 */
50 int iIndex=1;/* 表示鏈表中節點的序號 */
51 printf("**********本名單中有%d個學生**********\n",iCount);
52 pTemp=pHead;/* 指針得到首節點的地址 */
53 while(pTemp!=NULL)
54 {
55 printf("第%d個學生是:\n",iIndex);
56 printf("姓名:%s\n",pTemp->cName);/* 輸出姓名 */
57 printf("學號:%d\n\n",pTemp->iNumber);/* 輸出學號 */
58 pTemp=pTemp->next;/* 移動臨時指針到下一個節點 */
59 iIndex++;/* 進行自加運算 */
60 }
61 }
62
63 int main()
64 {
65 struct Student *pHead;/* 定義頭結點 */
66 pHead=Create();/* 創建節點 */
67 print(pHead);/* 輸出鏈表 */
68 return 0;
69 }
10.5 創建鏈表並將其輸出

![]()
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 //鏈表的插入操作
5
6 struct Student{
7 char cName[20];/* 姓名 */
8 int iNumber;/* 學號 */
9 struct Student *next;/* next的類型是指向本結構體類型的指針類型 */
10 };
11
12 int iCount;/* 全局變量表示鏈表長度 */
13
14 struct Student *Create()
15 {
16 struct Student *pHead=NULL;/* 初始化鏈表,頭指針為空 */
17 struct Student *pNew,*pEnd;
18 iCount=0;/* 初始化鏈表長度 */
19 pEnd=pNew=(struct Student *)malloc(sizeof(struct Student));
20 printf("請輸入學生的姓名和學號:\n");
21 scanf("%s",pNew->cName);
22 scanf("%d",&pNew->iNumber);
23 while(pNew->iNumber!=0)
24 {
25 iCount++;
26 if(iCount==1)
27 {
28 pNew->next=pHead;/* 使得指針指向為空 */
29 pEnd=pNew;/* 跟蹤新加入的節點 */
30 pHead=pNew;/* 頭指針指向首節點 */
31 }
32 else
33 {
34 pNew->next=NULL;/* 新節點的指針為空 */
35 pEnd->next=pNew;/* 原來的節點指向新節點 */
36 pEnd=pNew;/* pEnd指向新節點 */
37 }
38 pNew=(struct Student *)malloc(sizeof(struct Student));/* 再次分配節點的內存空間 */
39 scanf("%s",pNew->cName);
40 scanf("%d",&pNew->iNumber);
41 }
42 free(pNew);/* 釋放節點空間 */
43 return(pHead);
44 }
45
46 void Print(struct Student *pHead)
47 {
48 struct Student *pTemp;/* 循環所用的臨時指針 */
49 int iIndex=1;/* 表示鏈表中節點的序號 */
50 printf("\n****本名單中有%d名學生****\n",iCount);
51 pTemp=pHead;/* 指針得到首節點的地址 */
52 while(pTemp!=NULL)
53 {
54 printf("第%d名學生是:\n",iIndex);
55 printf("姓名:%s\n",pTemp->cName);/* 輸出姓名 */
56 printf("學號:%d\n\n",pTemp->iNumber);/* 輸出學號 */
57 pTemp=pTemp->next;/* 移動臨時指針到下一個節點 */
58 iIndex++;/* 進行自加運算 */
59 }
60 }
61
62 struct Student *Insert(struct Student *pHead)
63 {
64 struct Student *pNew;/* 定義pNew指向新分配的空間 */
65 printf("請輸入學生的姓名和學號:\n");
66 pNew=(struct Student *)malloc(sizeof(struct Student));/* 分配內存空間,返回該內存空間地址 */
67 scanf("%s",pNew->cName);
68 scanf("%d",&pNew->iNumber);
69 pNew->next=pHead;/* 新節點指針指向原來的首節點 */
70 pHead=pNew;/* 頭指針指向新節點 */
71 iCount++;/* 增加鏈表節點數量 */
72 return pHead;/* 返回頭指針 */
73 }
74
75 int main()
76 {
77 struct Student *pHead;/* 定義頭結點 */
78 pHead=Create();/* 創建節點 */
79 pHead=Insert(pHead);/* 插入節點 */
80 Print(pHead);/* 輸出鏈表 */
81 return 0;
82 }
10.6 鏈表的插入操作

![]()
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 //鏈表的刪除操作
5
6 struct Student{
7 char cName[20];/* 姓名 */
8 int iNumber;/* 學號 */
9 struct Student *next;/* next的類型是指向本結構體類型的指針類型 */
10 };
11
12 int iCount;/* 全局變量表示鏈表長度 */
13
14 struct Student *Create()
15 {
16 struct Student *pHead=NULL;/* 初始化鏈表,頭指針為空 */
17 struct Student *pNew,*pEnd;
18 iCount=0;/* 初始化鏈表長度 */
19 pEnd=pNew=(struct Student *)malloc(sizeof(struct Student));
20 printf("請輸入學生的姓名和學號:\n");
21 scanf("%s",pNew->cName);
22 scanf("%d",&pNew->iNumber);
23 while(pNew->iNumber!=0)
24 {
25 iCount++;
26 if(iCount==1)
27 {
28 pNew->next=pHead;/* 使得指針指向為空 */
29 pEnd=pNew;/* 跟蹤新加入的節點 */
30 pHead=pNew;/* 頭指針指向首節點 */
31 }
32 else
33 {
34 pNew->next=NULL;/* 新節點的指針為空 */
35 pEnd->next=pNew;/* 原來的節點指向新節點 */
36 pEnd=pNew;/* pEnd指向新節點 */
37 }
38 pNew=(struct Student *)malloc(sizeof(struct Student));/* 再次分配節點的內存空間 */
39 scanf("%s",pNew->cName);
40 scanf("%d",&pNew->iNumber);
41 }
42 free(pNew);/* 釋放節點空間 */
43 return(pHead);
44 }
45
46 void Print(struct Student *pHead)
47 {
48 struct Student *pTemp;/* 循環所用的臨時指針 */
49 int iIndex=1;/* 表示鏈表中節點的序號 */
50 printf("****本名單中有%d名學生****\n",iCount);
51 pTemp=pHead;/* 指針得到首節點的地址 */
52 while(pTemp!=NULL)
53 {
54 printf("第%d名學生是:\n",iIndex);
55 printf("姓名:%s\n",pTemp->cName);/* 輸出姓名 */
56 printf("學號:%d\n\n",pTemp->iNumber);/* 輸出學號 */
57 pTemp=pTemp->next;/* 移動臨時指針到下一個節點 */
58 iIndex++;/* 進行自加運算 */
59 }
60 }
61
62 struct Student *Insert(struct Student *pHead)
63 {
64 struct Student *pNew;/* 定義pNew指向新分配的空間 */
65 printf("請輸入學生的姓名和學號:\n");
66 pNew=(struct Student *)malloc(sizeof(struct Student));/* 分配內存空間,返回該內存空間地址 */
67 scanf("%s",pNew->cName);
68 scanf("%d",&pNew->iNumber);
69 pNew->next=pHead;/* 新節點指針指向原來的首節點 */
70 pHead=pNew;/* 頭指針指向新節點 */
71 iCount++;/* 增加鏈表節點數量 */
72 return pHead;/* 返回頭指針 */
73 }
74
75 void Delete(struct Student *pHead,int iIndex)
76 /* pHead為頭結點,iIndex表示要刪除的節點序號 */
77 {
78 int i;
79 struct Student *pTemp;/* 臨時指針 */
80 struct Student *pPre;/* 表示要刪除節點前的節點 */
81 pTemp=pHead;/* 得到鏈表的頭結點 */
82 pPre=pTemp;
83 printf("--------刪除第%d個學生--------",iIndex);
84 for(i=1;i<iIndex;i++)
85 {/* 通過for循環使得pTemp指向要刪除的節點 */
86 pPre=pTemp;
87 pTemp=pTemp->next;
88 }
89 pPre->next=pTemp->next;/* 連接刪除兩邊的節點 */
90 free(pTemp);/* 釋放要刪除節點的內存空間 */
91 iCount--;/* 減少鏈表中的結點個數 */
92 }
93
94 int main()
95 {
96 struct Student *pHead;/* 定義頭結點 */
97 pHead=Create();/* 創建節點 */
98 pHead=Insert(pHead);/* 插入節點 */
99 Delete(pHead,2);/* 刪除第二個節點 */
100 Print(pHead);/* 輸出鏈表 */
101 return 0;
102 }
10.7 鏈表的刪除操作

![]()
1 #include <stdio.h>
2 #include <malloc.h>
3
4 /*建立兩個帶頭節點的學生鏈表,每個節點包含學號、姓名和成績,
5 鏈表都按學號升序排列,將它們合並為一個鏈表仍按學號升序排列 */
6
7 //void merge(struct stud *La,struct stud *Lb)
8 //{
9 // struct stud *a,*b,*c;
10 // c=La;
11 // a=La->next;/* 合並前 */
12 // b=Lb->next;
13 // while(a!=NULL && b!=NULL)/* La和Lb都沒處理完 */
14 // {
15 // if(a->num <= b->num)
16 // {
17 // c->next=a;
18 // c=a;
19 // a=a->next;
20 // }
21 // else
22 // {
23 // c->next=b;
24 // c=b;
25 // b=b->next;
26 // }
27 // }
28 // if(a!=NULL)
29 // c->next=a;/* 若La沒有處理完 */
30 // else
31 // c->next=b;/* 若Lb沒有處理完 */
32 // free(Lb); /* 釋放Lb的頭結點 */
33 //}
34
35 int main()
36 {
37 printf("程序功能代碼如 merge() 函數。\n");
38 return 0;
39 }
10.8 兩個鏈表的合並