c++实现双向循环链表
2017-07-09 16:42
579 查看
#pragma once #include #include using namespace std; template struct ListNode { ListNode(T x):data(x),front(this),back(this){} T data; ListNode *front; ListNode *back; }; template class List_double { public: List_double(); List_double(const List_double& other); List_double& operator = (const List_double& other); ~List_double(); public: void InsertNode(T x, ListNode *temp = NULL); T DeleteNode(ListNode *del = NULL); ListNode* Find(T x); void PrintFront(); void PrintBack(); public: ListNode *head; }; template List_double::List_double() { head = NULL; } template List_double::List_double(const List_double& other) { head = NULL; ListNode *cur = other.head; while (cur) { InsertNode(cur->data); cur = cur->front; if (cur == other.head) break; } } template void List_double::InsertNode(T x, ListNode *ins = NULL)//在节点ins之后插入,默认插入表后 { ListNode *temp = new ListNode(x); if (head == NULL) { head = temp; return; } if (ins == NULL) { temp->front = head->front; temp->back = head; temp->back->front = temp; temp->front->back = temp; } else { temp->front = ins; temp->back = ins->back; temp->back->front = temp; temp->front->back = temp; } } template List_double& List_double::operator = (const List_double& other) { head = NULL; ListNode *cur = other.head; while (cur) { InsertNode(cur->data); cur = cur->front; if (cur == other.head) break; } } template List_double::~List_double() { while (head->front != head) { DeleteNode(); } delete head; } template T List_double::DeleteNode(ListNode *del = NULL)//默认删除表后数据,删除del所指位置 { assert(head != NULL); T ret; if (head->front == head) { ret = head->data; delete head; head = NULL; return ret; } if (del == NULL) { head->front = head->front->front; ret = head->front->back->data; delete head->front->back; head->front->back = head; return ret; } else { del->front->back = del->back; del->back->front = del->front; ret = del->data; delete del; return ret; } } template ListNode* List_double::Find(T x) { ListNode *p = head->front; while (p) { if (p == head) { if (p->data == x) return p; else return NULL; } if (p->data == x) { return p; } else { p = p->front; } } } template void List_double::PrintFront() { ListNode *p = head; while (p) { if (p == head->front) { cout << p->data << ' '; return; } else { cout << p->data << ' '; p = p->back; } } } template void List_double::PrintBack() { ListNode *p = head->front; while (p) { if (p == head) { cout << p->data << ' '; return; } else { cout << p->data << ' '; p = p->front; } } } #include #include #include "List.h" using namespace std; int main() { int n; cout << "操作:增加(1),删除(2),从前往后输出(3),从后往前输出(4),查找(5)" << endl; List_double list; while (cin >> n) { if (n == 0) break; switch (n) { case 1: int tt; cout << "请输入增加的数据int:" << endl; cin >> tt; list.InsertNode(tt); break; case 2: cout << "删除的数据为" << list.DeleteNode() << endl;; break; case 3: list.PrintFront(); break; case 4: list.PrintBack(); break; case 5: int ttt; cout << "请输入查找的数据int:" << endl; cin >> ttt; if (list.Find(ttt) == NULL) cout << "没有此数据" << endl; else cout << "存在此数据" << endl; break; } cout << "操作:增加(1),删除(2),从前往后输出(3),从后往前输出(4),查找(5)" << endl; } getchar(); getchar(); return 0; }
相关文章推荐
- C++模板实现双向循环链表(有带头结点)
- 双向循环链表删除算法的C++程序实现
- 【C++】模板实现带头节点的双向循环链表
- 双向循环链表的C++实现
- C++实现数据结构三 双向循环链表
- C++实现双向循环链表
- (C++版)链表(四)——实现双向循环链表创建、插入、删除等简单操作
- (C++版)链表(四)——实现双向循环链表创建、插入、删除等简单操作
- C++实现循环双向链表
- C++实现双向循环链表
- 双向循环链表插入算法的C++程序实现
- C++模板及用模板实现双向循环链表,顺序表
- 双向循环链表的头插、中插、尾插、删除、逆序顺序显示(C++实现)
- 如何用C++实现双向循环链表
- C++类模板 实现双向循环链表的基本算法 《数据结构》(C++版 北京科海)中摘抄
- C++实现有哨兵的双向循环链表
- 双向循环链表的c++ 实现
- 链表的C语言实现之循环链表及双向链表
- 链表的C语言实现之循环链表及双向链表
- 数据结构学习系列三-单向循环链表(c++实现且应用模板)