C++模拟链表
C++模拟链表
简易模拟链表,工厂设计模式。。
注意:请不要在操作时产生环状链表,会造成输出链表时陷入无限循环。
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#define range(i,a,b) for(int i=a;i<=b;++i)
#define rerange(i,a,b) for(int i=a;i>=b;--i)
#define LL long long
#define CLS(arr) memset(arr,0,sizeof(arr))
using namespace std;
inline void line(){range(i,,)cout<<(i==?'\n':'=');}//内置画线函数
struct node{
/*
* 链表节点
*/
int value;
node *next;
node(int val=){value=val;next=NULL;}
};
class linklist{
/*
* 链表类
*/
private:
node* head;
unsigned long len;
public:
linklist(unsigned long l=):len(l){
/*
* 链表构造,默认链表长度为1,传入其他参数可扩充。
* 模仿vector性质,未设置值的位置默认为0
*/
head=new node;
if(len==){head->next=NULL;}
else{
node *tmp=head;
range(i,,len){
head->next=new node;
head=head->next;
}
head->next=NULL;
head=tmp;
}
}
void combine(unsigned long l){len=l;}
void insert(unsigned long index,int value){
/*
* 在index后面插入一个value值的节点。
*/
node *tmp=head;
if(index>=len){cout<<"list index out of range"<<endl;return;}
while(index--)head=head->next;
node *store=head->next;
head->next=new node(value);
head->next->next=store;
head=tmp;
++len;
}
void pop(unsigned long index){
/*
* 删除index位置的节点。
*/
node *tmp=head;
if(index>=len){cout<<"list index out of range"<<endl;return;}
--len;
if(!index){
head=head->next;
delete(tmp);
return;
}
while((index--)-)head=head->next;
node *pos=head->next->next;
delete(head->next);
head->next=pos;
head=tmp;
}
void pop(node* pos){
/*
* 删除指针为pos的节点。
* 此函数为模拟迭代器。
*/
node *tmp=head;
--len;
if(head==pos){
head=head->next;
delete(tmp);
return;
}
while(head->next!=pos)head=head->next;
node *store=head->next->next;
delete(head->next);
head->next=store;
head=tmp;
}
void setvalue(unsigned long index,int value){
/*
* 改变index位置的值为value
*/
node *tmp=head;
if(index>=len){cout<<"list index out of range"<<endl;return;}
while(index--)head=head->next;
head->value=value;
head=tmp;
}
node* begin(){
/*
* 返回头节点地址
*/
return head;
}
node* end(){
/*
* 返回尾节点地址
*/
node *tmp=head;
while(tmp->next)tmp=tmp->next;
return tmp;
}
unsigned long length(){ return len; }//输出链表长度
void show(){
/*
* 输出整个链表
*/
node *tmp=head;
while(head){
cout<<head->value<<" ";
head=head->next;
}
cout<<endl;
head=tmp;
}
};
class factory{
/*
* 链表工厂:
* 创建链表、选择链表、合并两个链表
* 默认新增同名链表覆盖原链表
*/
private:
map<string,linklist*>data;
public:
int MENU(){
/*
* 工厂菜单
*/
unsigned int choice;
line();
cout<<"1.创建链表"<<endl;
cout<<"2.选择链表"<<endl;
cout<<"3.合并链表"<<endl;
cout<<"0.退出"<<endl;
line();
cout<<"选择:";cin>>choice;
if(choice>){cout<<"menu index out of range"<<endl;return -;}
else return choice;
}
int listmenu(){
/*
* 链表操作菜单
*/
unsigned int choice;
line();
cout<<"1.插入"<<endl;
cout<<"2.删除"<<endl;
cout<<"3.更改"<<endl;
cout<<"4.去奇"<<endl;
cout<<"0.退出"<<endl;
line();
cout<<"选择:";cin>>choice;
if(choice>){cout<<"menu index out of range"<<endl;return -;}
else return choice;
}
void MAIN(){
/*
* 链表工厂的主函数
*/
int tmp;
while((tmp=MENU())&&tmp){
switch (tmp){
case :create();break;
case :select();break;
case :combine();break;
case -:break;
}
}
}
void create(){
/*
* 创建链表。
*/
string name;
unsigned long len;
cout<<"链表命名:";cin>>name;
cout<<"链表长度:";cin>>len;
data[name]=new linklist(len<=?:len);
}
void select(){
/*
* 选择链表进入链表菜单。
*/
string name;int cnt=;
line();
map<string,linklist*>::iterator iter;
for(iter=data.begin();iter!=data.end();++iter)cout<<iter->first<<((++cnt)%?' ':'\n');
if(cnt%)cout<<endl;
line();
cout<<"请选择如上例举出的链表名:";cin>>name;
if(data[name]==NULL){cout<<"不存在该链表!"<<endl;return;}
while((cnt=listmenu())&&cnt){
unsigned long index;int value;
switch (cnt){
case :cout<<"输入位置、值:";cin>>index>>value;data[name]->insert(index,value);break;
case :cout<<"输入位置:";cin>>index;data[name]->pop(index);break;
case :cout<<"输入位置、值:";cin>>index>>value;data[name]->setvalue(index,value);break;
case :
node *begin=data[name]->begin(),*end=data[name]->end(),*pos;
for(pos=begin;pos!=end;pos=pos->next)if(pos->value&)data[name]->pop(pos);
}
data[name]->show();
}
}
void combine(){
/*
* 将两链表首尾结合。
*/
string name1,name2;int cnt=;
line();
map<string,linklist*>::iterator iter;
for(iter=data.begin();iter!=data.end();++iter)cout<<iter->first<<((++cnt)%?' ':'\n');
if(cnt%)cout<<endl;
line();
cout<<"请选择如上例举出的两个链表名:";cin>>name1>>name2;
if(data[name1]==NULL||data[name2]==NULL){cout<<"不存在链表!"<<endl;return;}
node *end=data[name1]->end(),*begin=data[name2]->begin();
end->next=begin;
data[name1]->combine(data[name1]->length()+data[name2]->length());
data[name1]->show();
}
};
int main(int argc, char *argv[]){
factory MM;//创建链表类工厂
MM.MAIN();//进入工厂主函数
return ;
}
C++模拟链表的更多相关文章
- hdu5009 Paint Pearls (DP+模拟链表)
http://acm.hdu.edu.cn/showproblem.php?pid=5009 2014网络赛 西安 比较难的题 Paint Pearls Time Limit: 4000/2000 M ...
- UVa12657 - Boxes in a Line(数组模拟链表)
题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...
- CF 552(div 3) E Two Teams 线段树,模拟链表
题目链接:http://codeforces.com/contest/1154/problem/E 题意:两个人轮流取最大值与旁边k个数,问最后这所有的数分别被谁给取走了 分析:看这道题一点思路都没有 ...
- UVA11988-Broken Keyboard(数组模拟链表)
Problem UVA11988-Broken Keyboard Accept: 5642 Submit: 34937 Time Limit: 1000 mSec Problem Descripti ...
- C - Boxes in a Line 数组模拟链表
You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simul ...
- B - Broken Keyboard (a.k.a. Beiju Text) 数组模拟链表
You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...
- 天梯赛 L2-022. (数组模拟链表) 重排链表
题目链接 题目描述 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→....例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2 ...
- HDU 6215 Brute Force Sorting(模拟链表 思维)
Brute Force Sorting Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- UVA11988:悲剧文本(模拟链表)
You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problem wi ...
随机推荐
- maven的一些使用配置!
1.国外库太慢,更换为国内镜像库在你的maven安装目录下找到conf目录下的setting.xml修改:<mirrors> <id>CN</id> <nam ...
- nginx 快速查看配置文件的方法
查看nginx实际调用的配置文件 1.查看nginx路径 ps aux|grep nginx root ?? S :43上午 :00.08 nginx: worker process root ?? ...
- 关于jdk与jre的区别
JDK:Java Development Kit JRE顾名思义是java运行时环境,包含了java虚拟机,java基础类库.是使用java语言编写的程序运行所需要的软件环境,是提供给想运行java程 ...
- ocrosoft Contest1316 - 信奥编程之路~~~~~第三关 问题 E: IQ(iq)
http://acm.ocrosoft.com/problem.php?cid=1316&pid=4 题目描述 根据世界某权威学会的一项调查,学信息学的学生IQ非常高.举个最好的例子,如果我们 ...
- 发现一个form小问题
在使用编辑器及框架时,form表单如果在太靠内的div层里,就取不到textarea的post值,具体原因位置,可能跟框架的CSS有关
- zoj 1002 Fire Net (二分匹配)
Fire Net Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose that we have a square city with s ...
- HDU - 3072 Intelligence System
题意: 给出一个N个节点的有向图.图中任意两点进行通信的代价为路径上的边权和.如果两个点能互相到达那么代价为0.问从点0开始向其余所有点通信的最小代价和.保证能向所有点通信. 题解: 求出所有的强连通 ...
- 深入解析vue.js响应式原理与实现
vue.js响应式原理解析与实现.angularjs是通过脏检查来实现数据监测以及页面更新渲染.之后,再接触了vue.js,当时也一度很好奇vue.js是如何监测数据更新并且重新渲染页面.vue.js ...
- 洛谷 P4883 mzf的考验 解题报告
P4883 mzf的考验 题目背景 \(mzf\)立志要成为一个豪杰,当然,他也是一个\(OIer\). 他希望自己除了会\(OI\)之外还会各种东西,比如心理学.吉他.把妹等等. 为了让自己有更大的 ...
- 染色 color
染色 color 题目描述 有一块矩阵平板,分成n*m个格子,一开始全是白色.在这上面进行k次染色,每次染色按照如下步骤:1. 随机选择一个格子,称为A.2. 随机选择一个格子,称为B.3. 将由A ...