动态表头的双向链表
来源:互联网 发布:最大公约数的算法 编辑:程序博客网 时间:2024/05/20 13:10
请实现以下基于双向链表的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函数。
注意,不需要提交main函数。
Problem Source: 第五周 5班
enum 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>() { next = NULL; back = NULL; } Node<List_entry>(List_entry e, Node<List_entry> * b = NULL, Node<List_entry> * n = NULL) { entry = e; next = n; back = b; }};template <class List_entry>class MyList {public: MyList() { count = 0; current = NULL; curPosition = 0; } ~MyList() { clear(); } // 拷贝构造函数和赋值运算符重载,注意深拷贝与浅拷贝的差异 MyList(const MyList<List_entry> ©) { count = 0; current = NULL; curPosition = 0; *this = copy; } void operator =(const MyList<List_entry> ©) { clear(); if (copy.empty()) return; Node<List_entry> * copyHead = copy.current; while (copyHead->back) copyHead = copyHead->back; current = new Node<List_entry>(copyHead->entry); while (copyHead->next) { copyHead = copyHead->next; current->next = new Node<List_entry>(copyHead->entry, current); current = current->next; } count = copy.count; curPosition = count - 1; setPosition(copy.curPosition); } // 清空list void clear() { if (!current) return; setPosition(0); while (current) { Node<List_entry> * temp = current; current = current->next; delete temp; } count = 0; current = NULL; curPosition = 0; } // 判断list是否为空 bool empty() const {return count == 0;} // 判断list是否已满 bool full() const { // 试着new一个节点,如果new不了了说明满了 Node<List_entry> * temp = new Node<List_entry>; if (temp) { delete temp; return false; } else { return true; } } // 获取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; Node<List_entry> * temp = new Node<List_entry>(entry); if (position == 0) { if (count == 0) { current = temp; } else { setPosition(0); temp->next = current; current->back = temp; current = temp; // 由于此前调用setPosition函数使得curPosition的位置为0,这里current指针必须和curPosition相对应 } } else { setPosition(position - 1); Node<List_entry> * oriNext = current->next; current->next = temp; temp->back = current; temp->next = oriNext; if (oriNext) oriNext->back = temp; } 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; if (position == 0) { if (count == 1) { entry = current->entry; delete current; current = NULL; } else { setPosition(0); Node<List_entry> * temp = current; current = current->next; current->back = NULL; entry = temp->entry; delete temp; } } else { Node<List_entry> * temp; setPosition(position - 1); temp = current->next; current->next = current->next->next; if (current->next) current->next->back = current; entry = temp->entry; delete temp; } count--; return success; } // 获取第position个位置的元素,保存在entry中 // 若position < 0 或者 position >= count,则返回underflow 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 &)) { Node<List_entry> * temp = current; if (temp == NULL) return; while (temp->back != NULL) temp = temp->back; while (temp != NULL) { visit(temp->entry); temp = temp->next; } }protected: int count; // 记录list内元素数量 mutable int curPosition; // current指针的位置编号 mutable Node<List_entry> *current; // current指针 // 设置current指针的位置,指向第position个位置 void setPosition(int position) const { if (position < curPosition) { while (curPosition != position) { curPosition--; current = current->back; } } else { while (curPosition != position) { curPosition++; current = current->next; } } }};
- 动态表头的双向链表
- 带表头节点的双向循环链表编程
- 带表头结点的双向循环链表
- 带表头结点的双向循环链表
- 带表头结点的双向循环链表
- 带表头结点的双向循环链表
- 不带表头的单向链表,带表头的单向链表,带表头的单向循环链表,带表头的双向循环链表。
- 双向动态链表
- 链表(单链表,带表头的链表,单向循环链表,双向循环链表)
- 数据结构:双向链表list的表头/尾添加数据、链表显示、链表清空
- 双向链表(链表尾加数据链表头加数据删除销毁)
- 实现双向固定表头的表格
- 双向循环链表头文件C语言
- string 动态双向链表的创建、排序,反转等
- WV.3-动态链表-新结点总是链表头
- bootstrap的table插件动态加载表头【表头】。
- 带表头节点的循环双向链表(头插,尾插,中间插,清除,前向显示,后向显示)
- html中动态分级表头的实现
- Sicily 1008. Translations
- hdu 4819 Mosaic(树套树型二维线段树)
- 手环测评(二)
- 函数传参、传引用、传指针时间复杂度分析
- Python进行SQLite数据库操作
- 动态表头的双向链表
- c++中的静态成员变量与静态成员函数
- Sicily 1005. Roll Playing Games
- Sicily 1151. 魔板
- RDLC报表刷新问题
- Sicily 1004. I Conduit!
- hibernate4基础梳理(一)---一个问题忙一天
- 了解OS
- POJ 3239 Solution to the n Queens Puzzle