首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于一个删除首结点的有关问题

2012-03-30 
关于一个删除首结点的问题#include stdio.h#include stdlib.h#definesizesizeof(structstudent)struct

关于一个删除首结点的问题
#include <stdio.h>
#include <stdlib.h>
#define   size   sizeof(struct   student)
struct   student
{
int   num;
char   name[20];
int   score;
struct   student   *next;
};
struct   student   *   creat();
void   print(struct   student   *);
struct   student   *dele(struct   student   *);

struct   student   *head;
struct   student   *temp,*last;

//--------------------------------
void   main()
{

head=creat();
print(head);
dele(head);
print(head);

}
//-------------------------------------
struct   student   *   creat()
{
int   n=1;
last=temp=(struct   student   *)malloc(size);
head=NULL;
printf( "请输入第%d个学生的学号: ",n);
scanf( "%d ",&temp-> num);
printf( "请输入第%d个学生的姓名: ",n);
scanf( "%s ",temp-> name);
printf( "请输入第%d个学生的分数: ",n);
scanf( "%d ",&temp-> score);

while(temp-> num!=0)
{
if(n==1)
{
head=temp;
n++;
}
else
{
temp=(struct   student   *)malloc(size);
printf( "请输入第%d个学生的学号: ",n);
scanf( "%d ",&temp-> num);
printf( "请输入第%d个学生的姓名: ",n);
scanf( "%s ",temp-> name);
printf( "请输入第%d个学生的分数: ",n);
scanf( "%d ",&temp-> score);
last-> next=temp;
last=temp;
n++;
}
temp-> next=NULL;
}
return(head);
}


//------------------------------------
void   print(struct   student   *head)
{
temp=head;
do
{
printf( "%d,%s,%d ",temp-> num,temp-> name,temp-> score);
temp=temp-> next;
putchar(10);
}while(temp-> next!=NULL);
}
//--------------------------------
struct   student   *   dele(struct   student   *head)
{
int   xuehao;
printf( "请输入要删除的学生学号: ");
scanf( "%d ",&xuehao);
temp=head;
if(head==NULL)
{
printf( "错误!\n ");
}

while(xuehao!=temp-> num&&temp-> next!=NULL)
{
last=temp;
temp=temp-> next;
}
if(xuehao==temp-> num)
{
if(xuehao==head-> num)
{
head=temp-> next;
}
else   if(xuehao==temp-> num&&temp-> next==NULL)
{
last-> next=NULL;
}
else
{
last-> next=temp-> next;
}
}
else
{
printf( "无此数据!\n ");
}
return(head);
}

我输入
1   jack   89
2   tom   65
3   may   98
然后我输入删除学号1,结果没有删掉,删除其他是正确的
请问我错在哪里   谢谢

[解决办法]
#include <stdio.h>
#include <stdlib.h>
#define size sizeof(struct student)

struct student
{
int num;
char name[20];
int score;
struct student *next;
};

//struct student * creat();
void creat();

//void print(struct student *);
void print();

//struct student *dele(struct student *);
void dele();

struct student *head;
struct student *temp,*last;

//--------------------------------


void main()
{

//head=creat();
creat();
print(head);
dele(head);
print(head);
temp=head;
while(temp-> next!=NULL)
{
head=temp-> next;
free(temp);
temp=head;
}
free(temp);
printf( "回车退出 ");
getchar();

}

//-------------------------------------
//struct student * creat();
void creat()
{
int n=1;
last=temp=(struct student *)malloc(size);
head=NULL;
printf( "请输入第%d个学生的学号: ",n);
scanf( "%d ",&temp-> num);
printf( "请输入第%d个学生的姓名: ",n);
scanf( "%s ",temp-> name);
printf( "请输入第%d个学生的分数: ",n);
scanf( "%d ",&temp-> score);

if(temp-> num==0)
{
char st[10];
gets(st);
printf( "无数据输入,回车键退出\n ");
getchar();
free(temp);
exit(0);
}
while(temp-> num!=0)
{
if(n==1)
{
head=temp;
n++;
}
else
{
temp=(struct student *)malloc(size);
printf( "请输入第%d个学生的学号: ",n);
scanf( "%d ",&temp-> num);
printf( "请输入第%d个学生的姓名: ",n);
scanf( "%s ",temp-> name);
printf( "请输入第%d个学生的分数: ",n);
scanf( "%d ",&temp-> score);
last-> next=temp;
last=temp;
n++;
}
temp-> next=NULL;
}
//return(head);
}

//------------------------------------
//void print(struct student *head);
void print()
{
printf( "here\n ");
temp=head;
while(temp-> next!=NULL)//;do
{
printf( "%d,%s,%d ",temp-> num,temp-> name,temp-> score);
temp=temp-> next;
putchar(10);
}
//printf( "%ld\n ",&temp-> next);
}
//--------------------------------

//struct student *dele(struct student *head)
void dele()
{
int xuehao;
printf( "请输入要删除的学生学号: ");
scanf( "%d ",&xuehao);
printf( "%d\n ",xuehao);
temp=head;
/*if(head==NULL)
{
printf( "错误!\n ");
}*/

while(xuehao!=temp-> num&&temp-> next!=NULL)
{
last=temp;
temp=temp-> next;
}
if(xuehao==temp-> num)
{
if(xuehao==head-> num)
{
head=temp-> next;
free(temp);
}
else if(xuehao==temp-> num&&temp-> next==NULL)
{
last-> next=NULL;
free(temp);
}
else
{
last-> next=temp-> next;
free(temp);
}
}
else
{
printf( "无此数据!\n ");
}
//return(head);
}
//1,head, temp, last 都是全局变量不需要值传递,所传递的值如果没有接收可能并不能改变原值。
//2,
/*if(head==NULL)
{
printf( "错误!\n ");
}*/
//这一段代码没用如果head==NULL的话根本执行不到这里就会出错。
//所以我加了一下一段在creat()里
/*if(temp-> num==0)
{
char st[10];
gets(st);
printf( "无数据输入,回车键退出\n ");
getchar();
free(temp);
exit(0);
}*/
//3,malloc(size)创建的内存需要free(); 我加在了main里 不知道有用没有,这一对函数我不太懂。

热点排行