简体   繁体   English

列表中的学生 ID 未打印出来

[英]Student ids in a list not getting printed out

I'm trying to print out an unsorted, then sorted list of student IDs after the user enters them in. It prints an empty list fine, but when there are actual ids to print, it just goes blank.我试图在用户输入学生 ID 后打印出一个未排序的排序列表。它可以很好地打印一个空列表,但是当有实际的 ID 需要打印时,它就会变成空白。 Why is this happening?为什么会这样? I can only get an empty list to work right but not a list with anything in it.我只能得到一个空列表才能正常工作,但不能得到一个包含任何内容的列表。


#include <stdio.h>  /* printf, scanf                                  */
#include <stdlib.h> /* malloc, exit, free                             */
#include <ctype.h>  /* typedef, tolower                               */

/**********************************************************************/
/*                          Symbolic Constants                        */
/**********************************************************************/

#define MAXIMUM_ID        999999  /* Maximum student id number        */
#define MINIMUM_ID        1       /* Minimum student id number        */
#define LIST_HEADER       MINIMUM_ID - 1
                                  /* Header of the student id list    */
#define LIST_TRAILER      MAXIMUM_ID + 1 
                                  /* Trailer of the student id list   */
#define HEADER_ALLOC_ERR  1       /* Error allocating the list header */
                                  /* memory                           */
#define TRAILER_ALLOC_ERR 2       /* Error allocating the list        */
                                  /* trailer memory                   */
#define ID_ALLOC_ERR      3       /* Error allocating the student id  */
                                  /* memory                           */
#define QUIT              0       /* Program exit value               */

/**********************************************************************/
/*                          Program Structures                        */
/**********************************************************************/
/* A school student id list                                           */
struct student_id
{
   int               student_id;      /* A student's id number        */
   struct student_id *p_next_student; /* Points to next student id    */
};
typedef struct student_id ID;

/**********************************************************************/
/*                          Function Prototypes                       */
/**********************************************************************/
void print_heading();
   /* Print the program heading                                       */
void print_instructions();
   /* Print the program instructions                                  */
char get_response();
   /* Get the response to continue or quit                            */
ID *create_empty_list();
   /* Create an empty list                                            */
void insert_student(ID *p_id_list, int student_id);
   /* Insert a student at the front of the list                       */
void print_list(ID *p_id_list);
   /* Print all the student id's in the list                          */
int get_count(ID *p_id_list);
   /* Get the count of the students on the list                       */
void sort_list(ID *p_student_id);
   /* Sort the students into ascending order by id                    */
void remove_duplicates(ID *p_id_list);
   /* Remove all duplicate id's                                       */

/**********************************************************************/
/*                             Main Function                          */
/**********************************************************************/
int main()
{
   ID  *p_id_list,   /* Points to the student id list                 */
       *p_next_list; /* Points to each student id in the list         */
   int  student_id;  /* A student's id number                         */

   /* Loop processing student id list until the user says to quit     */
   while(print_instructions(), get_response() == 'y')
   {
      /* Create an empty list of student IDs                          */
      p_id_list = create_empty_list();

      /* Loop processing inserted student ids until the user says     */
      /* to quit                                                      */
      while(printf("Enter the next student id (%d to quit): ", QUIT),
            scanf ("%d", &student_id), student_id > LIST_HEADER)
      {
         if(student_id > MAXIMUM_ID)
         {
            printf("    Id rejected - it cannot exceed %d", MAXIMUM_ID);
            printf("\n");
         }
         else
            insert_student(p_id_list, student_id);
      }

      /* Print the list of unsorted student ID's and show the number  */
      /* of student IDs                                               */
      printf("\nThe unsorted student ID list, as entered, is: ");
      print_list(p_id_list);
      printf("\nThe length of the unsorted student ID list is: %d", 
         get_count(p_id_list));

      /* Print the list of sorted student ID's and show the number of */
      /* student IDs                                                  */
      sort_list(p_id_list);
      printf("\n\nThe student id list, sorted ascending by id, is:");
      print_list(p_id_list);
      printf("\nThe length of the sorted student ID list is: %d", 
         get_count(p_id_list));

      /* Delete duplicate student IDs, print the student id list,     */
      /* show the number of student ids, and free the id list         */
      printf("\n");
      remove_duplicates(p_id_list);
      printf("\nThe student id list, with duplicate id's removed,");
      printf("sorted ascending by id, is:");
      print_list(p_id_list); 
      printf("\nThe length of the cleared and sorted student"); 
      printf("ID list is: %d", get_count(p_id_list));

      while(p_id_list != NULL)
      {
         p_next_list = p_id_list->p_next_student;
         free(p_id_list);
         p_id_list = p_next_list;
      }
   }

   /* Print a goodbye message and terminate the program               */
   printf("\n\nThanks for using this program. Have a great day! :D");
   printf("\n\n\n\n\n\n");
   return 0;
}

/**********************************************************************/
/*                   Print the program instructions                   */
/**********************************************************************/
void print_instructions()
{
   printf("\n\n\n  This program maintains a list of elements by their");
   printf(    "\n  id number.  After entry of all id's, it sorts them");
   printf(    "\n  into ascending order, then removes all duplicates.");
   return;
}

/**********************************************************************/
/*                Get the response to continue or quit                */
/**********************************************************************/
char get_response()
{
   char response[2]; /* Response to continue or quit                  */

   printf("\n");
   do
   {
      printf("\nDo you want to enter another student id list (y or n): ");
      scanf ("%1s", &response[0]);
      response[0] = tolower(response[0]);
   }
   while(response[0] != 'y' && response[0] != 'n');

   return (response[0]);
}

