c++單鏈表

來源:互聯網
上載者:User

標籤:creat   ext   sys   const   data   mes   ctr   delete   spl   

#include<iostream>#include<cstring>using namespace std;class cnode{    public :        int data;        cnode *next;        cnode()        {            next=NULL;        }}; class clist{    private:        cnode *head;    public:        clist();        void create();        void display();        ~clist();        int getlin() const;        bool isempty()const;        bool find(const int e) const;        cnode *getnode(int i) const;        void insert(int i,const int e);        void Delete(const int e);        void reverse();                };clist::clist(){    head=new cnode();    head->next=NULL;}void clist::create(){    cnode *p,*q;    p=head;    q=new cnode();    cout<<"請輸入值ctrl+z停止:"<<endl;    while(cin>>q->data)    {        p->next=q;        p=q;        q=new cnode();    }}void clist::display(){    cnode *p;    p=head->next;    while(p)    {        cout<<p->data<<" ";        p=p->next;    }    cout<<endl;}clist::~clist(){    cnode *p;    while(head)    {        p=head->next;        delete head;        head=p;            }}int clist::getlin() const{    int length=0;    cnode* p=head->next;    while(p)    {        length++;        p=p->next;    }    return length;}bool clist::isempty()const{    return (head->next==NULL);}bool clist::find(const int e)const{    cnode* p=head->next;    while(p)    {        if(p->data==e)        return true;        p=p->next;    }    return false;}cnode* clist::getnode(int i)const{    if(i<0||i>getlin())    {        throw i;    }    cnode* p=head;    while(p&&i)    {        p=p->next;        i--;    }    return p;}void clist::insert(int i,const int e){    cnode* p;    cnode *node=new cnode();    node->data=e;    if(i==1)    {        node->next=head->next;        head->next=node;    }    else{        p=getnode(i-1);        if(i==getlin())            p->next=node;        else{            node->next=p->next;            p->next=node;        }    }}void clist::Delete(const int e){    if(!find(e))    {        cout<<"不包含啊"<<endl;        return ;     }    cnode* p=head;    cnode *q=head->next;    while(q)    {        if(q->data==e)        {            break;                    }        p=p->next;        q=q->next;            }    p->next=q->next;    return;}void clist::reverse(){    if(isempty())    {        cout<<"kongle"<<endl;    }    cnode *p,*q;    int len=getlin();    int i=1;    int j=len;    while(i<j)    {        p=getnode(i);        q=getnode(j);        int temp=p->data;        p->data=q->data;        q->data=temp;        ++i;        --j;    }}int main(){    clist* link=new clist();    link->create();    link->display();    cout<<link->getlin()<<endl;    cout<<link->isempty()<<endl;    cout<<link->find(3)<<endl;                     link->insert(1,888);      link->insert(3,999);      link->Delete(6);      link->display();       link->reverse();      link->display();       system("pause");      return 0;  }

 

c++單鏈表

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.