#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++实现双向循环链表
最新推荐文章于 2022-09-22 15:27:33 发布