单链表头插法
本帖最后由 冰逸琉璃 于 2021-4-6 20:05 编辑麻烦大佬帮我看看这个头插法错那了{:10_266:}
#include <stdio.h>
#include <stdlib.h>
struct Papt
{
char lxr;
char dh;
struct Papt *pt;
};
void xa(struct Papt **px);//添加节点
void xb(struct Papt *papt);//打印当前链表
void freepx(struct Papt **px);//释放内存空间
void xa(struct Papt **px)
{
struct Papt *papt;
papt = (struct Papt *)malloc(sizeof(struct Papt));
if (papt == NULL)
{
printf("内存分配失败!");
}
printf("请输入联系人名字:");
scanf_s("%s", papt->lxr, 128);
printf("请输入电话:");
scanf_s("%s", papt->dh, 128);
printf("\n");
if (*px == NULL)
{
*px = papt;
papt->pt = NULL;
}
else
{
struct Papt *py;
py = *px;
while (py->pt != NULL)
{
py = py->pt;
}
py->pt = papt;
papt->pt = NULL;
}
}
void xb(struct Papt *papt)
{
struct Papt *temp = papt;
while (temp != NULL)
{
static int a = 1;
printf("联系人%d:%s\n", a, temp->lxr, 10);
printf("电话号码:%s\n", temp->dh, 10);
printf("\n");
temp = temp->pt;
a++;
}
}
void freepx(struct Papt **px)
{
struct Papt *book;
struct Papt *py;
while (*px != NULL)
{
py = *px;
*px = (*px)->pt;
free(py);
}
}
int main(void)
{
printf("| 欢迎使用通讯录管理程序 |\n");
printf("|--- 插入新的联系人:1 ---|\n");
printf("|--- 查找已有联系人:2 ---|\n");
printf("|--- 更改已有联系人:3 ---|\n");
printf("|--- 删除已有联系人:4 ---|\n");
printf("|--- 显示当前通讯录:5 ---|\n");
printf("|--- 退出通讯录程序:6 ---|\n");
printf("\n");
struct Papt *px = NULL;
int zl;
while (1)
{
printf("请输入指令:");
scanf_s("%d", &zl);
if (zl == 1)
{
printf("\n");
xa(&px);
}
else if (zl == 5)
{
printf("\n");
xb(px);
}
else if (zl == 6)
{
printf("\n");
printf("程序结束\n");
freepx(px);
break;
}
else
{
printf("\n");
printf("请输入有效指令!\n");
}
}
return 0;
} 建议抄小甲鱼代码
《带你学c带你飞》上面有 #include <stdio.h>
#include <stdlib.h>
struct Papt
{
char lxr;
char dh;
struct Papt *pt;
};
void xa(struct Papt **px);//添加节点
void xb(struct Papt *papt);//打印当前链表
void freepx(struct Papt **px);//释放内存空间
void xa(struct Papt **px)
{
struct Papt *papt;
papt = (struct Papt *)malloc(sizeof(struct Papt));
if (papt == NULL)
{
printf("内存分配失败!");
}
printf("请输入联系人名字:");
scanf("%s", papt->lxr);
printf("请输入电话:");
scanf("%s", papt->dh);
printf("\n");
papt->pt = NULL;
papt->pt = *px;
*px = papt;
/*
if (*px == NULL)
{
*px = papt;
papt->pt = NULL;
}
else
{
struct Papt *py;
py = *px;
while (py->pt != NULL)
{
py = py->pt;
}
py->pt = papt;
papt->pt = NULL;
}
*/
}
void xb(struct Papt *papt)
{
struct Papt *temp = papt;
while (temp != NULL)
{
static int a = 1;
printf("联系人%d:%s\n", a, temp->lxr, 10);
printf("电话号码:%s\n", temp->dh, 10);
printf("\n");
temp = temp->pt;
a++;
}
}
void freepx(struct Papt **px)
{
struct Papt *book;
struct Papt *py;
while (*px != NULL)
{
py = *px;
*px = (*px)->pt;
free(py);
}
}
int main(void)
{
printf("| 欢迎使用通讯录管理程序 |\n");
printf("|--- 插入新的联系人:1 ---|\n");
printf("|--- 查找已有联系人:2 ---|\n");
printf("|--- 更改已有联系人:3 ---|\n");
printf("|--- 删除已有联系人:4 ---|\n");
printf("|--- 显示当前通讯录:5 ---|\n");
printf("|--- 退出通讯录程序:6 ---|\n");
printf("\n");
struct Papt *px = NULL;
int zl;
while (1)
{
printf("请输入指令:");
scanf("%d", &zl);
if (zl == 1)
{
printf("\n");
xa(&px);
}
else if (zl == 5)
{
printf("\n");
xb(px);
}
else if (zl == 6)
{
printf("\n");
printf("程序结束\n");
freepx(px);
break;
}
else
{
printf("\n");
printf("请输入有效指令!\n");
}
}
return 0;
}
页:
[1]