資料結構實驗-線性表
阿新 • • 發佈:2022-03-18
#include <stdio.h> #include <stdlib.h> #include <windows.h> #define MAXSIZE 101 typedef int DataType; typedef struct { int *data; int length; }SeqList; // 1.Init void InitList (SeqList* L) { L->data = new int [MAXSIZE]; L->length = 0; } //2. Create bool CreateList (SeqList* L, DataType a[], int n) { if(n > MAXSIZE) { printf("The space is not enough, cann't create a List\n"); return false; } else { for(int i = 0; i < n; i ++ ) { L->data[i] = a[i]; } L->length = n; return 1; } } // 3. get the length of List int GetLength(SeqList* L) { return L -> length; } // 4. print the List void PrintList(SeqList* L) { for(int i = 0; i < L->length; i ++ ) { printf("%d%c", L->data[i], " \n"[i == L->length - 1]); } } // 5. return the first index of this value (index start at 1 not 0) // if the key of return is zero , means a query failure int Locate(SeqList* L, DataType value) { for(int i = 0; i < L->length; i ++ ) { if(L->data[i] == value) { return i + 1; } } return 0; } // 6. find the value of the index from the List bool GetValue(SeqList* L, int idx, int* value) { if(idx < 1 || idx > L->length) { return false; } else { *value = L->data[idx - 1]; // store in value return true; } } // 7. insert The range of insert is [1, L->length + 1] (idx - 1) bool Insert(SeqList* L, int idx, int value) { if(L->length >= MAXSIZE) { return false; } else if(idx < 1 || idx > L->length + 1) { return false; } else { for(int i = L->length; i >= idx; i -- ) { L->data[i] = L->data[i - 1]; } L->data[idx - 1] = value; ++ L->length; return true; } } // 8. delete bool Delete(SeqList* L, int idx, int* value) { if(L->length == 0) { printf("The list is empty, delete fault\n"); return false; } else if(idx < 1 || idx > L->length) { printf("The index is illegal, delete fault\n"); return false; } else { *value = L->data[idx - 1]; // store in value for(int i = idx; i < L->length; i ++ ) { L->data[i - 1] = L->data[i]; } -- L->length; return true; } } // 9. judge the list is empty bool Empty(SeqList* L) { return (L->length == 0); } // 10. erase the list void erase(SeqList *L) { delete L->data; L->length = 0; } // 11. calcate one's prev int GetPrev(SeqList *L, int idx, int *value) { if(idx <= 1 || idx > L->length) { return false; // not exist } else { *value = L->data[idx - 2]; //start at zero return true; } } // 12. calcate one's next bool GetNext(SeqList *L, int idx, int *value) { if(idx < 1 || idx > L->length - 1) { return false; } else { *value = L->data[idx]; return true; } } //print the list void print() { printf("1----清空線性表\n"); printf("2----判斷線性表是否為空\n"); printf("3----求線性表長度\n"); printf("4----獲取線性表指定位置元素\n"); printf("5----求前驅\n"); printf("6----求後繼\n"); printf("7----線上性表指定位置插入指定元素\n"); printf("8----刪除線性表指定位置元素\n"); printf("9----顯示線性表\n"); printf(" 推出,輸出一個負數!\n"); printf("注意:線性表的下標從 1 開始,線性表的最大長度是 100\n"); printf("請輸入操作程式碼: "); } int main() { SeqList L; InitList(&L); while(true) { print(); int op, x, idx; scanf("%d", &op); if(op < 0) { printf("操作結束,再見!\n"); break; } else if(op == 1) { erase(&L); } else if(op == 2) { if(Empty(&L)) printf("線性表為空"); else printf("線性表不為空"); } else if(op == 3) { printf("線性表的長度為: %d\n", GetLength(&L)); } else if(op == 4) { printf("請輸入要查詢的位置: "); scanf("%d", &idx); if(GetValue(&L, idx, &x)) { printf("位於 %d 的元素是: %d\n", idx, x); } else { printf("查詢失敗,請檢查輸入是否合法\n"); } } else if(op == 5){ printf("請輸入要查詢的位置: "); scanf("%d", &idx); if(GetPrev(&L, idx, &x)) { printf("位置 %d 的前驅元素是: %d\n", idx, x); } else { printf("查詢失敗,請檢查輸入是否合法\n"); } } else if(op == 6){ printf("請輸入要查詢的位置: "); scanf("%d", &idx); if(GetNext(&L, idx, &x)) { printf("位置 %d 的後繼元素是: %d\n", idx, x); } else { printf("查詢失敗,請檢查輸入是否合法\n"); } } else if(op == 7) { printf("請輸入要插入的位置: "); scanf("%d", &idx); printf("請輸入要插入的元素: "); scanf("%d", &x); if(Insert(&L, idx, x)) { printf("插入成功!\n"); } else { printf("插入失敗!請檢出輸入是否合法\n"); } } else if(op == 8) { printf("請輸入要刪除的位置: "); scanf("%d", &idx); if(Delete(&L, idx, &x)) { printf("刪除成功!\n 刪除的位置的元素是 %d\n", x); } else { printf("刪除失敗,請檢查輸入是否合法\n"); } } else if(op == 9) { PrintList(&L); } Sleep(1000); system("cls"); } return 0; }