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++模拟链表的更多相关文章

  1. hdu5009 Paint Pearls (DP+模拟链表)

    http://acm.hdu.edu.cn/showproblem.php?pid=5009 2014网络赛 西安 比较难的题 Paint Pearls Time Limit: 4000/2000 M ...

  2. UVa12657 - Boxes in a Line(数组模拟链表)

    题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...

  3. CF 552(div 3) E Two Teams 线段树,模拟链表

    题目链接:http://codeforces.com/contest/1154/problem/E 题意:两个人轮流取最大值与旁边k个数,问最后这所有的数分别被谁给取走了 分析:看这道题一点思路都没有 ...

  4. UVA11988-Broken Keyboard(数组模拟链表)

    Problem UVA11988-Broken Keyboard Accept: 5642  Submit: 34937 Time Limit: 1000 mSec Problem Descripti ...

  5. 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 ...

  6. 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 ...

  7. 天梯赛 L2-022. (数组模拟链表) 重排链表

    题目链接 题目描述 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→....例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2 ...

  8. HDU 6215 Brute Force Sorting(模拟链表 思维)

    Brute Force Sorting Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  9. UVA11988:悲剧文本(模拟链表)

    You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problem wi ...

随机推荐

  1. python 学习分享-socketserver

    SocketServer内部使用 IO多路复用 以及 “多线程” 和 “多进程” ,从而实现并发处理多个客户端请求的Socket服务端.即:每个客户端请求连接到服务器时,Socket服务端都会在服务器 ...

  2. os--留

    os.path.abspath(path) #返回绝对路径    绝对路径和文件路径的区别,绝对路径是当前在操作文本的路径,文件路径是当前文本的文件的路径 os.path.basename(path) ...

  3. input()报错:TypeError: '>=' not supported between instances of 'str' and 'int'

    今天学习python基础—分支与循环,接触到了if.在练习一个if语句的时候,出现了错误. 题目是: 根据分数划分成四个级别:优秀.良好.及格.不及格,分数是72: grade = 72if grad ...

  4. 【转载】Unity3d UnityEditor编辑器定制和开发插件

    在阅读本教程之前,你需要对Unity的操作流程有一些基础的认识,并且最好了解内置的GUI系统如何使用. 如何让编辑器运行你的代码 Unity3D可以通过事件触发来执行你的编辑器代码,但是我们需要一些编 ...

  5. 孤荷凌寒自学python第二十天python的匿名函数与偏函数

    孤荷凌寒自学python第二十天python的匿名函数与偏函数 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Python为使函数的使用更加方便高效,可以使用两种特殊的函数简化语句书写. 一 ...

  6. 课时17:函数:Python的乐高积木

    目录: 一.创建和调用函数 二.函数的参数 三.函数的返回值 四.课时17课后习题及答案 为了使得程序得代码变得简单,就需要把程序分解成较小得组成部分.有三种方法可以实现:函数.对象.模块. **** ...

  7. React跨域问题解决

    https://segmentfault.com/q/1010000012732581 非跨域问题报错 -rpccorsdomain="http://localhost:3000" ...

  8. C#利用VFW实现摄像头程序

    最近在搞这个考试监控,找来VFW的资料,胡编乱凑而成. VFW全称为Video for Windows,是微软提供的,内嵌windows系统. 首先定义一个VideoAPI类. 首先调用avicap3 ...

  9. LINQ to Entities 不识别方法“System int string 转换的问题

    这个问题困扰了挺久,网上找了挺多方法 都太好使. 分几种情况. 1.如果查询结果 转换,那比较容易. var q = from c in db.Customers where c.Country == ...

  10. Lambda表达式使用2

    1.概述 本篇主要介绍lambda中常用的收集器,收集器的作用就是从数据流中生成需要的数据接口. 最常用的就是Collectors.toList(),只要将它传递给collect()函数,就能够使用它 ...