案件没有执行

时间:2018-04-19 07:44:37

标签: c++ list graph

我有以下代码。问题是,如果我选择一个选项,例如选项#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 check\n";
        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 node\n";
        cout<<"5. Determine the number of occurrences  in a list\n";
        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;
}

1 个答案:

答案 0 :(得分:2)

sort(start);

失败,因为startNULL,这意味着pNULL

pnode p = start;

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

while (p->next != NULL)