Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于变量C的链表排序_C_List_Sorting_Linked List - Fatal编程技术网

基于变量C的链表排序

基于变量C的链表排序,c,list,sorting,linked-list,C,List,Sorting,Linked List,我正在尝试创建一个调查数据库,其中一个链表存储基于其变量之一的调查。当我对调查进行排序时,它只将排序的节点放在第一个节点之后(头部可能是999,仍然是第一个节点),而且我还看到在添加元素然后显示所有元素时,偶尔会无限打印元素 Sample Code: void addElement(struct listelement** head_ptr) { int data; int inputPPS; struct listelement *temp; struct

我正在尝试创建一个调查数据库,其中一个链表存储基于其变量之一的调查。当我对调查进行排序时,它只将排序的节点放在第一个节点之后(头部可能是999,仍然是第一个节点),而且我还看到在添加元素然后显示所有元素时,偶尔会无限打印元素

Sample Code:


void addElement(struct listelement** head_ptr)
{
    int data;
    int inputPPS;
    struct listelement *temp;
    struct listelement *newNode;



    if (*head_ptr == NULL)
    {

        addElement_AtStart(head_ptr);

    }

    else

    {

        temp = *head_ptr;
        newNode = (struct listelement*)malloc(sizeof(struct listelement));

        printf("\nPlease enter your PPS number (Number must be unique)\n");
        scanf("%d", &inputPPS);
        if (checkUnique(head_ptr, inputPPS) == 1) {

            newNode->surveyDetails.ppsNo = inputPPS;
            printf("\nPlease enter your first name:");
            scanf("%s", newNode->surveyDetails.fName);
            printf("\nPlease enter your last name:");
            scanf("%s", newNode->surveyDetails.lName);
            //printf("\nEnter email address: ");
            //do email validation
            //scanf("%s", newNode->studentData.email);

            printf("\nEnter current address: ");
            scanf(" %s", newNode->surveyDetails.address);//takes in the next 99 characters until a newline is found

            printf("\nPlease enter your :");
            scanf("%d", &newNode->surveyDetails.age);
            printf("\nPlease enter your yearly salary (as whole number):");
            scanf("%d", &newNode->surveyDetails.income);
            printf("\nHow many cigarrettes do you smoke a day? :");
            scanf("%d", &newNode->surveyDetails.ciggiesSmoked);
            printf("\nHow many units of alcohol do you drink in a day? :");
            scanf("%d", &newNode->surveyDetails.unitsTaken);
            printf("\nHow many time do you exercise every week? :");
            scanf("%d", &newNode->surveyDetails.timesExercised);


            while (temp != NULL)
            {
                if (sortedInsert(&head_ptr, newNode) == 1) {
                    printf("\nSurvey stored successfully\n");
                    break;
                }
                temp = temp->next;

            }

            //printf("\nSurvey didnt work successfully\n");


            //do sorted insert
        }
        else {//if pps is not unique recursively start function again prompting user
            printf("\nWe still value your feedback on this topic! If you believe you have entered your PPS incorrectly you can now try again!");
            addElement(head_ptr);
        }

    }
}

void addElement_AtStart(struct listelement** head_ptr)
{

    struct listelement *newNode;
    int inputPPS;

    newNode = (struct listelement*)malloc(sizeof(struct listelement));

    printf("\nWe will now take details from you for the survery...\n");
    printf("\nPlease enter your PPS number (Number must be unique)\n");
    scanf("%d", &inputPPS);
    if (checkUnique(head_ptr, inputPPS) == 1) {
        newNode->surveyDetails.ppsNo = inputPPS;
        //do unique check
        printf("\nPlease enter your first name:");
        scanf("%s", newNode->surveyDetails.fName);
        printf("\nPlease enter your last name:");
        scanf("%s", newNode->surveyDetails.lName);
        printf("\nEnter email address: ");
        //do email validation
        //scanf("%s", newNode->studentData.email);

        printf("\nEnter current address: ");
        scanf(" %s", newNode->surveyDetails.address);//takes in the next 99 characters until a newline is found

        printf("\nPlease enter your age:");
        scanf("%d", &newNode->surveyDetails.age);
        printf("\nPlease enter your yearly salary (as whole number):");
        scanf("%d", &newNode->surveyDetails.income);
        printf("\nHow many cigarrettes do you smoke a day? :");
        scanf("%d", &newNode->surveyDetails.ciggiesSmoked);
        printf("\nHow many units of alcohol do you drink in a day? :");
        scanf("%d", &newNode->surveyDetails.unitsTaken);
        printf("\nHow many time do you exercise every week? :");
        scanf("%d", &newNode->surveyDetails.timesExercised);
    }
    else {//if pps is not unique recursively start function again prompting user
        printf("\nWe still value your feedback on this topic! If you believe you have entered your PPS incorrectly you can now try again!");
        addElement_AtStart(head_ptr);
    }


    //no sorted insert if file has surveys sorting will be done in addElement

    newNode->next = *head_ptr;

    *head_ptr = newNode; // transfer the address of newNode' to'head'

}
int sortedInsert(struct listelement** head_ref, struct listelement* newNode)
{
    struct listelement* temp;
    /* Special case for the head end */
    if (*head_ref == NULL || (*head_ref)->surveyDetails.ppsNo >= newNode->surveyDetails.ppsNo)
    {
        newNode->next = *head_ref;
        *head_ref = newNode;
        printf("At head");
        return 1;
    }
    else
    {
        /* Locate the node before the point of insertion */
        temp = *head_ref;
        while (temp->next != NULL &&
            temp->next->surveyDetails.ppsNo < newNode->surveyDetails.ppsNo)
        {
            temp = temp->next;
        }
        newNode->next = temp->next;
        temp->next = newNode;
        return 1;
    }
    printf("failed");
}
int checkUnique(struct listelement *head_ptr, int inputPPS) {
    int nodeNum = 0;
    struct listelement *temp;
    temp = head_ptr;



    while (temp != NULL)
    {
        if (nodesAdded == 0) {
            nodesAdded++;
            printf("\n First node so is unique");
            return 1;
        }
        if (temp->surveyDetails.ppsNo== inputPPS )
        {
            printf("\n There is a user in the survey system with this PPS Number.");
            return 0;
        }
        nodeNum++;
        temp = temp->next;
    }
    printf("\nPPS Number is unique. Continuing...\n We will now ask you for your survey details... ");

    nodesAdded++;
    return 1;

}
示例代码:
void addElement(结构列表元素**head\ptr)
{
int数据;
int-inputPPS;
结构列表元素*temp;
结构listelement*newNode;
如果(*head_ptr==NULL)
{
开始时的附加元件(头部ptr);
}
其他的
{
温度=*压头温度;
newNode=(struct listelement*)malloc(sizeof(struct listelement));
printf(“\n请输入您的PPS号码(号码必须唯一)\n”);
scanf(“%d”、&inputPPS);
如果(检查唯一性(头部ptr,输入PPS)==1){
newNode->surveydeails.ppsNo=inputPPS;
printf(“\n请输入您的名字:”);
scanf(“%s”,newNode->surveydeails.fName);
printf(“\n请输入您的姓氏:”;
scanf(“%s”,newNode->surveyDetails.lName);
//printf(“\n输入电子邮件地址:”);
//进行电子邮件验证
//scanf(“%s”,newNode->studentData.email);
printf(“\n输入当前地址:”);
scanf(“%s”,newNode->surveydeails.address);//接收接下来的99个字符,直到找到换行符为止
printf(“\n请输入您的:”;
scanf(“%d”,&newNode->surveydeails.age);
printf(“\n请输入您的年薪(整数):”;
scanf(“%d”,&newNode->surveydeails.income);
printf(“\n你一天抽多少支雪茄?:”;
scanf(“%d”,&newNode->surveydeails.ciggiesmoked);
printf(“\n您一天喝多少单位的酒?:”;
scanf(“%d”,&newNode->surveydeails.unitsTaken);
printf(“\n您每周锻炼多少次?:”;
scanf(“%d”,&newNode->surveydeails.times);
while(temp!=NULL)
{
if(分拣机(&head_ptr,newNode)==1){
printf(“\n保存成功\n”);
打破
}
温度=温度->下一步;
}
//printf(“\n保护未成功\n”);
//进行排序插入
}
else{//如果pps不是唯一的,则再次递归启动函数,提示用户
printf(“\n我们仍然重视您对此主题的反馈!如果您认为您输入的PPS不正确,现在可以重试!”);
附录(总图ptr);
}
}
}
开始时无效添加元素(结构列表元素**头)
{
结构listelement*newNode;
int-inputPPS;
newNode=(struct listelement*)malloc(sizeof(struct listelement));
printf(“\n我们现在将从您处获取详细信息,以便进行调查…\n”);
printf(“\n请输入您的PPS号码(号码必须唯一)\n”);
scanf(“%d”、&inputPPS);
如果(检查唯一性(头部ptr,输入PPS)==1){
newNode->surveydeails.ppsNo=inputPPS;
//进行唯一检查
printf(“\n请输入您的名字:”);
scanf(“%s”,newNode->surveydeails.fName);
printf(“\n请输入您的姓氏:”;
scanf(“%s”,newNode->surveyDetails.lName);
printf(“\n输入电子邮件地址:”);
//进行电子邮件验证
//scanf(“%s”,newNode->studentData.email);
printf(“\n输入当前地址:”);
scanf(“%s”,newNode->surveydeails.address);//接收接下来的99个字符,直到找到换行符为止
printf(“\n请输入您的年龄:”;
scanf(“%d”,&newNode->surveydeails.age);
printf(“\n请输入您的年薪(整数):”;
scanf(“%d”,&newNode->surveydeails.income);
printf(“\n你一天抽多少支雪茄?:”;
scanf(“%d”,&newNode->surveydeails.ciggiesmoked);
printf(“\n您一天喝多少单位的酒?:”;
scanf(“%d”,&newNode->surveydeails.unitsTaken);
printf(“\n您每周锻炼多少次?:”;
scanf(“%d”,&newNode->surveydeails.times);
}
else{//如果pps不是唯一的,则再次递归启动函数,提示用户
printf(“\n我们仍然重视您对此主题的反馈!如果您认为您输入的PPS不正确,现在可以重试!”);
开始时的附加元件(头部ptr);
}
//如果文件有调查,则不进行排序插入。将在addElement中进行排序
新建节点->下一步=*头\u ptr;
*head\u ptr=newNode;//将newNode'的地址传输到'head'
}
int sortedInsert(结构列表元素**头\u参考,结构列表元素*新节点)
{
结构列表元素*temp;
/*头端的特殊情况*/
if(*head_ref==NULL ||(*head_ref)->surveydeails.ppsNo>=newNode->surveydeails.ppsNo)
{
newNode->next=*head\u ref;
*head_ref=新节点;
printf(“头部”);
返回1;
}
其他的
{
/*在插入点之前定位节点*/
温度=*水头参考;
while(临时->下一步!=空&&
temp->next->surveydeails.ppsNosurveydeails.ppsNo)
{
温度=温度->下一步;
}
新建节点->下一步=临时->下一步;
temp->next=newNode;
返回1;
}
printf(“失败”);
}
int checkUnique(结构listelement*head\u ptr,int inputPPS){
int nodeNum=0;
结构列表元素*temp;
温度=压头温度;
while(temp!=NULL)
{
if(nodesAdded==0){
nodesAdded++;
printf(“\n第一个节点是唯一的”);
返回1;
}
if(temp->surveydeails.ppsNo==inputPPS)
{
printf(“\n调查系统中有一个用户具有此PPS编号。”);
void addElement(struct listelement** head_ptr)
{
    int data;
    int inputPPS;
    struct listelement *temp;
    struct listelement *newNode;



    if (*head_ptr == NULL)
    {

        addElement_AtStart(head_ptr);

    }

    else

    {

        temp = *head_ptr;
        newNode = (struct listelement*)malloc(sizeof(struct listelement));

        printf("\nPlease enter your PPS number (Number must be unique)\n");
        scanf("%d", &inputPPS);
        if (checkUnique(&head_ptr, inputPPS) == 1) {

            newNode->surveyDetails.ppsNo = inputPPS;
            printf("\nPlease enter your first name:");
            scanf("%s", newNode->surveyDetails.fName);
            printf("\nPlease enter your last name:");
            scanf("%s", newNode->surveyDetails.lName);
            //printf("\nEnter email address: ");
            //do email validation
            //scanf("%s", newNode->studentData.email);

            printf("\nEnter current address: ");
            scanf(" %s", newNode->surveyDetails.address);//takes in the next 99 characters until a newline is found

            printf("\nPlease enter your :");
            scanf("%d", &newNode->surveyDetails.age);
            printf("\nPlease enter your yearly salary (as whole number):");
            scanf("%d", &newNode->surveyDetails.income);
            printf("\nHow many cigarrettes do you smoke a day? :");
            scanf("%d", &newNode->surveyDetails.ciggiesSmoked);
            printf("\nHow many units of alcohol do you drink in a day? :");
            scanf("%d", &newNode->surveyDetails.unitsTaken);
            printf("\nHow many time do you exercise every week? :");
            scanf("%d", &newNode->surveyDetails.timesExercised);


            while (temp != NULL)
            {
                if (sortedInsert(head_ptr, newNode) == 1) {
                    printf("\nSurvey stored successfully\n");
                    break;
                }
                temp = temp->next;

            }

            //printf("\nSurvey didnt work successfully\n");


            //do sorted insert

        }
        else if (checkUnique(&head_ptr, inputPPS) == 0){//if pps is not unique recursively start function again prompting user
            printf("\nWe still value your feedback on this topic! If you believe you have entered your PPS incorrectly you can now try again!");
            free(newNode);
        }

    }
}

void addElement_AtStart(struct listelement** head_ptr)
{

    struct listelement *newNode;
    int inputPPS;

    newNode = (struct listelement*)malloc(sizeof(struct listelement));

    printf("\nWe will now take details from you for the survery...\n");
    printf("\nPlease enter your PPS number (Number must be unique)\n");
    scanf("%d", &inputPPS);
    if (checkUnique(head_ptr, inputPPS) == 1) {
        newNode->surveyDetails.ppsNo = inputPPS;
        //do unique check
        printf("\nPlease enter your first name:");
        scanf("%s", newNode->surveyDetails.fName);
        printf("\nPlease enter your last name:");
        scanf("%s", newNode->surveyDetails.lName);
        printf("\nEnter email address: ");
        //do email validation
        //scanf("%s", newNode->studentData.email);

        printf("\nEnter current address: ");
        scanf(" %s", newNode->surveyDetails.address);//takes in the next 99 characters until a newline is found

        printf("\nPlease enter your age:");
        scanf("%d", &newNode->surveyDetails.age);
        printf("\nPlease enter your yearly salary (as whole number):");
        scanf("%d", &newNode->surveyDetails.income);
        printf("\nHow many cigarrettes do you smoke a day? :");
        scanf("%d", &newNode->surveyDetails.ciggiesSmoked);
        printf("\nHow many units of alcohol do you drink in a day? :");
        scanf("%d", &newNode->surveyDetails.unitsTaken);
        printf("\nHow many time do you exercise every week? :");
        scanf("%d", &newNode->surveyDetails.timesExercised);
    }
    else {//if pps is not unique recursively start function again prompting user

        printf("\nWe still value your feedback on this topic! If you believe you have entered your PPS incorrectly you can now try again!");
        free(newNode);

    }


    //no sorted insert if file has surveys sorting will be done in addElement

    newNode->next = *head_ptr;

    *head_ptr = newNode; // transfer the address of newNode' to'head'


    free(newNode);
}
int sortedInsert(struct listelement** head_ref, struct listelement* newNode)
{
    struct listelement* temp;
    /* Special case for the head end */
    if (*head_ref == NULL || (*head_ref)->surveyDetails.ppsNo >= newNode->surveyDetails.ppsNo)
    {
        newNode->next = *head_ref;
        *head_ref = newNode;
        printf("At head");
        return 1;
    }
    else
    {
        /* Locate the node before the point of insertion */
        temp = *head_ref;
        while (temp->next != NULL &&
            temp->next->surveyDetails.ppsNo < newNode->surveyDetails.ppsNo)
        {
            temp = temp->next;
        }
        newNode->next = temp->next;
        temp->next = newNode;
        return 1;
    }
    printf("failed");
}
int checkUnique(struct listelement *head_ptr, int inputPPS) {
    int nodeNum = 0;
    struct listelement *temp;
    temp = head_ptr;



    while (temp != NULL)
    {
        if (nodesAdded == 0) {
            nodesAdded++;
            printf("\n First node so is unique");
            return 1;
        }
        if (temp->surveyDetails.ppsNo== inputPPS )
        {
            printf("\n There is a user in the survey system with this PPS Number.");
            return 0;
        }
        nodeNum++;
        temp = temp->next;
    }
    printf("\nPPS Number is unique. Continuing...\n We will now ask you for your survey details... ");

    nodesAdded++;
    return 1;

}