我的STL之旅 MyList
#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的更多相关文章
- 我的STL之旅 MyStack
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...
- c++ STL 学习记录 草稿。
非常丑陋的尝试实现stl.慢慢修改吧. 1)简单实现 vector和list. 2)思索如何开始编写算法. 1,所有容器继承一个抽象容器.那么算法就可以使用抽象基类的next方法来遍历元素. 容器间耦 ...
- 动手实现自己的 STL 容器《2》---- list
1. 序: 本文参考了侯捷的 <STL 源码分析>一书,出于兴趣,自行实现了简单的 list 容器. 学习了 STL 的 list 容器的源代码,确实能够提高写链表代码的能力.其中的 so ...
- C++ Standard Template Library STL(undone)
目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...
- 标准C++中的STL容器类简单介绍
SGI -- Silicon Graphics[Computer System] Inc.硅图[计算机系统]公司. STL -- Standard Template Library 标准模板库. ...
- C#与C++相比较之STL篇
引言 Program into Your Language, Not in It--<代码大全>.如何深入一门语言去编程?我认为有三步:熟悉它:知道它的局限性:扩展它.如何熟悉?不必说,自 ...
- 转:STL使用入门( Using STL)
1 介绍 我最开始结束C++编程是从DOS下的Borland C++开始的.那时他们在最新版本3.1中就包含了一套模板库用来做collection.那真是个好东东.当我开始使用Visual C++ 2 ...
- 【C++探索之旅】开宗明义+第一部分第一课:什么是C++?
内容简介 1.课程大纲 2.第一部分第一课:什么是C++? 3.第一部分第二课预告:C++编程的必要软件 开宗明义 亲爱的读者,您是否对C++感兴趣,但是C++看起来很难,或者别人对你说C++挺难的, ...
- STL空间配置器
1.什么是空间配置器? 空间配置器负责空间配置与管理.配置器是一个实现了动态空间配置.空间管理.空间释放的class template.以内存池方式实现小块内存管理分配.关于内存池概念可以点击:内存池 ...
随机推荐
- struts入门初步(一)
struts2.0与struts1.0运用了不同的框架,有一定的不兼容性. struts2.0借鉴了webwork的框架思想. Struts2的基本步骤: 1.拷贝struts的jar到项目中(导 ...
- HTML5和CSS3登录页面制作实录
本文详细介绍使用HTML5 和CSS3 制作一个登录页面的完整过程. View demo login.html <form id="login"> <h1> ...
- git: No refs in common and none specified; doing no
用gitolite新建项目,clone后首次push,可能会出现: $ git push No refs in common and none specified; doing nothing ...
- 如何在MapControl界面添加双击事件实现标绘及符号样式更改
private void axMapControl1_OnDoubleClick(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnDo ...
- 张艾迪(创始人):Hello.世界...
The World No.1 Girl :Eidyzhang The World No.1 Internet Girl :Eidyzhang AOOOiA.global Founder :Eidyzh ...
- iOS中的translucent和automaticallyAdjustsScrollViewInsets用法
关于这两个属性我长话短说 具体的可以更具具体情况来设置: translucent用法 automaticallyAdjustsScrollViewInsets用法 translucent用法 iOS7 ...
- Windows下搭建Git开发环境
Windows下搭建Git开发环境主要有以下三种方法: 1,VS,vs2013和vs2015中已经集成了git插件了 2,msysGit+TortoiseGit 3,msysGit+SourceTre ...
- iOS开发UI篇—Quartz2D简单使用(二)
iOS开发UI篇—Quartz2D简单使用(二) 一.画文字 代码: // // YYtextview.m // 04-写文字 // // Created by 孔医己 on 14-6-10. // ...
- PHP分页做法
1.分页封装类 <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 priv ...
- WP8.1 Study18:动态磁贴
一.前言 动态磁贴在WindowsPhone8.1和Windows8.1都是其特色,有人喜欢有人讨厌,不过我觉得还是挺好的,可以让使用者很快知道App内的内容和吸引使用者打开App.下面来学习下怎样添 ...