c++实现双向循环链表
来源:互联网 发布:java程序员的简历 编辑:程序博客网 时间:2024/06/06 12:42
#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;elsereturn 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;elsecout << "存在此数据" << endl;break;}cout << "操作:增加(1),删除(2),从前往后输出(3),从后往前输出(4),查找(5)" << endl;}getchar();getchar();return 0;}
阅读全文
0 0
- C语言实现双向循环链表
- c双向循环链表实现
- C语言实现双向循环链表
- C语言实现双向循环链表
- C语言实现双向循环链表
- 数据结构--双向循环链表C实现
- C语言双向循环链表实现
- C语言实现双向循环链表
- 【C++】实现双向循环链表
- c语言实现双向循环链表
- C实现循环链表及双向链表
- C语言 实现循环链表及双向链表
- 双向循环链表的C语言实现
- 双向循环链表实现文件C语言
- 双向循环链表的实现(C)
- 数据结构的C实现_双向循环链表
- C语言实现双向循环链表[下]
- linux中C语言实现双向循环链表
- ubuntu 16.04, caffe, matlab2016b 安装
- Feature Pyramid Networks for Object Detection
- Ansible 使用高级进阶
- PAT甲级真题及训练集(24)--1004. Counting Leaves (30)
- 动态网格
- c++实现双向循环链表
- 图像压缩算法
- WebService学习
- eclipse javaweb 常见报错解决方案
- 【Unity3d】查看引用资源的文件
- ElasticSearch使用SQL
- 再论图像拉伸操作
- Latex公式的缩放
- 快速排序