#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
//#include<list>
using namespace std; //only for int use;
/*
class MyList{
int *my;
MyList();
int back();//返回最后一个元素
int front();//返回第一个元素
void clear();//清除所有元素
bool empty();//是否为空
void insert(int item); //插入元素
int pop_back();
int pop_front();
void push_back(int item);
void push_front(int item);
void sort();
}; MyList::MyList()
{
my=new int[N];
}
*/
//it's a program to packing
//定义结构,单链表
typedef struct MyList{
int item;
struct MyList *next;
}MyList,*List; List head,back;
int *a=new int[]; //从尾部插入
void push_back(int item)
{
List p=head;
while(p->next!=NULL){
p=p->next;
}
List q=(List)malloc(sizeof(MyList));
q->item=item;
q->next=NULL;
p->next=q;
back=q;
} //从头部插入
void push_front(int item)
{
List p,q;
p=head;
q=p->next;
List in=(List)malloc(sizeof(MyList));
in->item=item;
p->next=in;
in->next=q;
} //从尾部弹出
int pop_back()
{
List p,q;
q=head;
p=q->next;
while(p->next!=NULL){
q=p;
p=p->next;
}
q->next=NULL;
back=q;
int item=p->item;
free(p);
return item;
} //从头部弹出
int pop_front()
{
List p=head;
List q=p->next;
int item=q->item;
p->next=q->next;
free(q);
return item;
} //移除元素值为item的所有节点
int remove(int item)
{
List p=head;
List q=p->next;
int num=;
while(q!=NULL){
if(q->item==item){
num++;
p->next=q->next;
if(p->next==back){
back=p;
}
q=p->next;
}
else{
p=p->next;
q=p->next;
}
}
return num;
} //清除所有元素
void clear()
{
List p=head;
p=p->next;
while(p!=NULL){
List q=p->next;
int item=p->item;
free(p);
p=q;
cout<<"clear() "<<item<<endl;
}
head->next=NULL;
} //按位置(index)插入item
void insert(int index,int item)
{
List p;
p=head;
List q=p;
p=p->next;
int i=;
while(p!=NULL){
i++;
cout<<"i:"<<i<<endl;
if(i==index){
List t=(List)malloc(sizeof(MyList));
t->item=item;
q->next=t;
t->next=p;
break;
} q=p;
p=p->next;
}
if(i!=index)
cout<<"你输入的数字错误:please reload:"<<endl;
} //反转list 通过记录每个节点的值,然后重新赋值
//所以会用到clear()函数 ,及清除原来链表生成新链表
void reverse()
{
List p=head;
int i=;
while(p->next!=NULL){
p=p->next;
int item=p->item;
a[i++]=item; }
clear();
p=head;
cout<<i<<endl;
for(int j=i-;j>=;j--)
{
List q=(List)malloc(sizeof(MyList));
q->item=a[j];
q->next=NULL;
p->next=q;
p=q;
}
} //排序list: 通过记录每个节点的值,然后重新对值进行排序
//所以会用到clear()函数 ,及清除原来链表生成新链表
void sort()
{
List p=head;
int i=;
while(p->next!=NULL){
p=p->next;
int item=p->item;
a[i++]=item;
}
clear();
p=head;
sort(a,a+i);
for(int j=;j<i;j++)
{
List q=(List)malloc(sizeof(MyList));
q->item=a[j];
q->next=NULL;
p->next=q;
p=q;
}
} //返回list大小
int size()
{
List p=head;
int i=;
while(p->next!=NULL){
p=p->next;
i++;
}
return i;
} //判断是否为空
bool isEmpty()
{
if(head->next==NULL)
return true;
return false;
}
//for test use
void print()
{
List p=head;
while(p->next!=NULL){
p=p->next;
cout<<p->item<<" ";
}
cout<<endl;
} int main()
{
head=(List)malloc(sizeof(MyList));
back=(List)malloc(sizeof(MyList));
head->next=NULL;
back=head; push_front();
push_front();
push_back();
push_back();
push_back();
push_back();
push_front();
push_front();
print();
sort();
cout<<"after sort()"<<endl;
print();
reverse();
print();
cout<<"size() "<<size()<<endl;
insert(,);
print();
cout<<"remove() "<<remove()<<endl;
print();
cout<<"pop_back() "<<pop_back()<<endl;
cout<<"pop_front() "<<pop_front()<<endl;
print();
clear();
print(); return ;
}

