c - LinkedList 长度、通过堆访问和实现。 C

标签 c pointers data-structures struct nodes

我正在尝试从 main.c 文件打印我在另一个名为 linklist.c 的 .c 文件中创建的链接列表的长度。它不起作用,我相信这与指针和/或内存管理有关。我主要在这里对堆提出质疑。一些指导将不胜感激。

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

int main()
{
    printf("Hello world!\n");

    struct node* mylist = BuildOneTwoThree();
    int length = Length(mylist);

    printf(mylist->data);
    printf(length);
    return 0;
}

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


// Return the number of nodes in a list (while-loop version)
int Length(struct node** head) {
   int count = 0;
   struct node* current = head;
   while (current != NULL) {
      count++;
      current = current->next;
   }
   return(count);
}

/*
 Build the list {1, 2, 3} in the heap and store
 its head pointer in a local stack variable.
 Returns the head pointer to the caller.
*/
struct node* BuildOneTwoThree() {
   struct node* head = NULL;
   struct node* second = NULL;
   struct node* third = NULL;
   head = malloc(sizeof(struct node)); // allocate 3 nodes in the heap
   second = malloc(sizeof(struct node));
   third = malloc(sizeof(struct node));
   head->data = 1; // setup first node
   head->next = second; // note: pointer assignment rule
   second->data = 2; // setup second node
   second->next = third;
   third->data = 3; // setup third link
   third->next = NULL;
   // At this point, the linked list referenced by "head"
   // matches the list in the drawing.
   return head;
}

/*
 Takes a list and a data value.
 Creates a new link with the given data and pushes
 it onto the front of the list.
 The list is not passed in by its head pointer.
 Instead the list is passed in as a "reference" pointer
 to the head pointer -- this allows us
 to modify the caller's memory.
*/
void Push(struct node** headRef, int data) {
   struct node* newNode = malloc(sizeof(struct node));
   newNode->data = data;
   newNode->next = *headRef; // The '*' to dereferences back to the real head
   *headRef = newNode; // ditto head points to new node
}

// Given a list and an index, return the data
// in the nth node of the list. The nodes are numbered from 0.
// Assert fails if the index is invalid (outside 0..lengh-1).
int GetNth(struct node* head, int index) {
    struct node* current = head;
    int answer = 0;
    int x = index;
    if(x <= 0 || x >= sizeof(head)-1 )
        {
            return -1;
        }
    for(int i = 0; i <= sizeof(head)-1; i++){
        if (i == x){
            return current->data;
        }
        current = current->next;
    }
}

正如您所看到的,我使用 BuildOneTwoThree 函数来构建链接列表并正在编写适当的函数...当我尝试将 mylist 访问到输出中时,它崩溃了。

最佳答案

在大多数情况下,代码似乎仅从问题的角度起作用,printf 必须正确格式化。

printf("%i", length);

关于c - LinkedList 长度、通过堆访问和实现。 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51878642/

相关文章:

C++:在结构定义问题中指向结构的指针

c++ - 在 Unity 中用 C++ 实现组件系统

c - 如何扫描 C 中数字的前 2 位数字?

c - 重定向到文件的输出在 C 中给出了编码问题

c++ - 如何找到指针使用的字节数?

c - 为什么ssh登录时会调用两次pam_sm_authenticate?

c++ - 从指针计算数组索引

c++ - 在指针类中引用虚函数时出现段错误

arrays - 如何在剧院中找到二维阵列的最佳座位安排

arrays - 为什么在有界队列中使用数组作为数据结构是个坏主意?