读书人

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

发布时间: 2012-02-08 19:52:21 作者: rapoo

关于一个删除首结点的问题
#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 print(struct student *);
struct student *dele(struct student *);

//struct student *head; 尽量避免使用全局变量
//struct student *temp,*last;

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

struct student *head=creat();


print(head);
head=dele(head);//当删除第一个节点时,head的位置将改变,因些要更新head
print(head);

}
//-------------------------------------
struct student * creat()
{
int n=1;
struct student *head; //尽量避免使用全局变量
struct student *temp,*last;
//last=temp=(struct student *)malloc(size);
head=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)
while(1)
{
if( temp-> num !=0)// 输入数据有效,则将temp加入链表,否则free temp
{
if(n==1)
last=temp;
else
{
last-> next=temp;
last=last-> next;
}
//输入下一个节点
++n;
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);
}
else
{
if(n==1)
{
free(head);
head=NULL;
}
else
{
free(temp);
last-> next=NULL;
}
break;
}

// temp-> next=NULL;
}
return(head);
}


//------------------------------------
void print(struct student *head)
{
struct student *temp=head;
//do
while(temp != NULL) //先判断当前节点,再访问它的节点数据
{
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;
struct student * temp=head,*befor_temp=head;
if(head==NULL)
{
printf( "错误:链表为空!\n ");
return 0;
}

printf( "请输入要删除的学生学号: ");
scanf( "%d ",&xuehao);
//while(xuehao!=temp-> num&&temp-> next!=NULL)
while(temp!=NULL && xuehao!=temp-> num)//先判断节点,再访问它的域. &&前后表达式的顺序不能颠倒
{
befor_temp=temp;
temp=temp-> next;
}
//if(xuehao==temp-> num)
if(temp != NULL) //还没到未尾就结束循环,说明已找到一个节点的num域等于xuehao,所以不必再判断
{
//if(xuehao==head-> num)
//{
// head=temp-> next;
//}
//else if(xuehao==temp-> num&&temp-> next==NULL)
//{
// last-> next=NULL;
//}
//else
//{
// last-> next=temp-> next;
//}
if(temp==head)
head= head-> next ;
else
befor_temp-> next=temp-> next;

free(temp);
}
else
{
printf( "无此数据!\n ");
}
return(head);
}

读书人网 >C语言

热点推荐