YTU 2430: C语言习题 链表建立,插入,删除,输出
2430: C语言习题 链表建立,插入,删除,输出
时间限制: 1 Sec 内存限制: 128 MB
提交: 576 解决: 280
题目描述
编写一个函数creatlink,用来建立一个动态链表。(包含学号和成绩)
编写一个函数printlink,用来输出一个链表。
编写一个函数dellink,用来删除动态链表中一个指定的结点(由实参指定某一学号,表示要删除该学生结点)。
编写一个函数insertlink,用来向动态链表插入一个结点。
编写一个函数freelink,用来释放一个动态链表。
输入
输入多个学生的学号和成绩,建立动态链表,以0 0 结束
输入学号,删除链表中的对应结点
插入两个链表结点
输出
输出的链表
样例输入
1001 100
1002 95
1005 90
1008 76
0 0
1005
1006 98
1009 99
样例输出
1001 100.00
1002 95.00
1006 98.00
1008 76.00
1009 99.00
提示
/* C代码 */
int main()
{
struct student *creatlink(void);
struct student *dellink(struct student *,long);
struct student *insertlink(struct student *,struct student *);
void printlink(struct student *);
void freelink(struct student *);
struct student *head,stu;
long del_num;
head=creatlink();
scanf("%ld",&del_num);
head=dellink(head,del_num);
scanf("%ld%f",&stu.num,&stu.score);
head=insertlink(head,&stu);
scanf("%ld%f",&stu.num,&stu.score);
head=insertlink(head,&stu);
printlink(head);
freelink(head);
return 0;
}
/* C++代码 */
int main()
{
student *creatlink(void);
student *dellink(student *,long);
student *insertlink(student *,student *);
void printlink(student *);
void freelink(student *);
student *head,stu;
long del_num;
head=creatlink();
cin>>del_num;
head=dellink(head,del_num);
cin>>stu.num>>stu.score;
head=insertlink(head,&stu);
cin>>stu.num>>stu.score;
head=insertlink(head,&stu);
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
printlink(head);
freelink(head);
return 0;
}
迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<iomanip>
using namespace std;
struct student
{
long int num;
float score;
struct student *next;
};
struct student *creatlink()
{
struct student *p1,*p2,*head=NULL;
p1=p2=(struct student*)malloc(sizeof(struct student));
while(~scanf("%ld%f",&p1->num,&p1->score)&&(p1->score||p1->num))
{
if(head==NULL)head=p1;
else p2->next=p1;
p2=p1;
p1=p1->next=(struct student*)malloc(sizeof(struct student));
}
p2->next=NULL;
return head;
};
struct student *dellink(struct student *a,long b)
{
struct student *head=a,*p=a,*p2=a;
while(p!=NULL)
{
if(p->num==b)
p2->next=p->next;
p2=p;
p=p->next;
}
return head;
};
struct student *insertlink(struct student *a,struct student *b)
{
struct student *head=a,*p=a,*p2=a,*k;
k=(struct student*)malloc(sizeof(struct student));
k->num=b->num,k->score=b->score;
int n=0;
while(p!=NULL)
{
if(p->num>b->num)
{
p2->next=k;
k->next=p;
n=1;
}
p2=p;
p=p->next;
}
if(n==0)p2->next=k,k->next=NULL;
return head;
};
void printlink(struct student *a)
{
struct student *p=a;
while(p!=NULL)
{
printf("%ld %.2f\n",p->num,p->score);
p=p->next;
}
}
void freelink(struct student *a)
{
while(a!=NULL)
{
delete(a);
a=a->next;
}
}
int main()
{
student *creatlink(void);
student *dellink(student *,long);
student *insertlink(student *,student *);
void printlink(student *);
void freelink(student *);
student *head,stu;
long del_num;
head=creatlink();
cin>>del_num;
head=dellink(head,del_num);
cin>>stu.num>>stu.score;
head=insertlink(head,&stu);
cin>>stu.num>>stu.score;
head=insertlink(head,&stu);
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
printlink(head);
freelink(head);
return 0;
}
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<iomanip>
using namespace std;
struct student
{
long int num;
float score;
struct student *next;
};
struct student *creatlink()
{
struct student *p1,*p2,*head=NULL;
p1=p2=(struct student*)malloc(sizeof(struct student));
while(~scanf("%ld%f",&p1->num,&p1->score)&&(p1->score||p1->num))
{
if(head==NULL)head=p1;
else p2->next=p1;
p2=p1;
p1=p1->next=(struct student*)malloc(sizeof(struct student));
}
p2->next=NULL;
return head;
};
struct student *dellink(struct student *a,long b)
{
struct student *head=a,*p=a,*p2=a;
while(p!=NULL)
{
if(p->num==b)
p2->next=p->next;
p2=p;
p=p->next;
}
return head;
};
struct student *insertlink(struct student *a,struct student *b)
{
struct student *head=a,*p=a,*p2=a,*k;
k=(struct student*)malloc(sizeof(struct student));
k->num=b->num,k->score=b->score;
int n=0;
while(p!=NULL)
{
if(p->num>b->num)
{
p2->next=k;
k->next=p;
n=1;
}
p2=p;
p=p->next;
}
if(n==0)p2->next=k,k->next=NULL;
return head;
};
void printlink(struct student *a)
{
struct student *p=a;
while(p!=NULL)
{
printf("%ld %.2f\n",p->num,p->score);
p=p->next;
}
}
void freelink(struct student *a)
{
while(a!=NULL)
{
delete(a);
a=a->next;
}
}
int main()
{
student *creatlink(void);
student *dellink(student *,long);
student *insertlink(student *,student *);
void printlink(student *);
void freelink(student *);
student *head,stu;
long del_num;
head=creatlink();
cin>>del_num;
head=dellink(head,del_num);
cin>>stu.num>>stu.score;
head=insertlink(head,&stu);
cin>>stu.num>>stu.score;
head=insertlink(head,&stu);
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
printlink(head);
freelink(head);
return 0;
}
YTU 2430: C语言习题 链表建立,插入,删除,输出的更多相关文章
- C语言习题 链表建立,插入,删除,输出
Problem B: C语言习题 链表建立,插入,删除,输出 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 222 Solved: 92 [Subm ...
- YTU 2429: C语言习题 学生成绩输入和输出
2429: C语言习题 学生成绩输入和输出 时间限制: 1 Sec 内存限制: 128 MB 提交: 1897 解决: 812 题目描述 编写一个函数print,打印一个学生的成绩数组,该数组中有 ...
- Problem A: C语言习题 链表建立,插入,删除,输出
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct student { l ...
- Problem X: C语言习题 学生成绩输入和输出
Problem X: C语言习题 学生成绩输入和输出 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 4722 Solved: 2284[Submit] ...
- 20140502 static_cast和dynamic_cast的类型检查 双链表建立,删除,打印
1.static_cast和dynamic_cast的类型检查 static_cast的类型检查:只检查无关类之间的转换 CBaseY* pY1 = static_cast<CBaseY*> ...
- YTU 2414: C语言习题 字符串排序
2414: C语言习题 字符串排序 时间限制: 1 Sec 内存限制: 128 MB 提交: 656 解决: 305 题目描述 输入n个字符串,将它们按字母由小到大的顺序排列并输出.编写三个函数实 ...
- YTU 2974: C语言习题5.26--文件操作3
2974: C语言习题5.26--文件操作3 时间限制: 1 Sec 内存限制: 128 MB 提交: 213 解决: 92 题目描述 文本文件score.dic 中存储了n名学生的信息(班级编号 ...
- YTU 2973: C语言习题5.25--文件操作2
2973: C语言习题5.25--文件操作2 时间限制: 1 Sec 内存限制: 128 MB 提交: 242 解决: 105 题目描述 文本文件score.dic 中存储了n名学生的信息(班级编 ...
- YTU 2972: C语言习题5.24--文件操作1
2972: C语言习题5.24--文件操作1 时间限制: 1 Sec 内存限制: 128 MB 提交: 248 解决: 94 题目描述 文本文件score.dic 中存储了n名学生的信息(班级编号 ...
随机推荐
- Mongodb 断电或者强制关机之后
Mongodb相信大家都比较熟悉了,将它注册为服务什么的就不说了,网上到处都是.在公司用的过程中,我发现在意外断电,或者强制关机之后,启动服务时候就会报错,找了很久,试了很多种方法,才发现,它有个自带 ...
- mongodb 的创建和使用
1. sudo apt-get install mongodb 2. 登陆数据库: mongo, 3. 创建数据库:use dbname 4. 插入数据: db.dbname.insert({&quo ...
- Monkey King(左偏树)
洛谷传送门 每次给出要争吵的猴子a和b,用并查集判断如果他们是朋友输出-1 如果不是,找出a,b在的堆的根A,B,分别合并A,B的左右孩子,再合并一下. 之后把A,B的数据更改一下:权值除以2,左右孩 ...
- Uva10294 Arif in Dhaka (置换问题)
扯回正题,此题需要知道的是置换群的概念,这点在刘汝佳的书中写的比较详细,此处不多做赘述.此处多说一句的是第二种手镯的情况.在下图中“左图顺时针转1个位置”和“右图顺时针旋转5个位置”是相同的,所以在最 ...
- Request获取Session的两种方式
1.无请求参数 public HttpSession getSession() 获取当前request关联的session,如果当前request没有session,创建一个session. 2.有请 ...
- 介绍 JSON的
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...
- 原生js中stopPropagation,preventDefault,return false的区别
1.stopPropagation:阻止事件的冒泡,但不阻止事件的默认行为. 最好莫过于用例子说明: <div id='div' onclick='alert("div") ...
- linux命名详解及其软件安装实例
始于cd,ls命令 好啦,步入正题,我使用的linux连接工具为xshell,mRemoteNG,对两款工具不做介绍啦,你可以百度一下,实在不会入左上方群. 进入之后,便是上面的界面黑乎乎一片,对于初 ...
- windows安装ZIP压缩版的Weblogic Server
以前要装Weblogic Server的时候都是装的安装版,最近发现ZIP版本的Weblogic Server是一个只包含Weblogic Server的版本,于是就想着弄一下它. 这里用到的Webl ...
- hdu1042 (模拟n!)
题目大意: 求 n.(可能会超过整数范围,这里用数组模拟n!的值) pid=1042">http://acm.hdu.edu.cn/showproblem.php?pid=1042 A ...