c++实现双向循环链表

#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; } 
                                               
                                              
                                             
                                            
                                           
                                          
                                         
                                        
                                       
                                      
                                     
                                    
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值