【问题标题】:How to Implement Link List properly in C/C++ without program crashing如何在 C/C++ 中正确实现链接列表而不导致程序崩溃
【发布时间】:2018-08-20 11:29:23
【问题描述】:

我正在尝试实现链接列表。在这个示例程序中,用户输入一个整数值(要存储在列表中的字符串的数量),然后一个一个地输入字符串...但是在多次输入(可能是 4 或 5)之后程序崩溃like the image here...

即使,我不能一次调用任何包含 malloc() 的函数超过 3 次。 我不知道为什么会出现问题。帮我解决问题....

#include <bits/stdc++.h>
using namespace std;
typedef struct Linked_List NODE;

struct Linked_List
{
    string data;
    NODE* next;
};

//Function prototypes
NODE *traverse(NODE *temp);
NODE* createNode(string data);
void preAppend(NODE* ln_list, string x);
NODE* find_data(NODE* ln_list, string data);

int main()
{
    NODE* x=createNode("");
    int t;
    cin >>t;
    string z;
    while(t--)
    {
        cin >> z;
        preAppend(x, z);
    }

    traverse(x);
    return 0;
}

NODE *traverse(NODE *temp)
{
    cout << temp->data << endl;
    if(temp->next==NULL) return temp;
    traverse(temp->next);
}

NODE* createNode(string data)
{
    NODE* node = (NODE*)malloc(sizeof(NODE));
    if(node==NULL)
    {
        printf("Error creating node (Error! Allocating Memory)\n");
        exit(1);
    }
    node->data = data;
    node->next = NULL;
}

void preAppend(NODE* ln_list, string x)
{
    NODE* new_node = (NODE*)malloc(sizeof(NODE));
    if(new_node==NULL)
    {
        printf("Error! Appending (Error Allocating Memory)\n");
        exit(1);
    }
    new_node->data = x;
    new_node->next = ln_list->next;
    ln_list->next = new_node;
}

NODE* find_data(NODE* ln_list, string data)
{
    NODE* current_node;
    current_node = ln_list;
    while(current_node->next!=NULL)
    {
        if(current_node->data == data)
        {
            return current_node;
        }
        current_node  = current_node -> next ;
    }
    return NULL;
}

【问题讨论】:

    标签: c++ linked-list


    【解决方案1】:

    你的代码有几个问题:

    使用malloc 而不是new

    malloc 用于包含c++ 对象的对象(如您的情况下的string)不会调用构造函数,因此对非构造对象的任何操作都会失败。

    如果您的程序在没有return 语句的情况下运行,那是因为undefined behaviour

    解决方案:

    替换

    NODE* new_node = (NODE*)malloc(sizeof(NODE));
    

    NODE* new_node = new NODE;
    

    非 void 函数中没有 return 语句

    NODE *traverse(NODE *temp)
    {
      cout << temp->data << endl;
      if (temp->next == NULL) return temp;
      return traverse(temp->next);  // return statement is needed here
    }
    
    NODE* createNode(string data)
    {
      NODE* node = new NODE;
      if (node == NULL)
      {
        printf("Error creating node (Error! Allocating Memory)\n");
        exit(1);
      }
      node->data = data;
      node->next = NULL;
      return node;    // return statement needed here
    }
    

    滥用递归

    traverse 中使用递归可能会导致长列表的堆栈溢出。

    您应该使用迭代方法。但是你已经发现了那个。

    【讨论】:

      【解决方案2】:

      我一次上传了整个代码。这种方法应该可以不间断地工作 尽管如果您想全面了解每个部分,我建议您阅读我在我的网站上写的这篇文章。 https://www.thebytewise.com/post/data-structure-and-algorithm-using-c-linear-linked-list-thebytewise

      #include<stdio.h>
      #include<stdlib.h>
      
      void createList();
      void traverseList();
      
      struct node{
      int data;
      struct node *next;
      }*header;
      
      
      int main(){
          int n;
          printf("Enter the number of elements: ");
          scanf("%d", &n);
          createList(n);
          printf("\nData in the list:\n");
          traverseList(n);
          return 0;
      }
      
      void createList(int n){
      struct node *newNode, *temp;
      int data, i;
      
      newNode = (struct node *) malloc(sizeof(struct node));
      
      if(newNode == NULL){
          printf("ERROR: Memory Overflow");
      }
      else{
          printf("Enter element 1: ");
          scanf("%d", &data);
          newNode->data = data;
          newNode->next = NULL;
          header = newNode;
          temp = newNode;
      
          for(i=2;i<=n;++i){
      
                  newNode = (struct node *) malloc(sizeof(struct node));
      
             if(newNode == NULL){
              printf("ERROR: Memory Overflow");
             }
             else{
              printf("Enter element %d: ",i);
              scanf("%d",&data);
      
              newNode->data = data;
              newNode->next = NULL;
              temp->next = newNode;
              temp = temp->next;
             }
          }
      }
      }
      
      void traverseList(int n){
      struct node *temp;
      int i;
      
      if(header == NULL){
          printf("ERROR: Memory Underflow");
      }
      
      else{
          temp = header;
          for(i=0;i<n;++i){
              printf("\ndata %d= %d",i+1, temp->data);
              temp = temp->next;
          }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-07
        • 1970-01-01
        • 1970-01-01
        • 2018-07-29
        • 2019-05-23
        • 1970-01-01
        • 2016-02-04
        相关资源
        最近更新 更多