#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct LinkList
{
int m_Num;
struct LinkList*next;
}List;
void showMenu()
{
printf("**************\n");
printf("**1.添加节点**\n");
printf("**2.遍历链表**\n");
printf("**3.查找节点**\n");
printf("**4.删除节点**\n");
printf("**5.插入节点**\n");
printf("**************\n");
}
void addList(List*end,List*head)
{
end->next=(List*)malloc(sizeof(List));
end=end->next;
printf("请输入该节点编号:");
scanf("%d",&end->m_Num);
end->next=NULL;
(head->m_Num)++;
printf("成功添加节点");
}
void scanList(List*head)
{
if(head->m_Num==0) printf("当前链表没有节点");
else
{
List*tmp=head->next;
printf("当前共有%d个节点\n",head->m_Num);
while(tmp!=NULL)
{
printf("%d\n",tmp->m_Num);
tmp=tmp->next;
}
}
}
int main()
{
List*head=(List*)malloc(sizeof(List));
head->next=NULL;
List*end=head;
head->m_Num=0;
int choose=0;
while(1)
{
showMenu();
scanf("%d",&choose);
switch(choose)
{
case 1:
{
addList(end,head);
system("pause");
system("cls");
}
break;
case 2:
{
scanList(head);
system("pause");
system("cls");
}
break;
case 3:
break;
default:
break;
}
}
return 0;
}
这个有头结点链表哪里错了,好像结点没有连上,不加头结点最多一个结点,end指向最后一个结点
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
CSDN专家-深度学习进阶 2022-03-13 09:40关注你创建节点的时候每次返回end就好了,这样就不会丢失了

#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct LinkList { int m_Num; struct LinkList*next; } List; void showMenu() { printf("**************\n"); printf("**1.添加节点**\n"); printf("**2.遍历链表**\n"); printf("**3.查找节点**\n"); printf("**4.删除节点**\n"); printf("**5.插入节点**\n"); printf("**************\n"); } List* addList(List*end,List*head) { end->next=(List*)malloc(sizeof(List)); end=end->next; printf("请输入该节点编号:"); scanf("%d",&end->m_Num); end->next=NULL; (head->m_Num)++; printf("成功添加节点"); return end; } void scanList(List*head) { if(head->m_Num==0) printf("当前链表没有节点"); else { List*tmp=head->next; printf("当前共有%d个节点\n",head->m_Num); while(tmp!=NULL) { printf("%d\n",tmp->m_Num); tmp=tmp->next; } } } int main() { List*head=(List*)malloc(sizeof(List)); head->next=NULL; List*end=head; head->m_Num=0; int choose=0; while(1) { showMenu(); scanf("%d",&choose); switch(choose) { case 1: { end=addList(end,head); system("pause"); system("cls"); } break; case 2: { scanList(head); system("pause"); system("cls"); } break; case 3: break; default: break; } } return 0; }本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报 编辑记录解决 1无用