新手求助,想请问一个结构体的链表问题,望解答
本帖最后由 光着屁股的犀牛 于 2020-4-24 16:28 编辑想请问一个结构体的链表的小问题,望解答
为啥我这个新建的结构体变量会报错
local variable 'ne' used without having been initialized
未初始化就使用了局部变量“ne”
问题出错在第 92,92,92 行,代码可能有点长,辛苦,辛苦,只是一个新建变量的问题出错,应该是个小问题,但是自己解决不了,望求助;
https://imgchr.com/i/JDeVHA
#include <stdio.h>
#include <stdlib.h>
#define len sizeof(struct student)
int n = 0;
struct student
{
int num;
char name;
int score;
struct student *next;
};
struct student *head;
int main ()
{
struct student *creat(void);
void print(struct student *a);
struct student *delet(struct student *a);
struct student *inc(struct student *a);
int n;
creat();
print(head);
printf ("what do you want to do:\ndelete enter :1\tincreat enter :2");
scanf ("%d",&n);
if(n == 1)
{
print(delet(head));
}
else if(n == 2)
{
print(inc(head));
}
}
struct student *creat(void)
{
struct student *p1,*p2;
p1 = p2 = (struct student *)malloc(len);
printf ("please enter the student's number:");
scanf ("%d",&p1->num);
printf ("please enter the student's name:");
scanf ("%s",p1->name);
printf ("please enter the student's score :");
scanf ("%d",&p1->score);
printf ("\n");
head = NULL;
while (p1->num)
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(len);
printf ("please enter the student's number:");
scanf ("%d",&p1->num);
printf ("please enter the student's name:");
scanf ("%s",p1->name);
printf ("please enter the student's score :");
scanf ("%d",&p1->score);
printf ("\n");
}
p2->next =NULL;
return head;
}
void print(struct student *a)
{
struct student *p;
p = a;
while (p)
{
printf("\nnumber:%d\tname:%s\tscore:%d\t",p->num,p->name,p->score);
p = p->next;
}
}
struct student *inc(struct student *a)
{
struct student *p1,*p0,*p2;
struct student *ne;
int loc;
printf ("enter the number you want to increase:\n");
printf ("please enter the student's number:");
scanf ("%d",&ne->num); //问题出处!问题出处!问题出处!问题出处!问题出处!
printf ("please enter the student's name:");
scanf ("%s",ne->name);
printf ("please enter the student's score :");
scanf ("%d",&ne->score);
printf ("\n");
printf ("\nplease enter the location you want to add:");
scanf ("%d",&loc);
p1 = a;
p0 = ne;
if (a == NULL)
{
return ne;
}
else
{
while (p0->num>p1->num && p1->next!= NULL)
{
p2 = p1;
p1 = p1->next;
}
if (p0->num<=p1->num)
{
if(p1 == head)
{
head = p0;
p0->next = p1;
}
else
{
p2->next = p0;
p0->next = p1;
}
}
else
{
p1->next = p0;
p0->next = NULL;
}
}
n = n+1;
return a;
}
struct student *delet(struct student *a)
{
struct student *p1,*p2;
int num;
printf ("输入你要找的数:");
scanf ("%d",&num);
if (a == NULL)
{
printf ("空表");
goto END;
}
p1 = a;
while (num != p1->num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (num == p1->num)
{
if(p1 == head)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
}
else
{
printf("找不到");
}
printf("成功");
n--;
END:
return a;
}
希望有高手能帮忙指点一下,谢谢 你只是定义了一个该结构体的指针啊。
指针没有指向,当然会报错说没有初始化啦!
将该指针指向一个该结构体的对象,这叫指针的初始化。
指针不进行初始化,是不能用的 sunrise085 发表于 2020-4-24 16:53
你只是定义了一个该结构体的指针啊。
指针没有指向,当然会报错说没有初始化啦!
将该指针指向一个该结构 ...
感谢感谢,万分感谢,还是自己基础不扎实{:10_266:}
页:
[1]