#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. [Selenium] 数字显示的月份转换为英文显示

    如果只需要英文的前三位字母,可以自己截取.

  2. myeclipse maven pom.xml 配置错误

    http://www.oschina.net/question/2265006_219341#tags_nav maven pom.xml 配置文件错误       腾讯云消息队列CMQ架构解析> ...

  3. js高级程序设计(六)面向对象

    ECMA-262 把对象定义为:“无序属性的集合,其属性可以包含基本值.对象或者函数.”严格来讲,这就相当于说对象是一组没有特定顺序的值.对象的每个属性或方法都有一个名字,而每个名字都映射到一个值.正 ...

  4. C++ const、volatile、mutable的用法 (转)

      const.volatile.mutable的用法 鸣谢作者: http://blog.csdn.net/wuliming_sc/article/details/3717017 const修饰普通 ...

  5. <转>下一代Asp.net开发规范OWIN(1)—— OWIN产生的背景以及简单介绍

    2014-09-04 07:22 by JustRun http://www.cnblogs.com/JustRun1983/p/3955238.html 随着VS2013的发布,微软在Asp.Net ...

  6. iOS开发UI篇—常见的项目文件介绍

    iOS开发UI篇—常见的项目文件介绍 一.项目文件结构示意图 二.文件介绍 1.products文件夹:主要用于mac电脑开发的可执行文件,ios开发用不到这个文件 2.frameworks文件夹主要 ...

  7. Swift学习

    Swift 中文教程(一)基础数据类型 基础类型 虽然Swift是一个为开发iOS和OS X app设计的全新编程语言,但是Swift的很多特性还是跟和Objective-C相似. Swift也提供了 ...

  8. POJ 2828 线段树(想法)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 15422   Accepted: 7684 Desc ...

  9. cocopods 知识集合 及 一个 好的 国外iOS技术翻译站

    http://www.exiatian.com/cocoapods%E5%AE%89%E8%A3%85%E4%BD%BF%E7%94%A8%E5%8F%8A%E9%85%8D%E7%BD%AE%E7% ...

  10. 删除ubuntu旧版本内核

    方法一: 1.查看系统下可使用的内核有哪些 dpkg --get-selectiongs|grep linux-image liming@CM:~$ dpkg --get-selections|gre ...