OJ第三批——Problem N: 熟悉题型——填空题(删除线性表节点)
2015-07-01 18:45
435 查看
问题及代码:
128 MB
Submit: 277 Solved: 165
[Submit][Status][Web
Board]
已经给出部分代码,
#include<iostream>
using namespace std;
struct Linklist
{
int num;
Linklist *next;
};
Linklist *creat(int l,int n)
{
Linklist *t=new Linklist;
t->num=l;//每个节点编号
if(n==1)
{
t->next=NULL;
return t;
}
cin>>l;
t->next=creat(l,n-1);
return t;
}
void printf(Linklist *p,int n)
{
if(p==NULL)
return ;
cout<<p->num;
if(n!=1)
cout<<" ";
printf(p->next,n--);
}
Linklist *dellink(Linklist *head,int num)
{
Linklist *p1,*p2;
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
/************
请补充缺少的代码,使该程序完成功能。
***************/
return head;
}
int main()
{
int n,l,m;
cin>>n;
Linklist *head;
cin>>l;
head=creat(l,n);
cin>>m;
head= dellink(head,m);
printf(head,n);
return 0;
}
输入数据:1 2 3 4 5 6
输入 item:5
填写的部分可能不止一行,根据自己的判断添加。
填空题添加的代码一般很短。
运行结果:
Problem N: 熟悉题型——填空题(删除线性表节点)
Time Limit: 1 Sec Memory Limit:128 MB
Submit: 277 Solved: 165
[Submit][Status][Web
Board]
Description
给出一串具体长度的数据,删除指定数据。已经给出部分代码,
#include<iostream>
using namespace std;
struct Linklist
{
int num;
Linklist *next;
};
Linklist *creat(int l,int n)
{
Linklist *t=new Linklist;
t->num=l;//每个节点编号
if(n==1)
{
t->next=NULL;
return t;
}
cin>>l;
t->next=creat(l,n-1);
return t;
}
void printf(Linklist *p,int n)
{
if(p==NULL)
return ;
cout<<p->num;
if(n!=1)
cout<<" ";
printf(p->next,n--);
}
Linklist *dellink(Linklist *head,int num)
{
Linklist *p1,*p2;
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
/************
请补充缺少的代码,使该程序完成功能。
***************/
return head;
}
int main()
{
int n,l,m;
cin>>n;
Linklist *head;
cin>>l;
head=creat(l,n);
cin>>m;
head= dellink(head,m);
printf(head,n);
return 0;
}
Input
输入 n:6输入数据:1 2 3 4 5 6
输入 item:5
Output
输出:1 2 3 4 6Sample Input
10 1 2 3 4 5 6 7 8 9 10 8
Sample Output
1 2 3 4 5 6 7 9 10
HINT
只提交注释的部分,即填写的部分。填写的部分可能不止一行,根据自己的判断添加。
填空题添加的代码一般很短。
#include<iostream> using namespace std; struct Linklist { int num; Linklist *next; }; Linklist *creat(int l,int n) { Linklist *t=new Linklist; t->num=l;//每个节点编号 if(n==1) { t->next=NULL; return t; } cin>>l; t->next=creat(l,n-1); return t; } void printf(Linklist *p,int n) { if(p==NULL) return ; cout<<p->num; if(n!=1) cout<<" "; printf(p->next,n--); } Linklist *dellink(Linklist *head,int num) { Linklist *p1,*p2; p1=head; while(num!=p1->num&&p1->next!=NULL) { p2=p1; p1=p1->next; } if(p1->num==num) { if(p1==head) head=p1->next; else p2->next=p1->next; } return head; } int main() { int n,l,m; cin>>n; Linklist *head; cin>>l; head=creat(l,n); cin>>m; head= dellink(head,m); printf(head,n); return 0; }
运行结果:
相关文章推荐
- c++面试题
- linux 除了某个文件或某个文件夹以外全部删除
- angular input使用输入框filter格式化日期
- c++面试题
- 清算流程
- Why choose Lua?
- Oracle11g安装出现em.ear
- 入门指南
- 实现函数功能对数组元素进行插入、删除、查询操作
- 记账流程
- 如何从BBC网站学习英语
- 一、iOS控制器view的创建方式(官方文档翻译)
- ubuntu下几种Android测试工具
- 对账流程
- 根据Unicode编码用C#语言把它转换成汉字的代码
- MySQL PLSQL Demo - 003.静态游标
- Java字符串总结
- iOS通过dSYM文件分析crash
- 退款流程
- [转载]执行中的专注