我的STL之旅 MyList的更多相关文章

  1. 我的STL之旅 MyStack

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...

  2. c++ STL 学习记录 草稿。

    非常丑陋的尝试实现stl.慢慢修改吧. 1)简单实现 vector和list. 2)思索如何开始编写算法. 1,所有容器继承一个抽象容器.那么算法就可以使用抽象基类的next方法来遍历元素. 容器间耦 ...

  3. 动手实现自己的 STL 容器《2》---- list

    1. 序: 本文参考了侯捷的 <STL 源码分析>一书,出于兴趣,自行实现了简单的 list 容器. 学习了 STL 的 list 容器的源代码,确实能够提高写链表代码的能力.其中的 so ...

  4. C++ Standard Template Library STL(undone)

    目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...

  5. 标准C++中的STL容器类简单介绍

    SGI -- Silicon Graphics[Computer System] Inc.硅图[计算机系统]公司. STL -- Standard Template Library 标准模板库.   ...

  6. C#与C++相比较之STL篇

    引言 Program into Your Language, Not in It--<代码大全>.如何深入一门语言去编程?我认为有三步:熟悉它:知道它的局限性:扩展它.如何熟悉?不必说,自 ...

  7. 转:STL使用入门( Using STL)

    1 介绍 我最开始结束C++编程是从DOS下的Borland C++开始的.那时他们在最新版本3.1中就包含了一套模板库用来做collection.那真是个好东东.当我开始使用Visual C++ 2 ...

  8. 【C++探索之旅】开宗明义+第一部分第一课:什么是C++?

    内容简介 1.课程大纲 2.第一部分第一课:什么是C++? 3.第一部分第二课预告:C++编程的必要软件 开宗明义 亲爱的读者,您是否对C++感兴趣,但是C++看起来很难,或者别人对你说C++挺难的, ...

  9. STL空间配置器

    1.什么是空间配置器? 空间配置器负责空间配置与管理.配置器是一个实现了动态空间配置.空间管理.空间释放的class template.以内存池方式实现小块内存管理分配.关于内存池概念可以点击:内存池 ...

随机推荐

  1. DIV下的DIV居中

    .ParentDIV{ display: -webkit-flex; display: flex; -webkit-align-items: center; align-items: center; ...

  2. 在node.js中使用mongose模块

    对象与文档相对应 创建项目目录,用root进入 # mkdir /home/test/part9/ 直接# npm install mongoose,报错如下 ../node_modules/nan/ ...

  3. python——挖装饰器祖坟事件

    装饰器是什么呢? 我们先来打一个比方,我写了一个python的插件,提供给用户使用,但是在使用的过程中我添加了一些功能,可是又不希望用户改变调用的方式,那么该怎么办呢? 这个时候就用到了装饰器.装饰器 ...

  4. windows下面安装casperjs

    因为需要 就学习了一下casperjs,CasperJS是一个开源的导航脚本处理和测试工具,基于PhantomJS(前端自动化测试工具)编写.由于casperjs对PhantomJS的依赖性,所以需要 ...

  5. setInterval和setTimeout的区别

    setInterval会每隔指定的毫秒数后反复执行指定代码. setTimeout只会在指定的毫秒数后执行一次指定代码. setInterval的用法: // 创建(创建后即开始计时) var int ...

  6. 终于解决了IE8不支持数组的indexOf方法,array的IndexOf方法

    /* 终于解决了IE8不支持数组的indexOf方法 */ if (!Array.prototype.indexOf) { Array.prototype.indexOf = function (el ...

  7. Python (1) - 7 Steps to Mastering Machine Learning With Python

    Step 1: Basic Python Skills install Anacondaincluding numpy, scikit-learn, and matplotlib Step 2: Fo ...

  8. set集合类型

    set表示一个集合,相当于C#中的Hashtable,不同的地方在于,所有的值共用一个key. 相关操作如下图.

  9. 烟草公司基于BPM的IT一体化变革

    广州烟草有限公司隶属于广东省烟草专卖局,中国烟草总公司统一领导.垂直管理.主要职能是负责广州市的烟草生产.经营企业和市场的专卖管理.成立20多年来,广州烟草致力于烟叶和卷烟的生产经营.多元化经营.技术 ...

  10. opencv基本的数据结构(转)

    DataType : 将C++数据类型转换为对应的opencv数据类型 enum { CV_8U=0, CV_8S=1, CV_16U=2, CV_16S=3, CV_32S=4, CV_32F=5, ...