首页 > 代码库 > 单链表

单链表

#include <iostream> using namespace std;template <typename T>class List{private:    template <typename N>     struct Node    {        N data, *next;            Node() { data = 0; next = NULL; }        Node(N x, N *n = NULL ): data(x), next(n) {}    };        Node<T> *Head;    int last = 0; public:    List() { Head = new Node<T>; }        List(const T &x)     {        Head = new Node<T>;        Head->next = new Node<T>(x);         last++;    }        List(List<T> &L)    {        this->makeEmpty();        for(Node *p = Head->next; p != NULL; p = p->next)            this->Insert(i, new Node(p->data));     }        ~List() { makeEmpty(); }        void makeEmpty(Node<T> *p = Head->next)    {        if(p->next != NULL) makeEmpty(p->next);        delete p;    }         int Length() const { return last; }        Node<T>* getHead() const { return Head; }        Node<T>* Search(T x)    {        for(Node<T> *p = Head->next; p!=NULL; p = p->next)            if(p->data =http://www.mamicode.com/= x) return p;        return NULL;     }        Node<T>* Locate(int i)    {        for(Node<T> *p = Head->next, int r = 1; p != NULL; p = p->next, r++)            if(r == i) return p;         return NULL:    }        bool getData(int i, T &x) const    {        if(Node *p = Locate(i) != NULL)        {            x = p->data;            return true;            }        return false;    }        bool setData(int i, T &x)    {        if(Node *p = Locate(i) != NULL)        {            p->data =http://www.mamicode.com/ x;            return true;            }        return false;    }        bool Insert(int i , T &x)    {        last++;        for(Node<T> *p = Head->next, int r = 1; p != NULL; p = p->next, r++)            if(r + 1 == i || p->next = NULL) p->next = new Node(x, p->next);    }            bool Remove(int i, T &x)    {        if(Node *p = Locate(i - 1) != NULL || p->next != NULL)        {            Node *d = p->next;            p->next = d->next;            delete d;            return true;            }        return false;    }        bool IsEmpty() const { return Head->next == NULL ? true : false; }        bool IsFull() const { return false; }        void sortInsert(T x)    {        node *p = Head->next;           if(p->next == NULL)            {               p->next = new node(x, NULL);                return ;        }                    for(; p->next != NULL; p = p->next)           {               if(p->next->num > x)            {                   p->next = new node(x, p->next);                return ;            }        }        p->next = new node(x, NULL);    }        void Sort()    {        Node *head = new Node();        for(Node *p = Head->next; p != NULL; p = p->next)            head->sortInsert(p->data);        this.makeEmpty();        this.Head = head;                }        void input()    {        int n;        cin >> n;        for(int i = 0; i < n; i++)        {            int x;            cin >> x;            this->Insert(i + 1; x);        }        }        void output()    {        cout << "{";        for(Node *p = Head->next; p != NULL; p = p->next)            cout << p->data << " ";        cout << "}" << endl;    }        List<T> &operator = (List<T> &L)    {        this->makeEmpty();        for(Node *p = L.Head->next, Node *P = Head; p != NULL; p = p->next, P = P->next)            P->next = new(p->data);        return this->Head;    }}; int main(){    }

今晚思路好乱, 我他喵的也不知到我在写什么了, 代码还没编译通过, 贴上方便用手机看。。。

 

单链表