码迷,mamicode.com
首页 > 编程语言 > 详细

c++单链表

时间:2018-06-16 21:50:25      阅读:162      评论:0      收藏:0      [点我收藏+]

标签: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++单链表

标签:creat   ext   sys   const   data   mes   ctr   delete   spl   

原文地址:https://www.cnblogs.com/wpbing/p/9191429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!