这串代码在CodeBlocks运行无误,到学校的oj平台上就报段错误,不知道错到哪里了,请求指点。
这个题目就是单链表操作的一个集合。
#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
long int num;
float score;
struct student *next;
}Node,*Link;
Link creatlink(void)
{
Link head,node,rear;
long int p;
float q;
head = (Link)malloc(sizeof(Node));
head -> next = NULL;
rear = head;
while(scanf("%ld %f",&p,&q))
{
if(p == 0&&q == 0)
{
break;
}
node = (Link)malloc(sizeof(Node));
node -> num = p;
node -> score = q;
rear -> next = node;
rear = node;
}
rear -> next = NULL;
return head;
}
void printlink(struct student *head)
{
Link p;
p = head -> next;
while(p != NULL)
{
printf("%ld %.2f\n",p -> num,p -> score);
p = p -> next;
}
}
Link dellink(Link head,long int k)
{
if(head == NULL||head -> next == NULL)
exit(0);
Link p,q;
p = head -> next;
q = head;
while(p != NULL)
{
if(p -> num == k)
{
q -> next = p -> next;
free(p);
}
else
{
q = p;
p = p -> next;
}
}
return head;
}
Link insertlink(Link head,Link stu)
{
Link p,node,q;
p = head;
q = p -> next;
while(q != NULL&&q -> num < stu -> num)
{
p = p -> next;
q = p -> next;
}
if(q == NULL)
p -> next = NULL;
node = (Link)malloc(sizeof(Node));
node -> num = stu -> num;
node -> score = stu -> score;
node -> next = p -> next;
p -> next = node;
return head;
}
void freelink(Link head)
{
Link p;
while(p != NULL)
{
p = head;
head = head -> next;
free(p);
}
}
int main()
{
struct student *createlink(void);
struct student *dellink(struct student *, long);
struct student *insertlink(struct student *, struct student *);
void printlink(struct student *);
void freelink(struct student *);
struct student *head, stu;
long del_num;
head= creatlink();
scanf("%ld", &del_num);
head= dellink(head, del_num);
scanf("%ld%f", &stu.num, &stu.score);
head= insertlink(head, &stu);
scanf("%ld%f", &stu.num, &stu.score);
head= insertlink(head, &stu);
printlink(head);
freelink(head);
return 0;
}
|