C++单链表实现

明天考试,今天手写个单链表试试水。
本来以为挺简单的,没想到还是写了两个小时。一开始打算用模板类结果不太会用疯狂报错,最后只能搞个int型的。
只放代码。
和用vector写的程序对拍过后是正确的。

#include 
using namespace std;
typedef long long ll;
class List;

class Node //节点类
{
private:
    int value;
    Node *next;
public:
    Node(int x): value(x), next(0) {}
    friend class List;
    friend ostream &operator <<(ostream &, List);
    friend List operator +(const List, const List);
};

class List //链表类
{
private:
    Node *head, *tail;
public:
    List(): head(0), tail(0) {} //默认构造函数
    ~List();
    List (const List &); //拷贝构造函数 深拷贝
    void insert(int x); //插入节点
    void del(int x); //删除节点
    void reverse(); //逆置
    void display();
    friend ostream &operator <<(ostream &, const List);
    friend List operator +(const List, const List); //链表合并
};

List::~List()
{
    Node *p = head;
    while (p)
    {
        head = head -> next;
        delete p;
        p = head;
    }
    tail = 0;
}

List::List(const List &L)
{
    head = tail = 0;
    Node *p = L.head;
    while (p)
    {
        Node *p_now = new Node(*p);
        if (!head) head = tail = p_now;
        else tail -> next = p_now, tail = p_now;
        p = p -> next;
    }
}

void List::insert(int x)
{
    Node *p = new Node(x);
    if (!head) head = tail = p;
    else tail -> next = p, tail = p;
}

void List::del(int x)
{
    Node *prev = 0, *now = head;
    while (now)
    {
        if (now -> value == x) //删除相邻同值节点
        {
            if (prev)
            {
                if (now != tail)
                {
                    prev -> next = now -> next;
                    delete now; now = prev -> next;
                }
                else
                {
                    prev -> next = now -> next;
                    delete now; now = prev -> next;
                    tail = prev;
                }
            }
            else
            {
                head = now -> next;
                delete now; now = head;
            }
        }
        else prev = now, now = now -> next;
    }
}

void List::reverse()
{
    Node *prev = 0, *now = head, *latt = head -> next;
    while (now)
    {
        now -> next = prev;
        prev = now, now = latt;
        if (now) latt = now -> next;
    }
    swap(head, tail);
}

void List::display()
{
    Node *p = head;
    while (p)
    {
        cout << p -> value << ' ';
        p = p -> next;
    }
}


ostream &operator <<(ostream &output, const List L)
{
    Node *p = L.head;
    while (p)
    {
        output << p -> value << ' ';
        p = p -> next;
    }
    return output;
}

List operator +(const List a, const List b)
{
    List c;
    Node *p = a.head;
    while (p) c.insert(p -> value), p = p -> next;
    p = b.head;
    while (p) c.insert(p -> value), p = p -> next;
    return c;
}

int main()
{
    List L;
    int n; cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        int t; cin >> t; L.insert(t);
    }
    cout << L << endl; //测试数据插入是否正确
    int d; cin >> d;
    L.del(d);
    cout << L << endl; //测试数据删除是否正确
    L.reverse();
    cout << L << endl; //测试逆置是否正确
    return 0;
}

你可能感兴趣的:(C++单链表实现)