新手请教关于链表的问题, 编译,连接OK,运行出错
#include<stdio.h>#include<stdlib.h>
#include<malloc.h>
typedef struct Nobe
{
long num;
float score;
struct Nobe * pNext;
} Nb, *Pnb;
#define LEN sizeof (struct Nobe)
int n ;//全局变量
//函数声明
Pnb create ();
void delet (Pnb * head);
void insert (Pnb * head);
void search(Pnb head);
void show (Pnb head);
//主函数
int main()
{
Pnb head = NULL;
int i;
do
{
printf("\n");
printf("\n1------创建链表(create)\n");
printf("\n2------删除(delete)\n");
printf("\n3------插入(insert)\n");
printf("\n4------查找(search)\n");
printf("\n5------显示(show)\n");
printf("\n6------退出(exit)\n");
scanf("%d", &i);
switch(i)
{
case 1: head = create();
break;
case 2: delet( &head);
break;
case 3: insert( &head);
break;
case 4: search( head);
break;
case 5: show( head);
break;
case 6: break;
default:printf("输入错误,请重新选择!\n");
break;
}
}
while (i != 6);
return 0;
}
//创建函数
/*
Pnb create()
{
printf("请输入学生信息(以空格分隔):\n");
Pnb head, p1, p2;
//int n ;
n = 0;
p1 = p2 = (Pnb) malloc (LEN);
if(p1 == NULL)
{
printf("分配失败!程序中止!\n");
exit (-1);
}
scanf("%ld %f", &p1->num, &p1->score);
head = NULL;
while(p1->num != 0)
{
n += 1;
if(n == 1)
head = p1;
else
{
p2->pNext = p1;
p2 = p1;
p1 = (Pnb)malloc(LEN);
if(p1 == NULL)
{
printf("分配失败!程序中止!\n");
exit (-1);
}
scanf("%ld %f", &p1->num, &p1->score);
}
}
p2->pNext = NULL;
return head;
}
*/
//创建函数
Pnb create()
{
Pnb head = NULL;
head = (Pnb) malloc (LEN);//设置一个空首节点
if(head == NULL)
{
printf("分配失败!程序中止!\n");
exit (-1);
}
Pnb ptail = head;
ptail->pNext = NULL;
int i;
printf("请输入要生成链表节点的个数:n=");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("请输入第%d个学生的数据:", i+1);
Pnb p = (Pnb)malloc (LEN);
if(p == NULL)
{
printf("分配失败!程序中止!\n");
exit (-1);
}
scanf("%ld %f",&p->num, &p->score);
ptail->pNext = p;
p->pNext = NULL;
ptail = p;
}
return head;
}
//删除函数
void delet(Pnb * head)
{
long del_num;
Pnb p1 , p2;
if(*head == NULL)
{
printf("空链表!\n");
return ;
}
printf("请输入要删除的学生学号:");
scanf("%ld", &del_num);
p1 = *head;
while((p1->num != del_num) && p1->pNext != NULL)//查找
{
p2 = p1;
p1 = p1->pNext;//后移一位
}
if(p1->num = del_num)//找到了
{
if(p1 == *head)//在首节点
*head = p1->pNext;
else
p2 ->pNext = p1->pNext;//在中间
printf("删除了%ld信息!\n", del_num);
n -= 1;
// free(p1);
}
else
printf("找不到要删除的该信息!\n");
return ;
}
//插入函数
void insert(Pnb * head)
{
Pnb p1, p2;
p1 = * head;
Pnb stu_in = (Pnb) malloc (LEN);
if(stu_in == NULL)
{
printf("分配失败!程序中止!\n");
exit (-1);
}
printf("请输入要插入的学生的信息(以空格分隔):\n");
scanf("%ld %f", &stu_in->num, &stu_in->score);
if(p1 == NULL)//空链表
{
*head = stu_in;
stu_in->pNext = NULL;
}
else
{
while((stu_in->num > p1->num ) && (p1->pNext != NULL))//查找要插入的节点位置
{
p2 = p1; //后移一节点
p1 = p1->pNext;
}
if(stu_in->num <= p1->num)//找到要插入的节点位置
{
if(*head ==p1) //插入到第一个节点之前
{*head = stu_in;
stu_in->pNext = p1;//????//???本句可不要?
}
else
{p2->pNext = stu_in; //插入到中间位置,即是p2所指向的节点之后
stu_in->pNext = p1;
}
}
else
{p1->pNext = stu_in;
stu_in->pNext = NULL;}//插入到最后(表尾)
}
n += 1;
return ;
}
//显示函数
void show(Pnb head)
{
Pnb p = head->pNext;
printf("有如下%d种信息:\n", n);
while(head != NULL)
{
printf("%ld %5.2f\n",p->num, p->score);
p = p->pNext;
}
return ;
}
//查找函数
void search(Pnb head)
{
Pnb p, p1;
p = head;
long num_s;
printf("\n请输入要查找的学生学号:");
scanf("%ld", &num_s);
if(head == NULL)
{
printf("空链表!\n");
return ;
}
while ((num_s != p->num) && (p->pNext != NULL))
{
p1 = p;
p = p->pNext;
}
if(num_s == p->num)
printf("\n%ld %5.2f\n",p->num, p->score);
else
printf("在链表中查不到找该学生的信息!\n");
return ;
}
程序编译、连接在我的机器上都没有问题,我用的是VC6。0++
但是在运行是出问题
如下图
请大神看看是哪里出了问题?
小弟感激不尽!小弟找了好久都找不出问题,5555.。。