基于双向链表的list
来源:互联网 发布:淘宝客cms免费 编辑:程序博客网 时间:2024/06/03 18:45
Description
请实现以下基于双向链表的list类
enum Error_code{ success, underflow, overflow};template <class List_entry>struct Node{ List_entry entry; Node<List_entry> *next; Node<List_entry> *back;};template <class List_entry>class MyList{public: MyList(); ~MyList(); // 拷贝构造函数和赋值运算符重载,注意深拷贝与浅拷贝的差异 MyList(const MyList<List_entry> ©); void operator =(const MyList<List_entry> ©); // 清空list void clear(); // 判断list是否为空 bool empty() const; // 判断list是否已满 bool full() const; // 获取list的元素数量 int size() const; // 在第position个位置插入值为entry的元素,如果position为0则插入在链表头,依次类推 // 若position < 0 或者 position > count,则返回underflow Error_code insert(int position, const List_entry &entry); // 删除第position个位置的元素,并将该元素的值保存在entry中 // 若position < 0 或者 position >= count,则返回underflow Error_code remove(int position, List_entry &entry); // 获取第position个位置的元素,保存在entry中 // 若position < 0 或者 position >= count,则返回underflow Error_code retrieve(int position, List_entry &entry) const; // 将第position个位置的元素替换为entry // 若position < 0 或者 position >= count,则返回underflow Error_code replace(int position, const List_entry &entry); // 用visit函数遍历list内所有的元素 void traverse(void (*visit)(List_entry &));protected: int count; // 记录list内元素数量 mutable int curPosition; // current指针的位置编号 mutable Node<List_entry> *current; // current指针 // 设置current指针的位置,指向第position个位置 void setPosition(int position) const;};
注意,不需要提交main函数。
Input
None
Output
None
题解:
关于双向链表的构造并不难,注意要慎重处理链表的插入与删除操作,比如:向当前位置插入节点时,需要考虑当前位置之前是否存在节点;删除当前节点时,需要考虑当前位置之前以及之后是否存在节点。
Code
// Problem#: 20997// Submission#: 5187924// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen Universityenum Error_code{ success, underflow, overflow};template <class List_entry>struct Node{ List_entry entry; Node<List_entry> *next; Node<List_entry> *back; Node<List_entry>() {entry = 0; next = NULL; back = NULL;} Node<List_entry>(List_entry entry, Node<List_entry> * back,Node<List_entry> * next):entry(entry),back(back),next(next){}};template <class List_entry>class MyList{public: MyList() { count = 0; curPosition = -1; current = 0; } ~MyList() { clear(); } // 拷贝构造函数和赋值运算符重载,注意深拷贝与浅拷贝的差异 MyList(const MyList<List_entry> ©) { if (copy.empty()) { count = 0; curPosition = -1; current = 0; } else { int index = copy.curPosition; Node<List_entry> * point = copy.current; while(index!=0) { index--; point = point->back; } count = copy.count; current = new Node<List_entry>(point->entry,0,0); while(point->next!=0) { point = point->next; current->next = new Node<List_entry>(point->entry,current,0); current = current->next; } curPosition = count-1; } } void operator =(const MyList<List_entry> ©) { clear(); if (copy.empty()) { count = 0; curPosition = -1; current = 0; } else { int index = copy.curPosition; Node<List_entry> * point = copy.current; while(index!=0) { index--; point = point->back; } count = copy.count; current = new Node<List_entry>(point->entry,0,0); while(point->next!=0) { point = point->next; current->next = new Node<List_entry>(point->entry,current,0); current = current->next; } curPosition = count-1; } } // 清空list void clear() { if (empty()) { curPosition = -1; current = 0; return ; } List_entry tmp; while(!empty()) remove(0,tmp); curPosition = -1; count = 0; current = 0; } // 判断list是否为空 bool empty() const { return count==0; } // 判断list是否已满 bool full() const { return false; } // 获取list的元素数量 int size() const { return count; } // 在第position个位置插入值为entry的元素,如果position为0则插入在链表头,依次类推 // 若position < 0 或者 position > count,则返回underflow Error_code insert(int position, const List_entry &entry) { if (position < 0 ||position > count) return underflow; if (position==0&&empty()) { current = new Node<List_entry>(entry,0,0); curPosition = 0; count++; return success; } if (position==count) { setPosition(count-1); current->next = new Node<List_entry>(entry,current,0); current = current->next; count++; curPosition++; return success; } setPosition(position); Node<List_entry> * tmp = new Node<List_entry>(entry,current->back,current); if (current->back!=0) current->back->next = tmp;//链表插入的重点 current->back = tmp; current = tmp; count++; return success; } // 删除第position个位置的元素,并将该元素的值保存在entry中 // 若position < 0 或者 position >= count,则返回underflow Error_code remove(int position, List_entry &entry) { if (position < 0||position >= count) return underflow; setPosition(position); Node<List_entry> * tmp = current; entry = tmp->entry; if (current->back!=0) //链表删除的重点 { current->back->next = current->next; if (current->next!=0) current->next->back = current->back; } else if (current->next!=0) current->next->back = 0; current = current->next; count--; if (count==0) { curPosition = -1; current = 0; } delete tmp; return success; } Error_code retrieve(int position, List_entry &entry) const { if (position < 0||position >= count) return underflow; setPosition(position); entry = current->entry; return success; } // 将第position个位置的元素替换为entry // 若position < 0 或者 position >= count,则返回underflow Error_code replace(int position, const List_entry &entry) { if (position < 0||position >= count) return underflow; setPosition(position); current->entry = entry; return success; } // 用visit函数遍历list内所有的元素 void traverse(void (*visit)(List_entry &)) { if (empty()) return ; setPosition(0); while(curPosition!=count-1) { visit(current->entry); current = current->next; curPosition++; } visit(current->entry); }protected: int count; // 记录list内元素数量 mutable int curPosition; // current指针的位置编号 mutable Node<List_entry> *current; // current指针 // 设置current指针的位置,指向第position个位置 void setPosition(int position) const { if (empty()) return ; if(curPosition == position) return ; if (curPosition>=position) { while(curPosition!=position) { current = current->back; curPosition--; } } else { while(curPosition!=position) { current = current->next; curPosition++; } } }};
阅读全文
0 0
- 基于双向链表的list
- 双向链表list
- 基于模板类的双向链表
- List-c双向链表
- List双向链表容器
- List双向链表容器
- 双向循环链表list
- List (双向链表)
- list双向链表容器
- C++ List 双向链表
- 双向循环链表list
- list双向链表容器
- List环形双向链表
- List双向链表容器
- c++ List(双向链表)
- c++ List(双向链表)
- 双向链表容器list的头尾中,全部删除
- std::list(双向循环链表)的使用?
- 欢迎使用CSDN-markdown编辑器
- linux网络编程之socket(五):tcp流协议产生的粘包问题和解决方案
- 【nachos】山东大学操作系统课设实验nachos系统(2):线程同步
- Echart问题
- 命令行退出和遇到的一些问题
- 基于双向链表的list
- offsetLeft与style.left的区别
- Python中的X[:,0]和X[:,1]
- Bitmap 图像解码以及缩略图生成----BitmapFactory类
- 【CF768G】The Winds of Winter 可持久化线段树 DFS序
- UVa10537 Toll! Revisited
- Java的JVM GC(Garbage Collection)垃圾回收原理机制及算法 .
- linux网络编程之socket(六):利用recv和readn函数实现readline函数
- 安卓代码简单实现电商购物车