該例子說明幾個問題
1. 在使用局部變量時,一定要初始化。本例子在InsertList函數中,局部變量i沒初始化,程序運行時出錯。 2. printf("%-5d ", p->data);其中%-5d表示變量p->data占5個字符,並且左對齊; %5d表示右對齊。 ********************************************************************/#include <stdio.h>#include <malloc.h>#include <stdlib.h>/********************************************************************* 輸入鏈表的長度 len = 7 輸入鏈表第 1 個元素的值: 99 輸入鏈表第 2 個元素的值: -23 輸入鏈表第 3 個元素的值: 52 輸入鏈表第 4 個元素的值: -77 輸入鏈表第 5 個元素的值: 45 輸入鏈表第 6 個元素的值: 38 輸入鏈表第 7 個元素的值: 123 the list is not empty, the length of list is 7 99 -23 52 -77 45 38 123 刪除鏈表中第3個元素,該元素是52 the deleted list is 99 -23 -77 45 38 123 the length of list is 6 在鏈表第3個元素前插入88 the inserted list is 99 -23 88 -77 45 38 123 the length of list is 7 the sorted list is -77 -23 38 45 88 99 123 the length of list is 7 Press any key to continue
typedef struct Node{ int data; struct Node *pNext;} NODE;
typedef struct{ NODE *pHead; NODE *pTail; int len;} LINKLIST;
void InitList(LINKLIST *pList){ pList->pHead = pList->pTail = (NODE *)malloc(sizeof(NODE)); if (pList->pHead == NULL) { printf("內存分配失敗\n"); exit(-1); } pList->pTail->pNext = NULL; pList->len = 0;
return;}
void CreatList(LINKLIST *pList){ int n; int i;
printf("輸入鏈表的長度 len = "); scanf("%d", &n);
for (i = 0; i < n; i++) { NODE *pNew = (NODE *)malloc(sizeof(NODE)); if (pNew == NULL) { printf("內存分配失敗\n"); exit(-1); } pNew->pNext = NULL;
printf("輸入鏈表第 %d 個元素的值: ", i + 1); scanf("%d", &pNew->data); pList->pTail->pNext = pNew; pList->pTail = pNew; pList->len++; }
return;}
void TraverseList(LINKLIST *pList){ NODE *p = pList->pHead->pNext;
while (p != NULL) { printf("%-5d ", p->data); p = p->pNext; } printf("\n");
return;}
bool ListEmpty(LINKLIST *pList){ if (pList->len == 0) return true; else return false;}
int ListLength(LINKLIST *pList){ return pList->len;}
bool DeleteList(LINKLIST *pList, int pos, int *pVal){ int i = 0; NODE *p = pList->pHead;
while ((p->pNext != NULL) && (i < pos - 1)) { i++; p = p->pNext; } // 鏈表為空,返回false if ((p->pNext == NULL) || (i > pos - 1)) return false;
NODE *q = p->pNext; *pVal = q->data; p->pNext = q->pNext; free(q); q = NULL; pList->len--;
return true;}
bool InsertList(LINKLIST *pList, int pos, int val){ int i = 0; NODE *p = pList->pHead;
while ((p != NULL) && (i < pos - 1)) { i++; p = p->pNext; }
if ((i > pos - 1) || (p == NULL)) return false;
NODE *pNew = (NODE *)malloc(sizeof(NODE)); pNew->data = val; NODE *q = p->pNext; p->pNext = pNew; pNew->pNext = q; pList->len++;
return true;}
void SortList(LINKLIST *pList){ int i, j, tmp; int len = ListLength(pList); NODE *p, *q;
for (i = 0, p = pList->pHead->pNext; i < pList->len - 1; i++, p = p->pNext) { for (j = i + 1, q = p->pNext; j < pList->len; j++, q = q->pNext) { if (p->data > q->data) // a[i] > a[j] { tmp = p->data; // tmp = a[i]; p->data = q->data; // a[i] = a[j]; q->data = tmp; // a[j] = tmp; } } }
return;}
int main(void){ int val = 0; LINKLIST list;
InitList(&list); CreatList(&list); if (ListEmpty(&list)) { printf("the list is empty\n"); return 0; } else { printf("the list is not empty, the length of list is %d\n", list.len); TraverseList(&list); } DeleteList(&list, 3, &val); printf("刪除鏈表中第3個元素,該元素是%d\n", val); printf("the deleted list is\n"); TraverseList(&list); printf("the length of list is %d\n", list.len); printf("在鏈表第3個元素前插入88\n"); InsertList(&list, 3, 88); printf("the inserted list is \n"); TraverseList(&list); printf("the length of list is %d\n", list.len); printf("the sorted list is \n"); SortList(&list); TraverseList(&list); printf("the length of list is %d\n", list.len);
return 0;}