c++ 单链表创建

c++ 单链表创建,第1张

c++ 单链表创建
#include
using namespace std;

template class linkList;
template 
class Node
{
private:
    T data;
    Node *next;
public:
    friend class linkList;
    Node(T x,Node *y=NULL):data(x),next(y){}
};
template 
class linkList
{
private:
    Node *head;
public:
    linkList(T a=-1,Node *b=NULL)
    {
        head=new Node(a,b);
    }
    linkList(const linkList &L)
    {
        head=new Node;
        Node* x=L.head;
        while(x->next)
        {
            x=x->next;

        }
    }
    void InitFormTail(T c)
    {
        Node* p=head;
        while(p->next)
            p=p->next;
        p->next=new Node(c);

    }
    void InitFormTail(int a,T c)
    {
        Node *p=head;
        int x=1;
        while(p->next && xnext;
            x++;
        }
        p->next=new Node(c,p->next);

    }
    void CreateFormTail()
    {
        T c;
        Node *p=NULL,*tail;
        tail=head;
        tail->next=p;
        while(cin>>c)
        {

            if(!c) break;
            p=new Node(c);
            tail->next=p;
            tail=p;
        }
    }

    void ReadFormTail()
    {
        Node* p=head;
        while(p->next)
        {
            p=p->next;
            cout<data<<" ";
        }
    }

    ~linkList()
    {
        Node*p;
        while(head)
        {
            p=head->next;
            delete head;
            head=p;
        }
        delete head;
    }

};


int main()
{
    linkList L;
    L.CreateFormTail();
    L.ReadFormTail();
    return 0;
}

代码先放这,解析以后再补

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5520690.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-13
下一篇2022-12-13

发表评论

登录后才能评论

评论列表(0条)

    保存