案例未执行

Case not executing

本文关键字:执行 案例      更新时间:2023-10-16

我有以下代码。问题是,如果我选择一个选项,例如选项 #2 对它正在关闭的程序的元素进行排序,我不明白为什么......我正在使用Eclipse IDE。这些函数执行以下操作:

a( 在列表中插入不允许重复值的数据的函数(如果值允许重复值(不存在,粘贴发生在列表的开头(。有效负载元素为整数。

b( 在列表中插入数据以使列表保持升序排序的函数。有效载荷元素为整数

c( 确定可被 z 值整除的元素数量的函数,接收为参数。 d( 确定大于来自列表的第一个节点。

e( 确定列表中给定值的出现次数的函数。

#include<iostream>
using namespace std;
char menu()
{  char choice;
        cout<<"********Choose an option********* n" ;
        cout<<"1. duplicate checkn";
        cout<<"2. Sort the elements n";
        cout<<"3. Determine elements divisible by a value given Z n";
        cout<<"4. Determine the number of elements greater than the first noden";
        cout<<"5. Determine the number of occurrences  in a listn";
        cout<<"6. -----Exit-----n";
    cin>>choice;
    return choice;
}


struct node
{
    int value;
    node* prev;
    node* next;
};
typedef node* pnode;
int nro=0;
void showlist(pnode start, int div)
{
    nro=0;
    if(start!=NULL)
    {
        while(start!=NULL)
        {
            if(start->value%div==0)
            {
                nro++;
                cout<<start->value<<" ";
            }
            start=start->next;
        }
    }
}
void checkvalue(pnode start, int nr)
{
    nro=0;
    if(start!=NULL)
    {
        while(start!=NULL)
        {
            if(start->value==nr)
            {
                nro++;
            }
            start=start->next;
        }
    }
}
bool checkduplicates(pnode start, int val)
{
    if(start==NULL) return false;
    else
    {
        while(start!=NULL)
        {
            if(start->value==val) return true;
            start=start->next;
            return false;
        }
    }
}

void sort(pnode start)
{
    pnode p=start;
    pnode q=NULL;
    while(p->next!=NULL)
    {
        q=p->next;
        while(q!=NULL)
        {
            if(p->value > q->value)
            {
                int aux;
                aux=p->value;
                p->value=q->value;
                q->value=aux;
            }
            q=q->next;
        }
        p=p->next;
    }
}
void showbig(pnode start)
{
    pnode q=start->next;
    while(q!=NULL)
    {
        if(q->value > start->value)
        {
            cout<<q->value<<" ";
        }
        q=q->next;
    }
}
int main()
{
    pnode start=NULL;
    pnode end=NULL;
    pnode aux=NULL;
    char choice;
    int number;

         do{
    choice=menu();
    switch(choice)
    {
        case '1':
            int z;
            cout<<"Value: ";
            cin>>z;

                if(start==NULL)
                {
                    start = new node;
                    start->value=z;
                    start->next=NULL;
                    start->prev=NULL;
                    end=start;
                    aux=start;
                }
                else
                {
                    aux= new node;
                    aux->value=z;
                    aux->next=start;
                    start->prev=aux;
                    aux->prev=NULL;
                    start=aux;
                }
                if (!checkduplicates(start,z))
            {cout<<"Duplicate value. Cannot insert.n";
            break;}
                break;
            case '2':

                 sort(start);

            break;
            case '3':
            cout<<"Value: ";
            cin>>z;
            showlist(start,z);
            if(nro==0) cout<<"No values found.n";
            else cout<<"n";
            break;

        case '4':
            showbig(start);
            cout<<"n";
        break;
        case '5':

            cout<<"Value: ";
            cin>>z;
            checkvalue(start,z);
            if(nro==0) cout<<"No values found.n";
            else cout<<nro<<"n";
        break;

    default: cout<<"Exit n";
}

} while (choice !='6');
    return 0;
}
sort(start);

失败,因为start NULL这意味着p NULL

pnode p = start;

您尝试在此处取消引用 NULL 指针

while (p->next != NULL)