主题:关于单链表的一个问题,急急急急。。。
[code=c]
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct student
{
float score;
int num;
struct student *next;
};
/****************************创建一个单链表*********************************/
struct student *creat(void)
{
struct student *head;
struct student *p1, *p2;
int n = 0;
p1 = p2 = (struct student *)malloc(sizeof(struct student));//创建第一个节点
head = NULL;
scanf("%f %d",&p1->score,&p1->num);
while(p1->num != 0)
{
n = n+1;
if(n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct student *)malloc(sizeof(struct student));
scanf("%f %d",&p1->score,&p1->num);
}
p2->next = NULL;
return (head);
}
/*********************************显示创建的单链表***********************/
void show_linklist(struct student *p)
{
while(p !=NULL)
{
printf("%f %d\n",p->score,p->num);
p = p->next;
}
}
/**********************************删除单链表中的一个特定节点****************************/
void delete_node(struct student *head,int num)
{
struct student *p1, *p2;
if(head = NULL)
{
printf("链表是空的!\n");
exit(-1);
}
else
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("删除成功!您删除的元素是 %d",num);
}
else
printf("找不到您要删除的节点!\n");
// return head;
}
/*************************主函数********************************/
int main(void)
{
struct student *head;
head = creat();
delete_node(head,3);
show_linklist(head);
return 0;
}
[/code]
此程序时关于单链表的,程序编译后无错,但是输出的结果是“此内存不能为“read””,望哪位大侠指点!多谢
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct student
{
float score;
int num;
struct student *next;
};
/****************************创建一个单链表*********************************/
struct student *creat(void)
{
struct student *head;
struct student *p1, *p2;
int n = 0;
p1 = p2 = (struct student *)malloc(sizeof(struct student));//创建第一个节点
head = NULL;
scanf("%f %d",&p1->score,&p1->num);
while(p1->num != 0)
{
n = n+1;
if(n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct student *)malloc(sizeof(struct student));
scanf("%f %d",&p1->score,&p1->num);
}
p2->next = NULL;
return (head);
}
/*********************************显示创建的单链表***********************/
void show_linklist(struct student *p)
{
while(p !=NULL)
{
printf("%f %d\n",p->score,p->num);
p = p->next;
}
}
/**********************************删除单链表中的一个特定节点****************************/
void delete_node(struct student *head,int num)
{
struct student *p1, *p2;
if(head = NULL)
{
printf("链表是空的!\n");
exit(-1);
}
else
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("删除成功!您删除的元素是 %d",num);
}
else
printf("找不到您要删除的节点!\n");
// return head;
}
/*************************主函数********************************/
int main(void)
{
struct student *head;
head = creat();
delete_node(head,3);
show_linklist(head);
return 0;
}
[/code]
此程序时关于单链表的,程序编译后无错,但是输出的结果是“此内存不能为“read””,望哪位大侠指点!多谢