1-線性表單連結串列儲存
阿新 • • 發佈:2022-04-02
#include<stdio.h> #include<Windows.h> typedef int ElemType; typedef struct LNode { ElemType data; LNode* next; }LNode, * ListPoint; //ListPoint InitList();初始化單鏈表 //ListPoint List_HeadInsert(ListPoint &L);頭插法建立單鏈表 //ListPoint List_TailInsert(ListPoint &L);尾插法建立單鏈表 //LNode* GetElem(ListPoint L, int i);按序號查詢結點值 //LNode* LocateElem(ListPoint L, ElemType e); 按值查詢表結點 //ListPoint List_Insert(ListPoint L,, int i, ElemType e) 指定位置插入 //ListPoint DeleteElem(ListPoint L,);刪除結點 //int Length(ListPoint L);求表長 //void PrintList(ListPoint L);列印 //ListPoint InitList();初始化單鏈表,建立代空表頭的連結串列 ListPoint InitList() { ListPoint L = (ListPoint)malloc(sizeof(LNode)); L->next = NULL; return L; } //void List_HeadInsert(ListPoint L, ElemType e);頭插法建立單鏈表 void List_HeaderInsert(ListPoint L, ElemType e) { LNode* newNode = (LNode*)malloc(sizeof(LNode)); newNode->data = e; newNode->next = L->next; L->next = newNode; } //void List_TailInsert(ListPoint L, ElemType e);尾插法建立單鏈表 void List_TailInsert(ListPoint L, ElemType e) { LNode* newNode = (LNode*)malloc(sizeof(LNode)); LNode* tailNode = L; while (tailNode->next) { tailNode = tailNode->next; } newNode->data = e; newNode->next = NULL; tailNode->next = newNode; tailNode = newNode; } //LNode* GetElem(ListPoint L, int i);按序號查詢結點值 LNode* GetElem(ListPoint L, int i) { if (i < 0) return NULL; else if (i == 0) return L; L = L->next; int index = 1; while (L && index < i) { L = L->next; index++; } return L; } //LNode* LocateElem(ListPoint L, ElemType e); 按值查詢表結點 LNode* LocateElem(ListPoint L, ElemType e) { L = L->next; while (L) { if (L->data == e) { return L; } L = L->next; } return NULL; } //ListPoint List_Insert(ListPoint L,, int i, ElemType e) 指定位置插入 ListPoint List_Insert(ListPoint L, int i, ElemType e) { LNode* forthNode; if ((forthNode = GetElem(L, i-1))) { LNode* newNode = (LNode*)malloc(sizeof(LNode)); newNode->data = e; newNode->next = forthNode->next; forthNode->next = newNode; return newNode; } return NULL; } //ListPoint DeleteElem(ListPoint L, int i);刪除結點 ElemType DeleteElem(ListPoint L, int i) { LNode* forthNode; if ((forthNode = GetElem(L, i - 1)) && forthNode->next) { LNode* delNode = forthNode->next; forthNode->next = delNode->next; ElemType i = delNode->data; free(delNode); return i; } return NULL; } //int Length(ListPoint L);求表長 int Length(ListPoint L) { int count = 0; L = L->next; while (L) { count++; L = L->next; } return count; } //void DestoryList(ListPoint L); 刪除整個連結串列 void DestoryList(ListPoint L) { } //void PrintList(ListPoint L);列印 void PrintList(ListPoint L) { L = L->next; while (L){ printf("%3d", L->data); L = L->next; } putchar('\n'); } int main(int argc, char* argv) { ListPoint L = InitList(); PrintList(L); printf("連結串列總長度:%d\n", Length(L)); for (int i = 0; i < 5; i++) { List_HeaderInsert(L, i); } for (int i = 10; i < 17; i++) { List_TailInsert(L, i); } PrintList(L); printf("連結串列總長度:%d\n", Length(L)); LNode* node1 = GetElem(L,12); if (node1) { printf("第10個元素是%i\n", node1->data); } LNode* node2= LocateElem(L, 12); if (node2) { printf("找到了\n"); } else { printf("沒找到\n"); } List_Insert(L, 0, 0); List_Insert(L, 1, 1); List_Insert(L, 2, 2); List_Insert(L, 100, 1); PrintList(L); List_Insert(L, 15, 19); PrintList(L); DeleteElem(L, -1); PrintList(L); DeleteElem(L, 0); PrintList(L); DeleteElem(L, 1); PrintList(L); DeleteElem(L, 10); PrintList(L); DeleteElem(L, 13); PrintList(L); DeleteElem(L, 100); PrintList(L); system("pause"); return 0; }