1. 程式人生 > >c語言課上作業——初學連結串列操作

c語言課上作業——初學連結串列操作

http://www.cnblogs.com/maluning/p/7966875.html

根據上面的大佬的指導。今天完成了老師的作業。第一次完成了連結串列的建立、遍歷、插入、刪除(基本靠抄,不好意思)。

作為一個菜鳥,第一次完成100行以上的不用複製重複程式碼的程式碼。好興奮呀。

//不過還是有一點不明白。不明白以下程式碼中的頭指標或者說頭節點是不是要在主調函式中最後一次遍歷後也把它free掉。

  1 //  操作環境 Win 10
  2 //  IDE      Visual Studio 2017
  3 //  !!移植請將scanf_s換為scanf    !! 
  4 
  5 #include "
pch.h" 6 #include <stdio.h> 7 #include <stdlib.h> 8 9 //定義結構體 10 11 typedef struct node { 12 13 int data; //用於存放資料 14 node *next; //指向下一個節點 15 }Node, *PNode; 16 17 // 函式宣告 (幾種應用) 18 PNode CreateList(void); // 宣告建立連結串列函式 19 void TraverseList(PNode List); //
宣告遍歷連結串列函式 20 void InsertList(PNode List, int pos, int val); // 宣告連結串列插入函式 21 void DeleteTheList(PNode List); // 宣告刪除整個連結串列函式 22 void DeleteList(PNode List, int pos); // 宣告刪除連結串列元素函式 23 PNode FindList(PNode List); // 宣告連結串列查詢函式 24 25 //主調函式 26 int main() { 27 PNode List = CreateList();//
建立一個指標指向建立的連結串列的頭指標 28 TraverseList(List); //遍歷連結串列 29 InsertList(List, 3, 999);//在3處插入新節點 30 TraverseList(List); //再次遍歷連結串列 31 DeleteList(List, 3); //刪除插入的節點 32 TraverseList(List); //遍歷連結串列 33 FindList(List); //查詢連結串列 34 DeleteTheList(List); //刪除整個節點 35 TraverseList(List); //遍歷連結串列 36 return 0; 37 } 38 39 //建立連結串列 40 PNode CreateList(void) { 41 int len; //連結串列長度 42 int val; //存放連結串列節點的資料 43 PNode PHead = (PNode)malloc(sizeof(Node)); //建立頭指標並分配空間 44 if (PHead == NULL) { 45 printf("申請頭指標失敗\n"); 46 exit(-1); 47 } 48 49 PNode PTail = PHead; //宣告尾指標初始化指向頭指標 50 PTail->next = NULL; //使尾指標(目前是頭指標)指向空 51 52 printf("請輸入要建立的連結串列長度:\n"); 53 //scanf("%d", &len); 移植用 54 scanf_s("%d", &len); //輸入節點個數 55 for (int i = 0; i < len; i++) { 56 PNode PNew = (PNode)malloc(sizeof(Node)); //分配新節點 57 if (PNew == NULL) { 58 printf("申請新節點失敗\n"); 59 exit(-1); 60 } 61 62 printf("請輸入第%d個節點的資料:\n", i + 1); 63 //scanf("%d", &val); 64 scanf_s("%d", &val); 65 66 PNew->data = val; //輸入資料 67 PTail->next = PNew; //使上一個Next指向新節點 68 PNew->next = NULL; //使節點指向空(作為尾指標) 69 PTail = PNew; //讓尾指標指向新節點 70 } 71 printf("連結串列建立成功\n"); 72 return PHead; 73 } 74 75 76 // 宣告遍歷連結串列函式 77 void TraverseList(PNode List) { 78 PNode P = List->next; //將首節點賦值給臨時變數P 79 printf("遍歷連結串列的值為:"); 80 if (P == NULL) { 81 printf("連結串列為空\n"); 82 } 83 while (P != NULL) { 84 printf("%d ", P->data); 85 P = P->next; 86 } 87 printf("\n"); 88 } 89 90 // 宣告連結串列插入函式 91 void InsertList(PNode List, int pos, int val) { 92 int position = 0; 93 PNode P = List; 94 //尋找插入節點的前驅節點 95 while (P != NULL && position < pos - 1) { 96 P = P->next; 97 position++; 98 } 99 100 PNode Temp = (PNode)malloc(sizeof(Node)); // 分配一個臨時節點用來儲存要插入的資料 101 if (Temp == NULL) 102 { 103 printf("記憶體分配失敗!"); 104 exit(-1); 105 } 106 107 //插入節點 108 Temp->data = val; 109 Temp->next = P->next; 110 P->next = Temp; 111 } 112 113 // 宣告刪除連結串列元素函式 114 void DeleteList(PNode List, int pos) { 115 int position = 0; 116 PNode P = List; 117 //尋找要刪除的節點的前驅節點 118 while (P != NULL && position < pos - 1) { 119 P = P->next; 120 position++; 121 } 122 123 //刪除此節點 124 PNode Temp = P->next; // 定義臨時指標Tmp指向要刪除的節點 125 P->next = Temp->next; 126 free(Temp); 127 Temp = NULL; //避免產生野指標 128 } 129 130 // 宣告連結串列查詢函式 131 PNode FindList(PNode List) { 132 int position = 0; 133 PNode P = List->next; //建立臨時節點用於查詢節點 134 int num = 0; //記錄查詢的節點位置 135 int val; //存放要查詢的資料 136 printf("請輸入要查詢的數值:\n"); 137 //scanf("%d", &val); 138 scanf_s("%d", &val); 139 140 while (P != NULL && P->data != val) { 141 P = P->next; 142 num++; 143 } 144 145 if (P != NULL) { 146 printf("查詢的數值的位置為%d\n", num + 1); 147 } 148 else { 149 printf("查詢數值不存在\n"); 150 } 151 return P; 152 } 153 154 // 宣告刪除整個連結串列函式 155 void DeleteTheList(PNode List) { 156 PNode P, Temp; 157 P = List->next; 158 List->next = NULL; //避免野指標 159 while (P != NULL) { 160 Temp = P->next; 161 free(P); 162 P = Temp; 163 } 164 printf("刪除連結串列成功!\n"); 165 }