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 ...
随机推荐
- IIS Express mime type 列表。
C:\Users\Administrator\Documents\IISExpress\config\applicationhost.config -------------------------- ...
- flask 基础ssti注入
源代码地址 (请用python2.7运行,python3有点出入) 注入点: 不是返回的静态模板而是反回模板字符串变得让客户端可以控制. XSS 这里直接 http://39.105.116.195: ...
- ADB push 和ADB pull命令
adb push命令 :从电脑上传送文件到手机: adb pull命令 :从手机传送文件到电脑上
- 常用模块(string)
import string# dt = string.digits # 获取0-9的数字# dt = string.ascii_letters # 获取所有的大小写字母# dt = string.he ...
- Python全栈工程师(递归函数、闭包)
ParisGabriel 每天坚持手写 一天一篇 决定坚持几年 全栈工程师 Python人工智能从入门到精通 函数式编程: 是指用一系列函数解决问题 每一个函数完成细 ...
- vim使用的一些积累
vi visual interfacevim vi improved vim模式:编辑模式(命令模式)输入模式末行模式 编辑模式下,zz保存并退出移动光标:(编辑模式)1.逐字符移动 h 左 l 右 ...
- 团队冲刺Alpha(七)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- abp ef codefirst 设置默认值
public partial class tableIsWaringfiled : DbMigration { public override void Up() { //设置默认值为true Add ...
- ASP.NET——视频总结
ASP.NET的视频很早就看完了,但一直还没顾上总结.虽然在备战软考,学习任务很重,但是阶段的总结还是不要推太久了,不然也就起不到总结的效果了.在看视频之前,虽然已经做过了新闻发布系统,但是对B/S一 ...
- 【距离GDOI:130天】 AC自动机ing
弄完后缀数组,终于能安心来复习AC自动机了..其实当时学的很不好,非常不好..模版都是有问题的...今天花了第一节晚修和一节自习算是把AC自动机的基础弄好了...切掉3道基础题,然后就被某道坑爹题坑掉 ...