#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就好了,这样就不会丢失了
#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;
}
看了好一会儿才看出你的问题
你的代码们有主要问题是这里,每一次重新插入后,其实要让end执向最后得节点得,但是,其实函数传参是拷贝传参,按地址传参得,你要用传参得方式使每次对外循环时得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");
}
void addList(List** end, List* head)
{
List *temp = (List*)malloc(sizeof(List));
if (temp == NULL) return;
temp->m_Num = 0;
temp->next = NULL;
//end->next = (List*)malloc(sizeof(List));
//end = end->next;
//if (end == NULL) return;
printf("请输入该节点编号:");
scanf_s("%d", &temp->m_Num);
//scanf_s("%d", &end->m_Num);
(*end)->next = temp;
(*end) = temp;
//end->next = NULL;
(head->m_Num)++;
printf("成功添加节点 \n");
}
void scanList(List* head)
{
if (head->m_Num == 0)
printf("当前链表没有节点");
else
{
List* tmp = head;
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));
if (head == NULL) return 1;
head->next = NULL;
List* end = head;
head->m_Num = 0;
int choose = 0;
while (1)
{
showMenu();
scanf_s("%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;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!