标题:
原创 控制台测试链表程序-C语言指针练习的好例子
[打印本页]
作者:
jizhongbiao
时间:
2021-10-9 16:55
标题:
原创 控制台测试链表程序-C语言指针练习的好例子
51hei图片_20211009165251.png
(22.63 KB, 下载次数: 87)
下载附件
2021-10-9 16:55 上传
C语言源程序:
/* 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链表遍历结果如下:\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;
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1