国产免费AV|泡泡玛特欧洲总部将设在伦敦|中文天堂网www新版资源在线|一本久道综合在线中文|国精产品一二三产区的使用方法|香蕉鱼在线观看|www.27eee
ELEOK
標題:
控制臺測試鏈表程序-C語言指針練習的好例子
[打印本頁]
作者:
Jonsa
時間:
2021-11-15 14:57
標題:
控制臺測試鏈表程序-C語言指針練習的好例子
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;
}
復制代碼
歡迎光臨 ELEOK (http://www.afoofa.cn/)
Powered by Discuz! X5.0