C++ 单链表

#include <iostream>
using namespace std;

template<class T>
struct Node
{
public:
    Node() { }
    Node(T t, Node* next);
public:
    T data;
    Node* next;
};

template<class T>
class List
{
public:
    List(); // 默认构造
    ~List(); // 析构
    List<T>& operator=(const List& list); // 重载 "=" 号
    List(const List& list); // 拷贝构造函数

    int size(); // 返回节点个数
    bool is_empty(); // 判断链表是否为空
    
    T get(int index); // 获取节点
    T get_first(); // 获取头节点
    T get_last(); // 获取尾节点
    
    void insert(int index, T t); // 在第index位置前,插入数据为t的节点
    void insert_first(T t); // 在头部添加节点
    void insert_last(T t); // 在尾部添加节点
    
    void del(int index); // 删除index位置上的节点
    void delect_first(); // 删除头节点
    void delect_last(); // 删除尾节点
    
private:
    Node<T>* get_node(int index); // 获取第index位置的节点
private:
    Node<T>* head; // 头结点
    int count; // 节点数量
};

template<class T>
// 节点默认构造函数
Node<T>::Node(T t, Node* next)
{
    this->data = t;
    this->next = next;
}

template<class T>
// 默认构造
List<T>::List() : count(0)
{
    head = new Node<T>();
    head->next = NULL;
}

template<class T>
// 析构函数
List<T>::~List()
{
    Node<T>* ptmp;
    Node<T>* phead = head->next;
    if (phead)
    {
        while (phead != head)
        {
            ptmp = phead;
            phead = phead->next;
            delete ptmp;
        }
    }
    delete head;
    head = NULL;
}

template<class T>
// 重载赋值 "=" 号
List<T>& List<T>::operator=(const List& list)
{
    if (this != &list)
    {
        head = new Node<T>();
        head->next = NULL;

        Node<T>* phead = head;
        Node<T>* ptmp = list.head->next;

        if (ptmp != NULL)
        {
            do
            {
                Node<T>* pnode = new Node<T>(ptmp->data, NULL);
                phead->next = pnode;
                phead = pnode;
                ptmp = ptmp->next;
            } while (ptmp != list.head);
            phead->next = head;
        }
    }
    return *this;
}

template<class T>
// 拷贝构造
List<T>::List(const List& list)
{
    head = new Node<T>();
    head->next = NULL;
    if (list.head->next == NULL)
    {
        return;
    }
    Node<T>* ptmp = list.head->next;
    do
    {
        head->next = ptmp;
        ptmp = ptmp->next;
    } while (ptmp != list.head);   
}

template<class T>
// 返回节点个数
int List<T>::size()
{
    return count;
}

template<class T>
// 判断链表是否为空
bool List<T>::is_empty()
{
    return count == 0;
}

template<class T>
// 获取第index位置的节点
Node<T>* List<T>::get_node(int index)
{
    // 判断有效性
    if (index < 0 || index >= count)
    {
        cout << "get node failed! the index in out of bound!" << endl;
        return NULL;
    }
    int i = 0;
    Node<T>* pindex = head->next;
    while (i++ < index)
    {
        pindex = pindex->next;    
    }
    return pindex;
}

template<class T>
// 获取节点
T List<T>::get(int index)
{
    return get_node(index)->data;
}

template<class T>
// 获取头节点
T List<T>::get_first()
{
    return get_node(0)->data;
}

template<class T>
// 获取尾节点
T List<T>::get_last()
{
    return get_node(count - 1)->data;
}

template<class T>
// 在第index位置前,插入数据为t的节点
void List<T>::insert(int index, T t)
{
    if (index == 0)
    {
        insert_first(t); 
        return;
    }
    Node<T>* pindex = get_node(index - 1);
    if (pindex != NULL)
    {
        // 创建新节点
        Node<T>* pnode = new Node<T>(t, pindex->next);
        pindex->next = pnode;
        count++;
    }
}

template<class T>
// 在头部添加节点
void List<T>::insert_first(T t)
{
    // 创建一个数据为t的新节点
    Node<T>* pnode = new Node<T>(t, NULL);
    // 引用头节点
    Node<T>* phead = head->next;
    
    if (phead == NULL)
    {
        head->next = pnode;
        pnode->next = head;
    }
    else
    {
        pnode->next = phead;
        head->next = pnode;
    }
    // 节点数量+1
    count++;
}

template<class T>
// 在尾部添加节点
void List<T>::insert_last(T t)
{
    // 创建一个新节点
    Node<T>* pnode = new Node<T>(t, head);
    // 引用头节点
    Node<T>* phead = head->next;
    
    if (phead == NULL)
    {
        head->next = pnode;
    }
    else
    {
        // 创建引用,指向尾节点
        Node<T>* ptmp = NULL;
        while (phead != head)
        {
            ptmp = phead;
            phead = phead->next;    
        }
        ptmp->next = pnode;
    }
    count++;
}

template<class T>
// 删除index位置上的节点
void List<T>::del(int index)
{
    if (index == 0)
    {
        head->next = head->next->next;    
    }
    else
    {
        // 获取第index位置上的节点
        Node<T>* pindex = get_node(index);
        // 获取第index-1位置上的节点
        Node<T>* ptmp = get_node(index - 1);
        // 删除pindex节点
        ptmp->next = pindex->next;
        delete pindex;    
    }
    
    count--;
    
}

template<class T>
// 删除头节点
void List<T>::delect_first()
{
    del(0);
}

template<class T>
// 删除尾节点
void List<T>::delect_last()
{
    del(count - 1);
}

/*
void int_test()
{
    List<int>* ls = new List<int>();
    
    ls->insert_last(5);
    ls->insert_last(6);
    ls->insert_last(7);
    
    ls->insert_first(3);
    
    ls->insert(1, 4);
    
    ls->delect_first();
    ls->delect_last();
    
    ls->del(1);
    
    for (int i = 0; i < ls->size(); ++i)
    {
        cout << ls->get(i) << endl;    
    }
    cout << endl;
    
    List<int>* ls2(ls);
    
    cout << "--------------------" << endl;
    for (int i = 0; i < ls2->size(); ++i)
    {
        cout << ls2->get(i) << endl;    
    }
    cout << endl;
    
    List<int>* ls3 = ls2;
    
    cout << "--------------------" << endl;
    for (int i = 0; i < ls3->size(); ++i)
    {
        cout << ls3->get(i) << endl;    
    }
    cout << endl;
        
}
*/

class Person
{
public:
    Person() { }
    Person(string name, int age)
    {
        this->name = name;
        this->age = age;
    }
public:
     // 姓名
     string name;
     // 年龄
     int age;
};

// 测试自定义数据类型
void person_test()
{
    Person p1("张三", 19);
    Person p2("李四", 14);
    Person p3("王五", 15);
    Person p4("赵六", 16);
    Person p5("机器", 16);
    
    List<Person>* list = new List<Person>();
    
    // 尾加
    list->insert_last(p1);
    list->insert_last(p2);
    list->insert_last(p3);
    list->insert_last(p4);
    
    // 头加
    list->insert_first(p5);
    
    for (int i = 0; i < list->size(); ++i)
    {
        cout << "姓名:" << list->get(i).name << " 年龄:" << list->get(i).age << endl;    
    }
    cout << endl;
    
}

int main()
{
    
    // int_test();

    person_test();

    return 0;
}
posted @ 2022-02-11 11:07  萨塔妮娅  阅读(24)  评论(0编辑  收藏  举报