欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標(biāo)題:
原創(chuàng) 控制臺(tái)測(cè)試鏈表程序-C語(yǔ)言指針練習(xí)的好例子
[打印本頁(yè)]
作者:
jizhongbiao
時(shí)間:
2021-10-9 16:55
標(biāo)題:
原創(chuàng) 控制臺(tái)測(cè)試鏈表程序-C語(yǔ)言指針練習(xí)的好例子
51hei圖片_20211009165251.png
(22.63 KB, 下載次數(shù): 87)
下載附件
2021-10-9 16:55 上傳
C語(yǔ)言源程序:
/* mcu6666 */
#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鏈表遍歷結(jié)果如下:\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個(gè)元素是%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("未查到該元素 無(wú)法刪除");
return;
}
p = *head;
q = p;
while(p != NULL)
{
if (p->data == dat)
{
printf("第%d個(gè)元素是%d 已刪除\n",count,dat);
flag = 1;
q->next = p->next;
free(p);
p = q;
}
q = p;
p = p->next;
count++;
}
if (flag == 0)
{
printf("未查到該元素 無(wú)法刪除\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:鏈表清空\(chéng)t2:尾部添加元素\t3:查找指定元素\t4:遍歷鏈表\t5:刪除指定元素\t6:指定位置數(shù)據(jù)更改\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;
}
復(fù)制代碼
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1