【问题标题】:Linked list not functioning properly链接列表无法正常运行
【发布时间】:2017-04-30 02:57:53
【问题描述】:

我正在创建一个链表类型的数据结构,但不完全是。到目前为止,我有以下内容,但问题是,在此方法完成后,只有第一个链接仍然存在,第一个链接之后的链接消失了。这里有什么问题?

//Global Vars
m_p H = NULL; //m_p is the struct

func(){
    /* .... */
    H = malloc(sizeof(m_r));
    H->next = NULL;
    H->previous = NULL;
    m_p p = H;
    for(i = 0; i < nLists; i++){
        m_p n = malloc(sizeof(m_r));
        n->next = NULL;
        n->previous = p;
        p->next = n;

        p = p->next;
    }
  //if I iterate through the links here, they all print out fine

  m_p x = H;
  while(x != NULL){
     printf("%d ", x->data);
     x = x->next;
  }
}

既然 H 是一个全局变量,链接不应该保持原样吗?当我尝试遍历同一文件中另一个函数中的链接时,它们不存在,只有第一个存在。我做错了什么?

编辑:我省略了在每个结构中设置数据字段以保持代码简洁的部分。

编辑2:这是另一个文件中的一个函数,这是发生错误的地方:

extern m_p H;

m_p func2(int s) {

  int n_s = ALIGN(s);
    m_p p = H;

    while(p != NULL ){
        printf("1. %d \n", p->data);
        p = p->next; //this is always NULL even if there were more than one node previously in func()
        if(p == NULL) printf("LINK NOT PRESENT \n");    
    }

  /* ... */

}

这也是我的结构的样子:

typedef struct m{
    struct m *next;
    struct m *previous;
    int data;

} m_r, *m_p;

【问题讨论】:

  • 哎呀,我在尝试重新输入时出错了。 @BLUEPIXY
  • 这就是我的代码的样子;它仍然给出错误。 @BLUEPIXY
  • 错误仍然存​​在@BLUEPIXY
  • 第一个节点的previous 和最后一个节点的next没有设置。
  • 即使它们不是,如果我只使用下一个指针来迭代打印数据,为什么会产生问题?

标签: c pointers data-structures linked-list malloc


【解决方案1】:

据我所知,您的代码看起来不错。 BLUEPIXY 提供的代码有效,所以我必须同意 BLUEPIXY。您的问题可能在其他地方。这是我在电脑上测试的代码(和BLUEPIXY's code很像)。

list.h

struct node {
    struct node *previous, *next;
    int data;
};

void linkedList();
void anotherFunction();
void freeMemory();

list.c

#include <stdlib.h> // malloc, free
#include <stdio.h> // printf
#include "list.h"

// Global variable
struct node *m_root;

void linkedList(){
    m_root = malloc(sizeof(struct node));
    m_root->data = -1;
    m_root->next = NULL;
    m_root->previous = NULL;
    int i;
    int n = 10;
    struct node *prev = m_root;
    struct node *next;
    for(i=0;i<n;i++){
        next = malloc(sizeof(struct node));
        next->data = i;
        next->previous = prev;
        prev->next = next;

        prev = prev->next;
    }

    printf("Within linkedList\n");
    struct node *iter = m_root;
    while(iter!=NULL){
        printf("%d ",iter->data);
        iter = iter->next;
    }
    printf("\n");
}

void anotherFunction(){
    printf("Within anotherFunction\n");
    struct node *iter = m_root;
    while(iter!=NULL){
        printf("%d ",iter->data);
        iter = iter->next;
    }
    printf("\n");
}

void freeMemory(){
    printf("Freeing memory\n");
    struct node *current = m_root;
    struct node *next;
    while(next!=NULL){
        next = current->next;
        free(current);
    }
    m_root->next = NULL;
    m_root = NULL;
}

extern.c

#include <stdio.h>
#include "list.h"

extern struct node *m_root;

void anotherFileFunction(){
    struct node *iter = m_root;
    while(iter!=NULL){
        printf("1. %d\n",iter->data);
        iter = iter->next;
    }
}

int main(){
    linkedList();
    anotherFileFunction();
    freeMemory();
    anotherFileFunction();
    printf("Done!\n");
    return 0;
}

输出是:

Within linkedList
-1 0 1 2 3 4 5 6 7 8 9 
1. -1
1. 0
1. 1
1. 2
1. 3
1. 4
1. 5
1. 6
1. 7
1. 8
1. 9
Freeing memory
Done!

【讨论】:

  • 另一个函数在另一个文件中可能是一个问题吗?我经过进一步测试,链表在同一个文件的另一个函数中很好,但是当我在另一个文件中调用另一个函数时,其他链接似乎消失了。
猜你喜欢
  • 2016-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 2018-07-15
  • 2020-11-28
  • 2014-07-01
  • 1970-01-01
相关资源
最近更新 更多