回 帖 发 新 帖 刷新版面

主题:看看我建的链表怎么这么多的错误呀?

#include <iostream>
using namespace std;
class element
{
private:
    int data;
    element *next;
public:
    void create(int n,element *top);//建立
    void show(element *top);//显示
    void input(element *top);//插入
    void cut(element *top);//删除
    void find(element *top);//查找
};
void element::create(int n,element *top)//  建立一个链表
{
    cout<<"the create program is beginning:"<<endl;
    element *L=NULL,*p=top;
    cout<<"please input "<<n<<" intage numbers:"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>L.data;
        L->next=NULL;
        p->next=L;
        p=L;
    }
    cout<<"the create program is ending"<<endl;
}    
void element::show(element *top)//显示链表的内容
{
    cout<<"the show program is beginning:"<<endl;
    while(top->next)
    {
        cout<<top->next->data;
        top->next=top->next->next;
    }
    cout<<"the show program is ending"<<endl;
}
void element::input(element *top)//插入数据
{
    cout<<"the input program is beginning:"<<endl;
    int e;
    cout<<"please input e(the location you want to input):";
    cin>>e;
    element *p=top;
    for(int i=0;i<e-2;i++)
    {
        *p->next=*p->next->next;
    }
    element *L=new element();
    cout<<"please input the number you want to input:";
    cin>>L.data;
    L->next=p->next;
    p->next=L;
    cout<<"the input program is ending"<<endl;
}
void element::cut(element *top)//删除链表
{
    cout<<"the cut program is beginning:"<<endl;
    while(top->next)
    {
        element *L=top;
        top->next=L->next;
        delete L;
    }
    cout<<"the cut program is ending"<<endl;
}
void element::find(element *top)//查找数据
{
    cout<<"the find program is beginning:"<<endl;
    int i,e;
    cout<<"please inut e(the location of number you want to find):";
    cin>>e;
    element *p=top;
    for)i=0;i<e-1;i++)
    {
        p->next=p->next->next;
        cout<<p.data<<endl;
    }
    cout<<"the find program is ending"<<endl;
}

int main()
{
    element *top=new element();
    element k=new element();
    int n;
    cout<<"please input n:";
    cin>>n;
    k.create(n,top);
    k.show(top);
    k.input(top);
    k.show(top);
    k.find(top);
    k.show(top);
    k.cut(top);
    k.show(top);
    return 0;
}

回复列表 (共1个回复)

沙发

我刚刚又改写了一下程序,现在没有错误了,可是它会在执行的时候出错,不知道为什么?请指教!!
#include <iostream>
using namespace std;
class element
{
private:
    int data;
    element *next;
public:
    void create(int n,element *top);//建立
    void show(element *top);//显示
    void input(element *top);//插入
    void cut(element *top);//删除
    void find(element *top);//查找
};
void element::create(int n,element *top)//  建立一个链表
{
    cout<<"the create program is beginning:"<<endl;
    element *L=NULL,*p=top;
    cout<<"please input "<<n<<" intage numbers:"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>L->data;
        L->next=NULL;
        p->next=L;
        p=L;
    }
    cout<<"the create program is ending"<<endl;
}
    
void element::show(element *top)//显示链表的内容
{
    cout<<"the show program is beginning:"<<endl;
    while(top->next)
    {
        cout<<top->next->data;
        top->next=top->next->next;
    }
    cout<<"the show program is ending"<<endl;
}
void element::input(element *top)//插入数据
{
    cout<<"the input program is beginning:"<<endl;
    int e;
    cout<<"please input e(the location you want to input):";
    cin>>e;
    element *p=top;
    for(int i=0;i<e-2;i++)
    {
        *p->next=*p->next->next;
    }
    element *L=new element();
    cout<<"please input the number you want to input:";
    cin>>L->data;
    L->next=p->next;
    p->next=L;
    cout<<"the input program is ending"<<endl;
}
void element::cut(element *top)//删除链表
{
    cout<<"the cut program is beginning:"<<endl;
    while(top->next)
    {
        element *L=top;
        top->next=L->next;
        delete L;
    }
    cout<<"the cut program is ending"<<endl;
}
void element::find(element *top)//查找数据
{
    cout<<"the find program is beginning:"<<endl;
    int i,e;
    cout<<"please inut e(the location of number you want to find):";
    cin>>e;
    element *p=top;
    for(i=0;i<e-1;i++)
    {
        p->next=p->next->next;
        cout<<p->data<<endl;
    }
    cout<<"the find program is ending"<<endl;
}

int main()
{
    element *top=new element();
    element *k=new element();
    int n;
    cout<<"please input n:";
    cin>>n;
    k->create(n,top);
    k->show(top);
    k->input(top);
    k->show(top);
    k->find(top);
    k->show(top);
    k->cut(top);
    k->show(top);
    return 0;
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册