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

程序中的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;

    打赏 评论

相关推荐 更多相似问题