單鏈表的初始化,創建,插入,刪除和反轉
阿新 • • 發佈:2017-10-02
csdn def nbsp 資料 技術分享 while ctr tail scan
單鏈表的初始化,創建,插入,刪除和反轉
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 typedef int Elemtype; 5 typedef struct Node 6 { 7 Elemtype data; 8 struct Node *next; 9 }Node, *LinkedList; 10 11 //單鏈表的初始化 12 LinkedList LinkedListInit() 13 { 14 Node *L; 15 L = (Node*)malloc(sizeof(Node)); 16 if (L == NULL) 17 { 18 printf("申請內存空間失敗\n"); 19 } 20 L->next = NULL; 21 return L; 22 } 23 24 //單鏈表的創建一:頭插法建立單鏈表 25 LinkedList LinkedListCreatH() 26 { 27 Node *L; 28 L = (Node *)malloc(sizeof(Node)); 29 L->next = NULL; 30 31Elemtype x; 32 while (scanf("%d", &x) != EOF) 33 { 34 Node *p; 35 p = (Node *)malloc(sizeof(Node)); 36 p->data = x; 37 p->next = L->next; 38 L->next = p; 39 } 40 return L; 41 } 42 43 //單鏈表的創建二:尾插法建立單鏈表 44 LinkedList LinkedListCreatT()45 { 46 Node *L; 47 L = (Node *)malloc(sizeof(Node)); 48 L->next = NULL; 49 50 Node *r; 51 r = L; 52 Elemtype x; 53 while (scanf("%d", &x) != EOF) 54 { 55 Node *p; 56 p = (Node *)malloc(sizeof(Node)); 57 p->data = x; 58 //p->next = NULL; 59 r->next = p; 60 r = p; 61 } 62 r->next = NULL; 63 return L; 64 } 65 66 //單鏈表的插入,在鏈表的第i個位置插入x的元素 67 //要在第i個位置插入,就得先找到第(i-1)個位置,插在它後面 68 LinkedList LinkedListInsert(LinkedList L, int i, Elemtype x) 69 { 70 Node *pre; 71 pre = L; 72 int tempi = 0; 73 for (tempi = 1; tempi < i; tempi++) 74 pre = pre->next; 75 Node *p; 76 p = (Node *)malloc(sizeof(Node)); 77 p->data = x; 78 p->next = pre->next; 79 pre->next = p; 80 return L; 81 } 82 83 //單鏈表的刪除,在鏈表中刪除第一個值為x的元素 84 LinkedList LinkedListDelete(LinkedList L, Elemtype x) 85 { 86 Node *pre, *p; 87 p = L->next; 88 while (p->data != x) 89 { 90 pre = p; 91 p = p->next; 92 } 93 pre->next = p->next; 94 free(p); 95 return L; 96 } 97 98 //單鏈表的反轉 99 LinkedList LinkedListReverse(LinkedList L) 100 { 101 102 Node *rhead = NULL; 103 Node *prev = NULL; 104 Node *p = L->next;//如果原鏈表的頭是一個結點,結點的內容為任意值,p要指向頭的下一個結點才是鏈表的第一個值 105 //Node *p = L;//如果原鏈表的頭是一個指針,p直接等於Lj就可以了,L指的就是鏈表的第一個值 106 Node *pnext = NULL; 107 while (p != NULL) 108 { 109 pnext = p->next; 110 if (pnext == NULL) 111 rhead = p; 112 p->next = prev; 113 prev = p; 114 p = pnext; 115 } 116 free(L); 117 return rhead; 118 } 119 120 int main() 121 { 122 LinkedList list, start; 123 124 //單鏈表的創建一:頭插法建立單鏈表 125 printf("請輸入單鏈表的數據:"); 126 list = LinkedListCreatH(); 127 for (start = list->next; start != NULL; start = start->next) 128 printf("%d", start->data); 129 printf("\n"); 130 131 //單鏈表的創建二:尾插法建立單鏈表 132 printf("請輸入單鏈表的數據:"); 133 list = LinkedListCreatT(); 134 for (start = list->next; start != NULL; start = start->next) 135 printf("%d", start->data); 136 printf("\n"); 137 138 //單鏈表的插入,在鏈表的第i個位置插入x的元素 139 int i,x; 140 printf("請輸入插入數據的位置:"); 141 scanf("%d", &i); 142 printf("請輸入插入數據的值:"); 143 scanf("%d", &x); 144 LinkedListInsert(list, i, x); 145 for (start = list->next; start != NULL; start = start->next) 146 printf("%d", start->data); 147 printf("\n"); 148 149 //單鏈表的刪除,在鏈表中刪除第一個值為x的元素 150 printf("請輸入要刪除的元素的值:"); 151 scanf("%d", &x); 152 LinkedListDelete(list, x); 153 for (start = list->next; start != NULL; start = start->next) 154 printf("%d", start->data); 155 printf("\n"); 156 157 //單鏈表的反轉 158 Node *rhead; 159 rhead=LinkedListReverse(list); 160 for (start = rhead; start != NULL; start = start->next) 161 printf("%d", start->data); 162 printf("\n"); 163 164 system("pause"); 165 return 0; 166 } 167 //註意:結束輸入的時候連續輸入三個ctrl+z
運行結果:
註意:在VS2015環境下,需要輸入三個ctrl+z才能結束while中的scanf;在VC++6.0中一個ctrl+z就可以結束。
——如有不對的地方,非常歡迎給予指導!
——【感謝】部分資料來源於http://blog.csdn.net/m_zhurunfeng/article/details/54809821
——【感謝】部分資料來源於《劍指OFFER》
單鏈表的初始化,創建,插入,刪除和反轉