1.png (21.11 KB)
下載附件
2021-11-15 14:56 上傳
- #include "stdio.h"
- #include "malloc.h"
- typedef struct node {
- int data;
- node* next;
- }Node;
- typedef struct {
- unsigned char id;
- void(*Func)(Node **head);
- }FuncSt;
- Node* uartListHead = NULL;
- int ListCreat(Node **p_list, int size)
- {
- node* p = NULL;
- int i;
- *p_list = (Node*)malloc(sizeof(Node));
- if (*p_list == NULL)
- {
- return 0;
- }
- (*p_list)->next = NULL;
- for (i = size; i > 0; i--)
- {
- p = (Node*)malloc(sizeof(Node));
- if (p == NULL)
- {
- return 0;
- }
- p->data = i;
- p->next =(*p_list)->next;
- (*p_list)->next = p;
- }
- return 1;
- }
- void ScanPrintList(Node **head)
- {
- Node *p;
- Node *q;
- p = (*head)->next;
- printf("\n\n鏈表遍歷結果如下:\n");
- if (p == NULL)
- {
- printf("空鏈表\n");
- }
- while(p != NULL)
- {
- printf("%d\t",p->data);
- q = p->next;
- p = q;
- }
- printf("\n\n");
- }
- void ListGetDat(Node **head, int dat)
- {
-
- Node* tempPtr = (Node*)malloc(sizeof(Node));
- Node *p = NULL;
- int count = 0;
- int flag = 0;
- if (tempPtr == NULL)
- {
- return;
- }
- if ((*head)->next == NULL)
- {
- printf("未查到該元素");
- return;
- }
- p = *head;
- while(p != NULL)
- {
- if (p->data == dat)
- {
- printf("第%d個元素是%d\n",count,dat);
- flag = 1;
- }
- p = p->next;
- count++;
- }
- if (flag == 0)
- {
- printf("未查到該元素\n");
- }
- }
- void ListRemoveDat(Node **head, int dat)
- {
-
- Node *p = NULL;
- Node *q = NULL;
- int count = 0;
- int flag = 0;
- if ((*head)->next == NULL)
- {
- printf("未查到該元素 無法刪除");
- return;
- }
- p = *head;
- q = p;
- while(p != NULL)
- {
- if (p->data == dat)
- {
- printf("第%d個元素是%d 已刪除\n",count,dat);
- flag = 1;
- q->next = p->next;
- free(p);
- p = q;
- }
- q = p;
- p = p->next;
- count++;
- }
- if (flag == 0)
- {
- printf("未查到該元素 無法刪除\n");
- }
- }
- void ListRemoveDatTest(Node **head)
- {
- int temp;
- printf("remove input:\n");
- scanf("%d",&temp);
- ListRemoveDat(head, temp);
- }
- void ListGetDatTest(Node **head)
- {
- int temp;
- printf("aim input:\n");
- scanf("%d",&temp);
- ListGetDat(head, temp);
- }
- void ListTailAdd(Node **head, int dat)
- {
-
- Node* tempPtr = (Node*)malloc(sizeof(Node));
- Node *p = NULL;
- if (tempPtr == NULL)
- {
- return;
- }
- if ((*head)->next == NULL)
- {
- (*head)->next = tempPtr;
- tempPtr->data = dat;
- tempPtr->next = NULL;
- return;
- }
- p = *head;
- while(p->next != NULL)
- {
- p = p->next;
- }
- p->next = tempPtr;
- tempPtr->data = dat;
- tempPtr->next = NULL;
- }
- void TailAddTest(Node **head)
- {
- int temp;
- printf("value input:\n");
- scanf("%d",&temp);
- ListTailAdd(head, temp);
- }
- void ListClean(Node **head)
- {
- Node *p = NULL;
- while((*head)->next != NULL)
- {
- p = (*head)->next;
- (*head)->next = p->next;
- free(p);
- }
- }
- FuncSt g_funcTable[] = {
- {1, ListClean},
- {2, TailAddTest},
- {3, ListGetDatTest},
- {4, ScanPrintList},
- {5, ListRemoveDatTest},
- };
- void FnucHandleTask(unsigned char id)
- {
- for (int i = 0; i < (sizeof(g_funcTable) / sizeof(g_funcTable[0])); i++)
- {
- if (id == g_funcTable[i].id)
- {
- g_funcTable[i].Func(&uartListHead);
- break;
- }
- }
- }
- void InitPrnt(void)
- {
- printf("1:鏈表清空\t2:尾部添加元素\t3:查找指定元素\t4:遍歷鏈表\t5:刪除指定元素\t6:指定位置數據更改\t\n");
- }
- int main()
- {
- unsigned char testCategory;
- if (ListCreat(&uartListHead, 10) == 1)
- {
- printf("list creat succes!\n");
- }
- while(1)
- {
- InitPrnt();
- scanf("%d",&testCategory);
- FnucHandleTask(testCategory);
- }
- return 1;
- }
復制代碼
【必讀】版權免責聲明
1、本主題所有言論和內容純屬會員個人意見,與本論壇立場無關。2、本站對所發內容真實性、客觀性、可用性不做任何保證也不負任何責任,網友之間僅出于學習目的進行交流。3、對提供的數字內容不擁有任何權利,其版權歸原著者擁有。請勿將該數字內容進行商業交易、轉載等行為,該內容只為學習所提供,使用后發生的一切問題與本站無關。 4、本網站不保證本站提供的下載資源的準確性、安全性和完整性;同時本網站也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的損失或傷害。 5、本網站所有軟件和資料均為網友推薦收集整理而來,僅供學習用途使用,請務必下載后兩小時內刪除,禁止商用。6、如有侵犯你版權的,請及時聯系我們(電子郵箱1370723259@qq.com)指出,本站將立即改正。
|