You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于数组实现单链表的C++代码在Code::Blocks中运行崩溃,求排查

Hey there, let's figure out why your linked list code is crashing in Code::Blocks. I've spotted a couple of key issues that are causing the runtime crash—let's break them down one by one:

Key Issues Causing the Crash

  • Uninitialized listptr member
    The List class has a Node* listptr member, but you didn't add a default constructor to initialize it to NULL (or nullptr for modern C++). When you create List l1 in main, listptr gets a random garbage value instead of NULL. So in the create function, the check if(listptr==NULL) fails, and you end up trying to access temp->next when temp is also uninitialized—this is a dangling pointer access that directly causes the crash.

  • Minor typo in output formatting
    You wrote cout << "/nEnter the data" << endl;—that's /n instead of \n, which just prints "/n" instead of a new line. It doesn't cause the crash, but it's a small UI issue.

  • Non-standard main function
    Your main() should be int main() and return 0 at the end to comply with C++ standards. While some compilers allow the shorthand, fixing this avoids potential unexpected behavior.


Fixed Code

#include<iostream>
using namespace std;

class Node {
    int data;
    Node *next;
public:
    Node(int n) {
        data = n;
        next = NULL;
    }
    friend class List;
};

class List {
    Node *listptr;
public:
    // Default constructor to initialize listptr to NULL
    List() : listptr(NULL) {}
    void create();
    void display();
};

void List::create() {
    Node *temp = NULL; // Initialize temp to avoid garbage values
    int n, num;
    cout << "Enter number of nodes:" << endl;
    cin >> n;
    cout << "\nEnter the data" << endl; // Fixed /n to \n
    for(int i=0; i<n; i++) {
        cin >> num;
        Node *new_node = new Node(num);
        if(listptr == NULL)
            listptr = temp = new_node;
        else {
            temp->next = new_node;
            temp = temp->next;
        }
    }
}

void List::display() {
    Node *temp = listptr;
    while(temp != NULL) {
        cout << temp->data << "->";
        temp = temp->next;
    }
    cout << "NULL" << endl; // Added to mark the end of the list
}

int main() { // Standard int main()
    List l1;
    l1.create();
    l1.display();
    return 0; // Return 0 as per C++ standards
}

Breakdown of Fixes

  1. Added a default constructor for List that initializes listptr to NULL using an initializer list—this ensures the pointer starts as a valid null value instead of garbage.
  2. Initialized temp to NULL in the create function to avoid accidental use of an uninitialized pointer.
  3. Fixed the /n typo to \n for proper line breaks in output.
  4. Updated main() to follow standard C++ syntax with int main() and return 0;.
  5. Added cout << "NULL" << endl; in display() to clearly show the end of the linked list, making output more readable.

内容的提问来源于stack exchange,提问作者Rajan Pandey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:13:46