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 爬取网页中JavaScript动态添加的内容(一)

    当我们进行网页爬虫时,我们会利用一定的规则从返回的 HTML 数据中提取出有效的信息.但是如果网页中含有 JavaScript 代码,我们必须经过渲染处理才能获得原始数据.此时,如果我们仍采用常规方法 ...

  2. [ecmanget][常用标签]bookmark

    Bookmarks Bookmarks 书签栏 redis Try RedisRedisRedis应用2-Redis实现开发者头条页面点赞功能 - noaman_wgs的博客 - CSDN博客wind ...

  3. idea中maven项目放到包中的mapper的xml文件不发布的问题

    今天重新一下mybatis的基础,然后一直报错,提示的是 result map 找不到com.zm.model.User对象可是看 mapper的写法没问题.找了半天才发现 是mapper没扫描到 解 ...

  4. 使用awk根据多维度统计系统tps

    业务简单日志格式: [2017-08-28 01:00:00.523]transfer_search,7001045,1708271100083774377,20170827,,,,,,,,,,001 ...

  5. 多线程 线程组 ThreadGroup

    package org.zln.thread; import java.util.Date; /** * Created by sherry on 000024/6/24 22:30. */ publ ...

  6. hdu 1564 Play a game (博弈)

    Play a game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. [poj] 3068 "Shortest" pair of paths || 最小费用最大流

    [原题](http://poj.org/problem?id=3068) 给一个有向带权图,求两条从0-N-1的路径,使它们没有公共点且边权和最小 . //是不是像传纸条啊- 是否可行只要判断最后最大 ...

  8. yii2.0查询慢的原因

    最近使用Yii2.0来搭建项目,测试的时候发现无论是请求列表数据还是发布数据,都很慢,然后我一步一步打印时间来查看哪里的问题,始终找不到原因,最后在网上看到这篇: 'db' => [ 'clas ...

  9. Linux 软连接 & 硬链接

    1.Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接]硬连接指通过索引节点 ...

  10. [codevs_1237]餐巾计划问题

    题目描述 一个餐厅在相继的N 天里,每天需用的餐巾数不尽相同.假设第i天需要ri块餐巾(i=1,2,…,N).餐厅可以购买新的餐巾,每块餐巾的费用为p分:或者把旧餐巾送到快洗部,洗一块需m天,其费用为 ...