C语言习题 链表建立,插入,删除,输出
Problem B: C语言习题 链表建立,插入,删除,输出
Time Limit: 1 Sec Memory Limit:
128 MB
Submit: 222 Solved: 92
[Submit][Status][Web
Board]
Description
编写一个函数creatlink,用来建立一个动态链表。(包含学号和成绩)
编写一个函数printlink,用来输出一个链表。
编写一个函数dellink,用来删除动态链表中一个指定的结点(由实参指定某一学号,表示要删除该学生结点)。
编写一个函数insertlink,用来向动态链表插入一个结点。
编写一个函数freelink,用来释放一个动态链表。
Input
输入多个学生的学号和成绩,建立动态链表,以0 0 结束
输入学号,删除链表中的对应结点
插入两个链表结点
Output
输出的链表
#include<iostream>
#include<iomanip>
using namespace std;
struct student
{int num;
double score;
student *p;
}; student *creatlink(void)
{
student *head,*p,*s;
head=new student;
s=p=head;
while(1)
{cin>>(*p).num>>(*p).score;
if((*p).num==0&&(*p).score==0)
{delete p;(*s).p=NULL; return head;}
(*p).p=new student;
s=p;
p=p->p;
(*p).p=NULL;
} }
student *dellink(student *head,long a)
{student *p,*h;
h=p=head;
if((*p).num==a)
{head=(*p).p;
delete p; //////////////////////////
return head;}
while(1)
{
if((*p).num==a)
{h->p=p->p;delete p;return head;}
h=p;
p=(*p).p;
}
return head;}
student *insertlink(student *head,student *a)
{student *p,*h,*s;
s=h=head;
p=new student ;
p->num=a->num;
p->score=a->score;
p->p=NULL;
if(p->num<head->num)
{head=p;
p->p=h;return head;}
while(1)
{if(h->num>p->num)
{
p->p=h;
s->p=p;return head;}
s=h;
h=h->p;
if(h==NULL)
{s->p=p;p->p=NULL;return head;}
}
}
void printlink(student *head)
{
while(head!=NULL)
{
cout<<head->num<<' '; cout<<setiosflags(ios::fixed)<<setprecision(2)<<head->score<<endl;;
head=head->p; }
} void freelink(student *head)
{
student *p;
while(head!=NULL)
{
p=head->p;
delete head;
head=p; } }
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; }
C语言习题 链表建立,插入,删除,输出的更多相关文章
- YTU 2430: C语言习题 链表建立,插入,删除,输出
2430: C语言习题 链表建立,插入,删除,输出 时间限制: 1 Sec 内存限制: 128 MB 提交: 576 解决: 280 题目描述 编写一个函数creatlink,用来建立一个动态链表 ...
- 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] ...
- YTU 2429: C语言习题 学生成绩输入和输出
2429: C语言习题 学生成绩输入和输出 时间限制: 1 Sec 内存限制: 128 MB 提交: 1897 解决: 812 题目描述 编写一个函数print,打印一个学生的成绩数组,该数组中有 ...
- 20140502 static_cast和dynamic_cast的类型检查 双链表建立,删除,打印
1.static_cast和dynamic_cast的类型检查 static_cast的类型检查:只检查无关类之间的转换 CBaseY* pY1 = static_cast<CBaseY*> ...
- 链表的C++实现——创建-插入-删除-输出-清空
注:学习了数据结构与算法分析后,对链表进行了C++实现,参考博文:http://www.cnblogs.com/tao560532/articles/2199280.html 环境:VS2013 // ...
- 单链表的插入删除操作(c++实现)
下列代码实现的是单链表的按序插入.链表元素的删除.链表的输出 // mylink.h 代码 #ifndef MYLINK_H #define MYLINK_H #include<iostream ...
- [PHP] 数据结构-链表创建-插入-删除-查找的PHP实现
链表获取元素1.声明结点p指向链表第一个结点,j初始化1开始2.j<i,p指向下一结点,因为此时p是指向的p的next,因此不需要等于3.如果到末尾了,p还为null,就是没有查找到 插入元素1 ...
- 「C语言」单链表/双向链表的建立/遍历/插入/删除
最近临近期末的C语言课程设计比平时练习作业一下难了不止一个档次,第一次接触到了C语言的框架开发,了解了View(界面层).Service(业务逻辑层).Persistence(持久化层)的分离和耦合, ...
随机推荐
- Wamp环境下配置--Apache虚拟主机
1.首先打开apache的配置文件httpd.conf,并去掉#Include conf/extra/httpd-vhosts.conf前面的#,启用虚拟主机功能 # Virtual hosts In ...
- hdu3038(并查集)
题目链接 题意:有n次询问,给出a到b区间的总和,问这n次给出的总和中有几次是和前面已近给出的是矛盾的 分析:sum数组维护着到根节点的距离(即区间和),每次合并x,y,s(a,b分别为x,y的根节点 ...
- leetcode - Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- jquery下php与ajax的数据交换方式
参考链接:http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2013/0122/11971.html 一.前台传递字符串变量,后台返回字符串变量( ...
- excel删除问号~?~
1.直接替换(菜单)编辑——替换——查找内容——(输入)~?~——替换为(空,就是什么都不输入)——全部替换.2.设原数据在A列,从A1开始,若得到的数值数据需要参与计算,则在B1输入=--LEFT( ...
- Cordova CLI源码分析(一)——简介
本系列文章分析基于node.js的命令行工具Cordova CLI,所以如果对node.js基础不是很了解,建议参考http://nodejs.gamesys.net/node-js提供的基础教程 文 ...
- css实现自适应屏幕高度
body,html{ margin:0px; height:100%; }
- 关于使用X-UA-Compatible来设置IE浏览器兼容模式
原文地址:http://www.cnblogs.com/nidilzhang/archive/2010/01/09/1642887.html 前言 为了帮助确保你的网页在所有未来的IE版本都有一致的外 ...
- dpdk组态 千兆网卡 驱动 失败 原因分析及 解决方案
dpdk版本号是1.7.1稳定版,server它是ubuntu12.04LTS x86 64bit 绑定默认驱动程序千兆网卡ixgbe失败 # ./dpdk_nic_bind.py -b ixgbe ...
- 关于Core Location-ios定位
IOS中的core location提供了定位功能,能定位装置的当前坐标,同一时候能得到装置移动信息.由于对定位装置的轮询是非常耗电的,所以最好仅仅在非常必要的前提下启动. 当中,最重要的类是CLLo ...