实现list-- 模板

实现一个带头结点的双向带环链表:

观察这样一个链表(写出代码如下):


//List.h
#pragma once                                                                                                                         
#include <assert.h>
#include <iostream>
using namespace std;

template <class T>
struct ListNode
{
    ListNode(T x)
        :_prev(NULL)
         ,_next(NULL)
         ,_data(x)
    {}  
    ListNode* _prev;
    ListNode* _next;
    T _data;
};
template <class T>                                                                                                                   
class List
{
    typedef ListNode<T> Node;
public:
    List()
        :_head(new Node(T()))
    {
        _head->_next = _head;
        _head->_prev = _head;
    }
    ~List()
    {
        Node* cur = _head->_next;
        while(cur != _head)
        {                                                                                                                            
            Node* todelete = cur;
            cur = cur->_next;
            delete todelete;
        }
        delete _head;
        _head = NULL;
    }
    List(const List<T>& l);
    List<T>& operator=(const List<T>& l);
    void PushBack(T x);
    void PushFront(T x);
    void Insert(Node* pos, T x);
    void PopFront();
    void PopBack();
    void Erase(Node* pos);                                                                                                           
    ListNode<T>* Front();
    ListNode<T>* Back();
    size_t Size();
    int Empty();
    void print();
protected:
    Node* _head;
};


//List.cpp
#include <iostream>                                                                                                                  
#include "List.h"

template <class T>
void List<T>::Insert(Node* pos, T x)
{
    assert(pos);
    Node* pre = pos->_prev;
    Node* new_node = new Node(x);
    pre->_next = new_node;
    new_node->_next = pos;
    pos->_prev = new_node;
    new_node->_prev = pre;
    return;
}
template <class T>
void List<T>::PushBack(T x)
{                                                                                                                                    
    Insert(_head, x);
}
template <class T>
void List<T>::PushFront(T x)
{
    Insert(_head->_next, x);
}

template <class T>
void List<T>::Erase(Node* pos)
{
    assert(pos && pos != _head);
    Node* prev = pos->_prev;
    Node* next = pos->_next;
    prev->_next = next;                                                                                                              
    next->_prev = prev;
    delete pos;
}

template <class T>
void List<T>::PopFront()
{
    Erase(_head->_next);
}

template <class T>
void List<T>::PopBack()
{
    Erase(_head->_prev);
}                                                                                                                                    

template <class T>
ListNode<T>* List<T>::Front()
{
    return _head->_next;
}

template <class T>
ListNode<T>* List<T>::Back()
{
    return _head->_prev;
}
template <class T>
size_t List<T>::Size()                                                                                                               
{
    Node* cur = _head->_next;
    size_t count = 0;
    while(cur != _head)
    {
        cur = cur->_next;
        ++count;
    }
    return count;
}

template <class T>
int List<T>::Empty()
{
    return Size() == 0;                                                                                                              
}

template <class T>
void List<T>::print()
{
    Node* cur = _head->_next;
    while(cur != _head)
    {
        cout<<cur->_data<<"->";
        cur = cur->_next;
    }
    cout<<endl;
    cur = _head->_prev;
    while(cur != _head)
    {                                                                                                                                
        cout<<cur->_data<<"->";
        cur = cur->_prev;
    }
    cout<<endl;
}

template <class T>
List<T>::List(const List<T>& l)
{
   _head = new Node(T());
   _head->_next = _head;
   _head->_prev = _head;
   Node* cur = l._head -> _next;
   while(cur != l._head)
   {                                                                                                                                 
       PushBack(cur->_data);
       cur = cur->_next;
   }
}   
template <class T>
List<T>& List<T>::operator=(const List<T>& l)
{
    if(this != &l)
    {
        Node* cur = _head->_next;
        while(cur != _head)
        {
            Node* pre = cur->_prev;
            Node* nex = cur->_next;
            pre->_next = nex;                                                                                                        
            nex->_prev = pre;
            delete cur;
            cur = nex;
        }
        cur = (l._head)->_next;
        while(cur != l._head)
        {
           PushBack(cur->_data);
           cur = cur->_next;
        }
    }
    return *this;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言不支持类模板的概念,但可以通过结构体和指针来实现一个类似于list的数据结构。 首先,我们可以定义一个包含数据和指向下一个节点的指针的结构体,例如: ```c typedef struct Node { int data; struct Node* next; } Node; ``` 然后,我们可以定义一个包含头指针和尾指针的结构体来表示这个list,例如: ```c typedef struct List { Node* head; Node* tail; } List; ``` 接下来,我们可以实现一些常见的操作,例如创建list、添加元素、删除元素等等。下面是一些示例代码: ```c void initList(List* list) { list->head = NULL; list->tail = NULL; } void addElement(List* list, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; if (list->head == NULL) { list->head = newNode; list->tail = newNode; } else { list->tail->next = newNode; list->tail = newNode; } } void deleteElement(List* list, int data) { Node* current = list->head; Node* previous = NULL; while (current != NULL) { if (current->data == data) { if (previous == NULL) { list->head = current->next; } else { previous->next = current->next; } // 如果删除的是尾节点,更新tail指针 if (current == list->tail) { list->tail = previous; } free(current); return; } previous = current; current = current->next; } } ``` 通过以上代码,我们可以使用类似于list的数据结构来存储和操作元素。虽然这并不是真正意义上的类模板,但可以达到类似的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值