![](https://i-blog.csdnimg.cn/blog_migrate/58a65e242fac1606c51c4ee09b5c6a0d.png)
题目
![](https://i-blog.csdnimg.cn/blog_migrate/2b63955876ff90c272c9ce7db02b0231.jpeg)
解决代码及点评
/*
功能:建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,
将此结点删除,输出最后的链表。
时间:17:00 2013/10/25
*/
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
struct student69
{
int ID;
char name[80];
int sex;
int age;
struct student69 *pNext;
};
typedef struct student69 ST;
ST *initList69() //初始化链表!!!
{
ST *pHead=(ST *)malloc(sizeof(ST));
pHead->pNext=NULL;
return pHead;
}
void insertSt(ST *pHead,int id,int sex,int age)
{
while (pHead->pNext!=NULL) //后继不为空,则指针向后移,直到最后一个结点
{
pHead=pHead->pNext;
}
ST *p=(ST *)malloc(sizeof(ST)); //创建新节点
if(p==NULL)return; //结点是否创建成功
pHead->pNext=p; //最后一个结点指向新节点
p->ID=id;
p->age=age;
p->sex=sex;
char a[80]={'\0'};
puts("Please enter the name: ");
scanf_s("%s",a);
strcpy_s(p->name,a);
p->pNext=NULL; //新结点后继赋空
}
void showStudent69(ST *pHead) //输出全部信息
{
ST *p=pHead->pNext;
do
{
printf("ID: %d\t",p->ID);
printf("Name %s\t",p->name);
printf("age %d\t",p->age);
printf("sex %d\n",p->sex);
} while ((p->pNext != NULL)&&(p=p->pNext));
}
void deleteSt(ST *pHead,int theAge)
{
ST *p=pHead->pNext;
ST *prior=pHead; //保存前驱
while(p->age!=theAge&&p->pNext!=NULL) //不满足条件往后走
{
prior=p;
p=p->pNext;
}
if(p->age==theAge) //找到条件 将后继赋给前驱
{
prior->pNext=p->pNext;
}
free(p); //释放结点
if(p->age!=theAge)return;
}
void main()
{
ST *pHead=initList69();
insertSt(pHead,1,1,18);
insertSt(pHead,2,0,19);
insertSt(pHead,3,1,30);
showStudent69(pHead);
int theAge;
scanf_s("%d",&theAge);
deleteSt(pHead,theAge);
showStudent69(pHead);
system("pause");
}
代码编译以及运行
由于资源上传太多,资源频道经常被锁定无法上传资源,同学们可以打开VS2013自己创建工程,步骤如下:
1)新建工程
2)选择工程
3)创建完工程如下图:
4)增加文件,右键点击项目
5)在弹出菜单里做以下选择
6)添加文件
7)拷贝代码与运行
程序运行结果
代码下载
http://download.csdn.net/detail/yincheng01/6681845
解压密码:c.itcast.cn