问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
#include<stdio.h>
#include<stdlib.h>
typedef struct listnode{
int date;
struct listnode* next;
}list;
int main()
{
struct listnode* p;
int x;
int a;
int b;
int count;
list node1 = { 10,NULL };
list node2 = { 20,NULL };
list node3 = { 30,NULL };
list node4 = { 40,NULL };
node1.next = &node2;
node2.next = &node3;
node3.next = &node4;
p = &node1;
while (p != NULL)
{
printf("%d\n", p->date);
p = p->next;
}
printf("请输入需要查找的数");
scanf_s("%d", &x);
p = &node1;
while (p != NULL)
{
if (x == p->date)
{
printf(" 找到啦 %d\n", p->date);
}
p = p->next;
}
printf("请增加一个数\n");
scanf_s("%d", &a);
printf("增加到第几个数后面\n");
scanf_s("%d", &b);
p = &node1;
while (p != NULL && b <= 4)
{
count = 0;
if (p != NULL&&count<=b)
{
count++;
p = p->next;
}
list* h;
h = (list *) malloc(sizeof(list));
h->date = a;
h->next = p->next;
p->next = h;
}
while (p != NULL)
{
printf("%d\n", p->date);
p = p->next;
}
}