wowchinamalin 于 2014.09.28 09:07 提问

/*

#include
#include
#include
#define SIZE 10
struct student
{
int num;//链表元素-学号
char name[SIZE];//链表元素-姓名
struct student *next;//链表指针域
};
struct student * creat()
{
int i=0,number;//判断是否为头结点
struct student * head,*p,*q;
printf("请输入学号，按“0”退出\n");
scanf("%d",&number);
while(number!=0)
{
i=i+1;
p = (struct student *)malloc(sizeof(struct student));//动态开辟链表节点
if(p == NULL)//不能正常分配内存
exit(1);
p->num=number;
printf("请输入姓名\n");
scanf("%s",&p->name);
if(i==1)
{
head=p;
}
else
{
q->next=p;
}
q=p;
p->next=NULL;
printf("请输入学号，按“0”退出\n");
scanf("%d",&number);
}
return head;
}
struct student * shuchu(struct student * head)//链表输出函数
{
struct student * p;
p=head;
while(p!=NULL)
{
printf("num:%d name:%s\n",p->num,p->name);
p=p->next;

``````}
``````

}
struct student *shanchu(struct student *head)//删除节点函数
{
struct student * p, * q , *k;
int a;
p=head;
q=head;
printf("请输入你要删除的学生学号：\n");
scanf("%d",&a);
if(a==p->num&&p!=NULL)
{
k=head;//把头结点给K
head=head->next;//头结点后移一位
printf("head->num=%d\n",head->num);//输出头结点的num用以测试
free(k);//释放k结点
printf("111111111111111\n");
}
else
{

``````    while(q!=NULL)
{
if(q->num!=a)//如果输入学号与节点学号不同则判断下一个
{
p=q;
q=q->next;
}
else//如果相同则删除
{
k=q;
q=q->next;
p->next=q;
free(k);
}
}printf("22222222222222\n");
}
return head;
};
``````

int main()
{
struct student * p;
p=creat();printf("22222222222222\n");
shuchu(p);
//shuchu(p);
p=shanchu(p);
shuchu(p);
}

1个回答

u013648963   2014.09.28 20:48