/**********************************************************************/
/*                        Create an empty list                        */
/**********************************************************************/
ID *create_empty_list()
{
   ID *p_new_list; /* List of student ID's                            */

   if((p_new_list = (ID *)malloc(sizeof(ID))) == NULL)
   {
      printf("\n\nError #%d occurred in create_empty_list",
                                                      HEADER_ALLOC_ERR);
       printf(  "\nUnable to allocate memory for the list header.");
       printf(  "\nThe program is aborting.");
       exit  (HEADER_ALLOC_ERR);
   }
   p_new_list->student_id = LIST_HEADER;

   if((p_new_list->p_next_student = (ID *)malloc(sizeof(ID))) == NULL)
   {
      printf("\n\nError #%d occurred in create_empty_list",
                                                     TRAILER_ALLOC_ERR);
      printf(  "\nUnable to allocate memory for the list header.");
       printf(  "\nThe program is aborting.");
       exit  (TRAILER_ALLOC_ERR);
   }
   p_new_list->p_next_student->student_id = LIST_TRAILER;
   p_new_list->p_next_student->p_next_student = NULL;

   return p_new_list;
}

/**********************************************************************/
/*             Insert a student at the front of the list              */
/**********************************************************************/
void insert_student(ID *p_id_list, int insert_id)
{
   ID *p_current_id = p_id_list->p_next_student,
      *p_new_list,  /* Points to the new id list                      */
      *p_previous_id = p_id_list;

   while(

   if((p_new_list = (ID *)malloc(sizeof(ID))) == NULL)
   {
      printf("\n\nError #%d occurred in insert_student", ID_ALLOC_ERR);
       printf(  "\nUnable to allocate memory for a new student id.");
       printf(  "\nThe program is aborting.");
       exit  (ID_ALLOC_ERR);
   }

   p_new_list->student_id     = insert_id;
   p_new_list->p_next_student = p_current_id;
   p_previous_id->p_next_student  = p_new_list;

   return;
}

/**********************************************************************/
/*               Print all the student id's in the list               */
/**********************************************************************/
void print_list(ID *p_id_list)
{
   printf("\nEntering print_list");
   if(get_count(p_id_list) == LIST_HEADER)
      printf("\n   The student id list is empty.");
   else
   {
      while(p_id_list = p_id_list->p_next_student,
            p_id_list->student_id < LIST_TRAILER)
      {
         printf("\nEntering loop");
         printf("\n                   %6d", p_id_list->student_id);
      }
   }

   return;
}

/**********************************************************************/
/*              Get the count of the students on the list             */
/**********************************************************************/
int get_count(ID *p_id_list)
{
   int student_count=0; /* Counts the length of the id list           */

   while(p_id_list->p_next_student->student_id != LIST_TRAILER)
      student_count += 1;

   return(student_count);
}

/**********************************************************************/
/*            Sort the students into ascending order by id            */
/**********************************************************************/
void sort_list(ID *p_student_id)
{
   ID  *p_id_list,     /* Pointer to a student ID                     */
       *p_temp_id;     /* Pointer to a temporary student ID           */
   int student_number; /* Student ID sorting counter                  */

   for (student_number = 0; student_number < get_count(p_student_id);
                                                       student_number++)
   {
      p_id_list = p_student_id;

      while(p_id_list->p_next_student->p_next_student->student_id !=
            LIST_TRAILER)
      {
         if(p_id_list->p_next_student->student_id < 
            p_id_list->p_next_student->p_next_student->student_id)
         {
            p_temp_id                 = 
               p_id_list->p_next_student->p_next_student;
            p_id_list->p_next_student->p_next_student
               = p_temp_id->p_next_student;
            p_temp_id->p_next_student = p_id_list->p_next_student;
            p_id_list->p_next_student    = p_temp_id;
         }
         p_student_id = p_student_id->p_next_student;
      }
   }
}

/**********************************************************************/
/*                      Remove all duplicate id's                     */
/**********************************************************************/
void remove_duplicates(ID *p_id_list)
{
   ID *p_current_id  = p_id_list->p_next_student,  
                                  /* Points to the current id         */
      *p_previous_id = p_id_list; /* Points to the previous id        */

   printf("\n");
   while(p_current_id->student_id != LIST_TRAILER)
   {
      if(p_previous_id->student_id == p_current_id->student_id)
      {
         printf("\nDeleting the duplicate student id: %d",
                                              p_current_id->student_id);
         p_previous_id->p_next_student = p_current_id->p_next_student;
         free(p_current_id);
         p_current_id = p_previous_id->p_next_student;
      }
      else
      {
         p_previous_id = p_previous_id->p_next_student;
         p_current_id  = p_current_id->p_next_student;
      }
   }

   return;
}

I'm not sure which function is the problem because it al sems alright to me.我不确定哪个功能是问题,因为它对我来说似乎没问题。

In get_count there is an infinete loop because it is checking the same p_id_list over and over again without ever moving to the next record.get_count有一个 infinete 循环,因为它一遍p_id_list遍地检查相同的p_id_list而从未移动到下p_id_list记录。

while(p_id_list->p_next_student->student_id != LIST_TRAILER)
    student_count += 1;

Just need to ensure that the next record is visited on each loop.只需要确保在每个循环中访问下一条记录。 For example:例如:

ID *cur = p_id_list->p_next_student;
while (cur->student_id != LIST_TRAILER) {
    student_count += 1;
    cur = cur->p_next_student;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM