回 帖 发 新 帖 刷新版面

主题:[求助]求大侠解释case跳出问题

#include<iostream>
#include<cstdio>
using namespace std;
class SLNote
{
    public:
        int data;
        SLNote* next;
        SLNote(SLNote* nextNote=NULL)
        {
            next=nextNote;
        }
        SLNote(const int& item,SLNote* nextNode=NULL)
        {
            data=item;next=nextNode;
        }
};
class SLList
{
    private:
        SLNote *head,*tail,*currptr;
        int size;
    public:
        SLList()
        {
            head=new SLNote();
            size=0;
        }
        ~SLList()
        {
            while(!IsEmpty())
            {
                currptr=head->next;
                head->next=currptr->next;
                delete currptr;
            }
            delete head;
        }
        bool IsEmpty()const
        {
            return head->next==NULL;
        }
        int length()
        {
            return size;
        }
        void create();
        void Find(int k);
        void Search(const int& item);
        void Delete(int k,int& item);
        void Insert(int k,const int& item);
        void Output();
};
void SLList::create()
{
    int c;
    SLNote *a,*b;
    a=head;
    cout<<"请输入数列(以#结束):";
    while(cin>>c&&c!='#')
    {
        b=new SLNote(c);
        a->next=b;
        a=b;
        size++;
    }
}
void SLList::Find(int k)
{
    SLNote *p;
    int i;
    if(k<1)
    {
        cout<<"存取位置不合法"<<endl;
        return;
    }
    p=head;
    i=0;
    while(p!=NULL&&i<k)
    {
        p=p->next;
        i++;
    }
    if(p==NULL)
        cout<<"无此结点"<<endl;
    else
        cout<<"该节点的值为:"<<p->data<<endl;
}
void SLList::Search(const int& item)
{
    SLNote *p;
    int i;
    p=head->next;
    i=1;
    while(p!=NULL&&p->data!=item)
    {
        p=p->next;
        i++;
    }
    if(p!=NULL)
        cout<<"该结点在"<<i<<"位置"<<endl;
    else
        cout<<"不存在该结点"<<endl;
}
void SLList::Delete(int k,int& item)
{
    SLNote *p,*q;
    int i;
    if(k<1)
    {
        cout<<"删除不合法"<<endl;
        return;
    }
    p=head;i=1;
    while(p!=NULL&&i<k)
    {
        p=p->next;
        i++;
    }
    if(p==NULL)
    {
        cout<<"无此结点"<<endl;
        return;
    }
    q=p->next;
    p->next=q->next;
    item=q->data;
    delete q;
}
void SLList::Insert(int k,const int& item)
{
    SLNote *p,*s;
    int i;
    if(k<0)
    {
        cout<<"插入不合法"<<endl;
        return;
    }
    p=head;
    i=0;
    while(p!=NULL&&i<k)
    {
        p=p->next;
        i++;
    }
    if(p==NULL)
    {
        cout<<"插入不合法"<<endl;
        return;
    }
    s=new SLNote;
    s->data=item;
    s->next=p->next;
    p->next=s;
}
void SLList::Output()
{
    SLNote *p;
    p=head->next;
    while(p!=NULL)
    {
        cout<<p->data<<' ';
        p=p->next;
    }
    cout<<endl;
}
int main()
{
    printf("|---------------------------|\n");
    printf("|        1、创建操作        |\n");
    printf("|        2、存取操作        |\n");
    printf("|        3、查找操作        |\n");
    printf("|        4、插入操作        |\n");
    printf("|        5、删除操作        |\n");
    printf("|        0、退出            |\n");
    printf("|---------------------------|\n");
    int a,t,k;
    SLList wy;
    while(cin>>a)
    {
        switch(a)
        {
            case 0:
                return 0;
            case 1:
                wy.create();
                cout<<"创建成功"<<endl;
                wy.Output();
                break;
            case 2:
                cout<<"要存取的结点位置为:";
                cin>>k;
                wy.Find(k);
                wy.Output();
                break;
            case 3:
                cout<<"要查找的结点为:";
                cin>>k;
                wy.Search(k);
                wy.Output();
                break;
            case 4:
                cout<<"要插入的数据:";
                cin>>k;
                cout<<"要插入的位置";
                cin>>t;
                wy.Insert(k,t);
                wy.Output();
                break;
            case 5:
                cout<<"要删除结点的位置:";
                cin>>k;
                wy.Delete(k,t);
                cout<<"已删除结点:"<<t<<endl;
                wy.Output();
                break;
            default:
                cout<<"输入错误请重新输入"<<endl;
        }
    }
}
编了一个单链表测试程序,但是选择case1,执行完创建单链表就自动结束了程序,求大虾,求解释[em10][em10]

回复列表 (共1个回复)

沙发

void SLList::create()
{
    int c;
    SLNote *a,*b;
    a=head;
    cout<<"请输入数列(以#结束):";
    while(cin>>c&&c!='#')
    {
        b=new SLNote(c);
        a->next=b;
        a=b;
        size++;
    }
    if(cin.fail())
    {
        cin.clear();
        cin.sync();
    }
}

我来回复

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