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名学生的信息(班级编号 ...
随机推荐
- Java学习关于随机数工具类--Random类
Random类是伪随机数生成器.之所以称为伪随机数(pseudorandom),是因为它们只是简单的均匀分布序列.Random类定义了以下构造函数: Random() Random(long seed ...
- 大数据学习——Linux-SSH报错:Could not resolve hostname centos02: Temporary failure in name resolution
https://blog.csdn.net/mcb520wf/article/details/83303792 随笔异常 ssh: Could not resolve hostname centos0 ...
- Leetcode 264.丑数II
丑数II 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10 ...
- hdu 4539
#include<stdio.h> #include<string.h> ]; int s]; int main() { int i,j,n,m; int ch; while( ...
- iOS时钟,秒针扫秒样式
昨天做一个时钟小demo,发现了一些问题. 描述能力有限,我封装好了一个时钟框架,朋友们可以参考 https://github.com/qianlishun/ClockView 点击这里可以 ...
- 《effective C++》:条款07——为多态基类声明virtual析构函数
在继承中,基类的析构函数需要定义为虚析构函数数否则: (1)当派生类对象经由一个base类指针删除时,而这个base类的析构函数不是虚函数时,其结果是未定义的. (2)这样做会导致derived类部分 ...
- poj2723 2sat判断解+二分
典型的2-sat问题,题意:有m个门,每个门上俩把锁,开启其中一把即可,现在给n对钥匙(所有 钥匙编号0123456...2n-1),每对钥匙只能用一把,要求尽可能开门多(按顺序,前max个). 关键 ...
- Object-C Xcode 编译提示 note: please rebuild precompiled header ZWYLPrefixHeader
错误提示如下图 解决思路: 由于手欠不小心,在.pch文件上的第一行加了几个文字,删除以后,还有一个空行,估计是这个空行引起的.删除这个空行,就好了.
- BZOJ 1123 tarjan
题目链接 题意:一张无向图,把第$i$个点关联的所有边去掉,求无向图中有多少个点对不连通. 题解: 如果割的不是割点,那么总答案是$2\times (n-1)$. 如果是割点,要分别考虑每个子树的贡献 ...
- BUPT2017 springtraining(16) #1 题解
https://vjudge.net/contest/162590 A: 不难发现,当L=R时输出L,当L<R时输出2. B: 贪心得配对.1和n配 2和n-1配,对与对直接只要花1个代价就可以 ...