UCaoo 2020-09-30 16:45 采纳率: 100%
浏览 86
已采纳

程序中的while陷入了死循环,我觉得是链表指针的问题,但是我找不出来,求大佬帮助

C++

图片说明


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
typedef int status;

#define ERROR 0
#define OK 1
#define stu "C:\\Users\\Administrator\\Desktop\\student\\stu.txt" 

//structure of student
typedef struct Student
{
    char no[8];
    char name[20];
    int score;
}Student;


//definition of linked list
typedef struct LNode
{
    Student data;
    struct LNode *next;
}LNode, *LinkList;



//create list (formal)
LNode * CreateList(LinkList &L, int sum)
{

        p = new LNode;
        while(p != NULL && count < sum)
        {
            printf("Please enter the Sno:\n");
            scanf("%s",&((p->data).no));
            printf("Please enter the name:\n");
            scanf("%s",&((p->data).name));
            printf("Please enter the score:\n");
            scanf("%d",&((p->data).score));

            p->next = L->next;
            L->next = p;
            count++;

            while(getchar()!= '\n')
                continue;
        } 
}


//sort
status SortElem(LinkList &L)
{
    if(L == NULL || L->next == NULL) return ERROR;
    struct LNode *pstart;
    struct LNode *head;
    struct LNode *minNode;
    struct LNode *p;
    pstart = new LNode;
    p = new LNode; 
    pstart->next = L;
    head = pstart;

    printf("sno:%s\nname:%s\nscore:%d\n",(L->next->data).no, (L->next->data).name, (L->next->data).score);

    while(head->next != NULL)
    {
        printf("sno:%s\nname:%s\nscore:%d\n",(L->next->data).no, (L->next->data).name, (L->next->data).score);
        minNode = head->next, p = head->next->next;
        while(p->next != NULL)  //这个循环出错了
        {
            printf("sno:%s\nname:%s\nscore:%d\n",(L->next->data).no, (L->next->data).name, (L->next->data).score);
            if((p->data).score < (minNode->data).score)
                minNode = p;
            p = p->next;
        }
        swap(minNode->data, head->next->data);
        head = head->next;
     } 
     L = pstart->next;

     p = L;
     while(p != NULL)
     {
        printf("sno:%s\nname:%s\nscore:%d\n",(p->data).no,(p->data).name,(p->data).score);
        p = p->next;
      } 

     delete pstart;
}

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-09-30 21:54
    关注

    两处
    p = new LNode;
    后面加上
    p->next = NULL;

    pstart = new LNode;
    后也加上
    pstart->next = NULL;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog