首页 > 解决方案 > 尝试打印双向链表时出现意外断点

问题描述

当我试图打印出我的双向链表时,我遇到了一个意外的断点。意外断点printSongInfo在 dsA1LL.cpp 中的函数中弹出行

while (ptr != NULL) {
    printf("%-35s %-35s %-35d\n", ptr->title, ptr->artist, ptr->rating);
    ptr = ptr->next;
}

//刚刚添加的新我完成了对这段代码的一些更改,现在它没有打印出列表。它只打印出第一个节点。

这只会打印一行然后出现断点。我想知道它是函数还是导致问题printSongInfo的双向链表函数。getSongInfo源代码 dsA1.cpp 文件

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#include"dsA1.h"
#pragma warning(disable : 4996)
#pragma warning(disable : 6387)


int main() {


    char title[kStringLength];
    char artist[kStringLength];
    int rating = 0; 
    SongNode* head = NULL; 
    SongNode* tail = NULL; 



    printf("Enter Title, Artist and Rating\n");
    printf("Enter'.' to get out of the loop and print list\n");
    
    while (kInfiniteLoop) {

        printf("Title: "); 
        fgets(title, kStringLength, stdin); 
        eliminateEndOfLine(title); 

        if (strcmp(title, ".") == 0) {
            break;
        }

        printf("Artist: "); 
        fgets(artist, kStringLength, stdin);
        eliminateEndOfLine(artist);
            
        printf("Rating: "); 
        while (rating = getNum()) { // error check the rating this will check to make sure the rating is in the range 1-5.

            if (rating<1||rating>5){
                printf("The number you have entered is invaild\n");
                rating = 0; 
                printf("Rating: "); 
                continue; 
            }
            break; 
        }
        head = getSongInfo(head, title, artist, rating); 
    }
    
    printSongInfo(head); 
    return 0;
}

dsA1.h 头文件

#pragma once

//structs 
typedef struct SongNode {
    char* title;
    char* artist;
    int rating; 
    SongNode* next;
    SongNode* prev; 
}SongNode;

//Constants 
#define kInfiniteLoop 1 
#define kStringLength 30

//Prototypes
SongNode* getSongInfo(SongNode* head, char title[kStringLength], char artist[kStringLength], int rating);
void printSongInfo(SongNode* head); 
int getNum(void); 
void eliminateEndOfLine(char* buffer); 
 

dsA1LL.cpp 其他源代码文件

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#include "dsA1.h"
#pragma warning(disable : 4996)
#pragma warning(disable : 6387)

SongNode* getSongInfo(SongNode *head, char title[kStringLength], char artist[kStringLength],int rating) {
    
    SongNode* newNode; 
    newNode = (SongNode*)malloc(sizeof(SongNode));
    if (newNode == NULL) {
        printf("No Memory left\n"); 
        return head;
    }

    newNode->title = (char*)malloc(strlen(title) + 1); 
    if (newNode->title == NULL) {
        printf("No memory left for title\n"); 
        return head; 
    }

    newNode->artist = (char*)malloc(strlen(artist) + 1); 
    if (newNode->rating == NULL) {
        printf("No memory left for artist\n"); 
        return head; 
    }

    
    strcpy(newNode->title, title); 
    strcpy(newNode->artist, artist);
    newNode->rating = rating; 

    if (head == NULL) {
        head = newNode;
        return head; 
    }

    head->prev = newNode;
    newNode->next = head; 
    
    return head; 
}

void printSongInfo(SongNode* head) {
    SongNode* ptr;
    ptr = head;

    printf("\n");
    printf("%-35s %-35s\n", "Title", "Artist");

    while (ptr != NULL) {
        printf("%-35s %-35s %-35d\n", ptr->title, ptr->artist, ptr->rating);
        ptr = ptr->next;
    }
}


/*===============================================================================================================*/
/*FUNCTION   :getNum(void)                                                                                       */
/*PARAMETERS :void                                                                                               */
/*RETURNS    :number                                                                                             */
/*DESCRIPTION:This function is the user input function to get a number rating                                    */
/*===============================================================================================================*/
int getNum(void)
{/* the array is 121 bytes in size; we'll see in a later lecture how we can improve this code */
    char record[121] = { 0 }; /* record stores the string */
    int number = 0;
    /* NOTE to student: indent and brace this function consistent with your others */
/* use fgets() to get a string from the keyboard */
    fgets(record, 121, stdin);
    /* extract the number from the string; sscanf() returns a number
 * corresponding with the number of items it found in the string */
    if (sscanf(record, "%d", &number) != 1)
    {
        /* if the user did not enter a number recognizable by
         * the system, set number to -1 */
        number = -1;
    }
    return number;
}



/*=======================================================================================================*/
/*FUCNTION      :void eliminateEndOfLine                                                                 */
/*PARAMETER     :(char* buffer)                                                                          */
/*RETURNS       :void                                                                                    */
/*DESCRIPTION   :This function takes a pointer to a string and looks through the string to find the      */
/*               newline.It takes the new line out of the string.                                        */
/*=======================================================================================================*/
void eliminateEndOfLine(char* buffer)
{
    char* target = strchr(buffer, '\n');
    if (target != NULL)
    {
        *target = '\0';
    }
}

标签: c

解决方案


您创建的第一个节点没有next设置为NULL

一个简单的解决方法是在支票newNode->next = head上方移动。尽管恕我直言,在该检查if (head == NULL)中添加更清楚。newNode->next = NULL

看起来您也可以通过前置添加到列表中。您将 newNode->next 设置为 head 并将 head->prev 设置为 newNode - 但您从未将 head 设置为 newNode。

最后:结帐strdup而不是手卷malloc/strcpy


推荐阅读