List(3)List的单链式实现的增强版(保留当前位置)
来源:互联网 发布:上海至寻网络骗局 编辑:程序博客网 时间:2024/06/06 00:45
list.h
/*----------------------------------------------- Created By EverSteins Email:EverSteins@gmail.com 转载请注明出处 ------------------------------------------------*/ typedef int ElemType;typedef struct Node{ElemType entry;Node *next;Node():entry(0),next(NULL){}Node(ElemType entry):entry(entry),next(NULL){}Node(ElemType entry,Node *link=NULL):entry(entry),next(link){}}Node;class List{public:List():count(0),head_node(NULL),current_position(0),current_node(NULL){}//List(const ElemType ©);//void operator=(const ElemType ©);~List();bool insert(int position,const ElemType &x);bool remove(int position,ElemType &x);bool empty() const;void showAll() ;protected:int count;Node *head_node;mutable int current_position;mutable Node *current_node;void set_position(int position) const;};
list.cc
/*----------------------------------------------- Created By EverSteins Email:EverSteins@gmail.com 转载请注明出处 ------------------------------------------------*/ #include "stdafx.h"#include "utility.h"#include "list.h"using namespace std;void List::set_position(int position) const{//pre:0<=position<countif (position<current_position){current_position=0;current_node=head_node;}for (;current_position!=position;current_position++)current_node=current_node->next;}bool List::insert(int position,const ElemType &x){//pre:0<=position<countif (position<0 || position>count)return false;if (position>0){set_position(position-1);Node *old_node=current_node->next;Node *new_node=new Node(x,old_node);current_node->next=new_node;}else{Node *new_node=new Node(x,head_node);head_node=new_node;current_position=0;current_node=head_node;}count++;return true;}bool List::remove(int position,ElemType &x){if (position<0 || position>=count)return false;if (position>0){set_position(position-1);Node *remove_node=current_node->next;Node *remove_following_node=remove_node->next;current_node->next=remove_following_node;x=remove_node->entry;delete remove_node;}else{//todo}count--;return true;}bool List::empty() const{return count<=0;}void List::showAll() {Node *current_node=head_node;while (NULL != current_node){cout<<current_node->entry<<" ";current_node=current_node->next;}}List::~List(){Node *current_node=head_node;while (NULL != current_node){Node *following_node=current_node->next;delete current_node;current_node=following_node;}}
main.cc
/*----------------------------------------------- Created By EverSteins Email:EverSteins@gmail.com 转载请注明出处 ------------------------------------------------*/ #include "stdafx.h"#include "utility.h"#include "list.h"using namespace std;int _tmain(int argc, _TCHAR* argv[]){List list;list.insert(0,0);list.insert(1,10);list.insert(2,20);list.insert(3,30);list.insert(4,40);list.insert(5,50);list.insert(3,-1);list.insert(3,-2);list.insert(3,-3);list.showAll();return 0;}
- List(3)List的单链式实现的增强版(保留当前位置)
- List(1)List单链表的链式实现
- List(2)List的顺序实现
- 线性表(List)---栈的链式存储
- 线性表(List)---队列的链式存储
- ArrayList,List等非链式线性结构是如何实现动态增长的
- 单队列的链式表示和实现
- 基于List实现适配器Queue(链式队列)
- list的实现
- list 方法的实现
- list 方法的实现
- list 方法的实现
- list 方法的实现
- list容器的实现
- python list的实现
- list 的c实现
- list的实现
- List数据结构的实现
- java web中IBATIS的工作原理(1)
- 暗时间-我不想与我不能
- 去除vss源代码管理
- 笔记:Gof设计模式--Builder
- Note on Hadoop Tutorial : Introduction
- List(3)List的单链式实现的增强版(保留当前位置)
- 黑马程序员_javaIO流(一)
- 基于lucene的nutch索引详解
- 图像处理与计算机视觉:基础,经典以及最近发展(4)图像处理与分析
- 使用iOS4的GestureRecognizers识别手势(Xcode4)
- MYSQL - ERROR 1130:Host is not allow
- iOS 4 Gesture Recognizers 应用实战
- 可访问性不一致
- Linux C编程 简单电话薄(从文件中读取记录,支持插入、删除和保存操作)