增加一个链表节点的问题
#include<stdio.h>#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct student)//结构的大小
struct student *creat(); //创建结构
void print(struct student *head);//结构名称
struct student *del(struct student *head, int num);//del用于删除节点,*head即是链表的头指针,num是要删除节点的num.
struct student *insert(struct student *head ,struct student *stu_2);//第一个参数要被插入的链表。
//第二个参数带插入的结构的地址
struct student //定义结构体
{
int num;
float score;
struct student *next;
};
void main()
{
struct student *stu,*p,*stu_2;
int n;
stu=creat();
p = stu;
print(p);
printf("Please enter the num to delete:\n");
scanf("%d",&n);
print(del(p,n));
printf("\nPlease enter the num to insert:\n");
scanf("%d",&stu_2.num);
printf("\nplease enter the score:");
scanf("%f",&stu_2.score);
p = insert(stu,stu_2);
print(p);
printf("\n\n");
system("pause");
}
int n;//全局变量
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
p1=p2=(struct student *)malloc(LEN);
printf("please enter the num:\n");
scanf("%d",&p1->num);
printf("please enter the score:\n");
scanf("%f",&p1->score);
head =NULL;
n=0;
while(p1->num)
{
n++;
if(n==1)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1 = (struct student *)malloc(LEN);
printf("please enter the num:\n");
scanf("%d",&p1->num);
printf("please enter the score:\n");
scanf("%f",&p1->score);
}
p2 -> next=NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
printf("there ate %d recods:\n",n);
p = head;
if(head!=0)
{
do
{
printf("学好为%d的同学的成绩为%0.2f.\n",p -> num , p -> score);
p = p -> next;
}while(p != 0);
}
}
struct student *del(struct student *head, int num)
{
struct student *p1,*p2;
if(NULL==head)
{
printf("\nthis is a null!\n");
goto END;
}
p1=head;
while(p1 -> num != num && p1 -> next != NULL)
{
p2 = p1;
p1 = p1 ->next;
}
if(p1 ->num =num)
{
if(p1 == head)
{
head = p1 -> next;
}
else
{
p2 -> next = p1 -> next;
}
printf("\nDelete No: %d succed!\n",num);
n=n-1;
}
else
{
printf("\n%d not found succed!\n",num);
}
END:
return head;
}
struct student *insert(struct student *head ,struct student *stu_2)
{
struct student *p0,*p1,*p2;
p1 = head;
p0 =stu_2;
if(NULL == head)
{
head = p0;
p0 -> next = NULL;
}
else
{
while( (p0 -> num > p1 -> num )&&(p1 -> next != NULL) )//p0的值最小,插入头部
{
p2 = p1;
p1 = p1 -> next;
}
if(p0 -> num < p1 -> num)//插入中间
{
if(p1==head)
{
head = p0;
}
else
{
p2 -> next = p0;
}
p0 -> next = p1;
}
else //p0的num 最大,插入到末尾
{
p1 -> next = p0;
p0 -> next = NULL;
}
}
n=n+1;
return head;
}
程序运行时老是报这个错误是怎末回事啊,感觉没有错误啊,求大神指点。
这个位置:
void main()
{
struct student *stu,*p,*stu_2;
int n;
stu=creat();
p = stu;
print(p);
printf("Please enter the num to delete:\n");
scanf("%d",&n);
print(del(p,n));
printf("\nPlease enter the num to insert:\n");
scanf("%d",&stu_2.num);
printf("\nplease enter the score:");
scanf("%f",&stu_2.score);
p = insert(stu,stu_2);
print(p);
printf("\n\n");
system("pause");
}
C:\XSL\c6.0.zuoye\20200412\zizuo(2).c(34) : error C2231: '.num' : left operand points to 'struct', use '->'
C:\XSL\c6.0.zuoye\20200412\zizuo(2).c(36) : error C2231: '.score' : left operand points to 'struct', use '->' 这不是说的很清楚么?
num和score左侧的点应该用箭头
stu_2.score应该是stu_2->score
stu_2.num应该是stu_2->num 运行之后会报这个错误:
C:\XSL\c6.0.zuoye\20200412\zizuo(2).c(34) : warning C4700: local variable 'stu_2' used without having been initialized a1764441928 发表于 2020-4-15 21:09
运行之后会报这个错误:
C:\XSL\c6.0.zuoye\20200412\zizuo(2).c(34) : warning C4700: local variable 's ...
指针stu_2没有初始化,应该要开辟一个空间给stu_2,stu_2=(student*)malloc(sizeof(stu))
malloc(sizeof(student)),*p2=NULL
页:
[1]