简体   繁体   English

链接列表 - 添加节点时程序崩溃

[英]Linked List - program crashes while adding nodes

My code compiles correctly, but after 4 loops of my insertLast function, the program crashes. 我的代码编译正确,但在insertLast函数的4个循环之后,程序崩溃了。 Can someone help me understand why? 有人可以帮我理解为什么吗?

I posted a similar question earlier which helped me identify other problems. 我之前发布了一个类似的问题,帮助我找出其他问题。 I've rewritten the function but I still have the same problem. 我已经重写了这个功能,但我仍然有同样的问题。 My code below: 我的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include "LinkedList.h"


int main (int argc, char* argv[])

{
    int ii;

        {
        FILE* f; /*open file for reading to get ints*/
        f = fopen(argv[1], "r");

        if(f==NULL) 
            {
            printf("Error: could not open file");
            return 0;
            }

    LinkedList* canQueue=createList();

    for(ii = 0; ii < 10; ii++)
        {
        TinCan* tempCan= (TinCan*) malloc(sizeof(TinCan));
        fscanf(f, " WGT_%d", &tempCan[ii].weight);
        insertLast(canQueue, tempCan); /*Inserts the new can into linked list*/
        }
    testLinkedList(canQueue);
    }
    return 0;

}

LinkedList.h LinkedList.h

typedef struct TinCan
    {
    int weight;
    } TinCan;

typedef struct Node
    {
    TinCan* data;
    struct Node *next;
    } Node;

typedef struct LinkedList
    {
    Node *head;
    } LinkedList;

void insertLast(LinkedList* list, TinCan *newData);
LinkedList* createList();
void testLinkedList(LinkedList* list);

LinkedList.c LinkedList.c

#include <stdio.h>
#include <stdlib.h>
#include "LinkedList.h"

LinkedList* createList() /*creates empty linked list*/
  {
    LinkedList* myList;
    myList = (LinkedList*)malloc(sizeof(LinkedList));
    myList->head = NULL;
    return myList;
  }

void insertLast(LinkedList* list, TinCan *newData)
    {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = newData;
    newNode->next = NULL;

    if(list->head==NULL)
        {
        Node* current = (Node*)malloc(sizeof(Node));
        list->head=newNode;
        current=newNode;
        }

        else
            {
            Node* temp = (Node*)malloc(sizeof(Node));
            temp = list->head;
            while(temp->next!=NULL)
                {
                temp = temp->next;
                }
             temp->next = newNode;
            }
  printf("Looped\n");
  }


void testLinkedList(LinkedList* list)
  {
  Node* current;
  current = list->head;

  while(current != NULL)
    {
    printf("Weight = %d\n", current->data->weight);
    current = current->next;
    }
  }

These lines can be removed: 这些行可以删除:

Node* current = (Node*)malloc(sizeof(Node));
current=newNode;

This line doesn't need an allocation of memory: 这一行不需要分配内存:

Node* temp = (Node*)malloc(sizeof(Node));

I bet you are actually breaking on this line though: 我敢打赌你实际上是打破了这条线:

fscanf(f, " WGT_%d", &tempCan[ii].weight);

tempCan isn't an array, I'm not 100% sure what the &tempCan[ii] will do, but I suspect you're accessing memory around your tempCan pointer location and that it's only working for 4 because that's the size of something. tempCan不是一个数组,我不是100%肯定&tempCan[ii]将会做什么,但我怀疑你正在访问tempCan指针位置周围的内存,并且它只适用于4,因为这是一个大小的东西。

In for loop, 在for循环中,

fscanf(f, " WGT_%d", &tempCan[ii].weight);

instead do 相反

fscanf(f, " WGT_%d", &tempCan->weight);

tempCan has allocated for only 1 element. tempCan仅分配了1个元素。 As your loop counter increments, you as accessing invalid location. 随着循环计数器递增,您将访问无效位置。

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

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