c++ 单链表创建

#include<iostream>
using namespace std;

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

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

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

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

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

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

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

};


int main()
{
    LinkList<int> L;
    L.CreateFormTail();
    L.ReadFormTail();
    return 0;
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值