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. 【Pascal's Triangle II 】cpp

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...

  2. SpringMVC 集成 Freemarker 模板引擎

    本文通过 maven 项目中集成 1.引入 SpringMVC 与 Freemarker 需要的依赖 <!-- SpringMVC --> <dependency> <g ...

  3. JMeter学习笔记(九) 参数化2--CSV Data Set Config

    2.CSV Data Set Config 1)添加 CSV Data Set Confi 2)配置CSV Data Set Config 3)添加HTTP请求,引用参数,格式 ${} 4)执行HTT ...

  4. Mybatis + Oracle 批量insert的问题

    这个问题真的太坑了 之前用ibatis+sql server 的foreach 很容易就写出来批量insert数据,但是测试后报错:SQL结束格式错误 现在换到银行工作,数据库也换成Oracle了 特 ...

  5. REMIX与LOCALHOST相连

    REMIX与LOCALHOST相连 让Remix与本地文件系统进行交互,点击connect同时找到localhost下的Remix文件管理器的共享目录.在开始之前,参考网址: https://remi ...

  6. Python下安装protobuf

    1. 下载安装包 2. 解压缩 tar –xzvf protobuf-2.6.1.tar.gz 3. 安装protoc 在python中使用protocbuf需要Protocal Buffer 编译器 ...

  7. Android动态添加和移除布局

    package com.hyang.administrator.studentproject; import android.os.Bundle; import android.support.v7. ...

  8. linux fork()函数 转载~~~~

    转自  ::  http://blog.csdn.net/jason314/article/details/5640969  一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork ...

  9. ComboBox列表自定义类保存数据

    之前没弄明白ComboBox还可以这样用. 先建一个ComboBox子项类,然后可以获取该项类做一些判断,关键是要重写ToString()方法. public class ComboItem { pu ...

  10. mac系统安装/升级node

    一.安装 1.node 是通过brew来安装的,所以第一步先安装brew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Ho ...