【问题标题】:Linked List made with 2 structs. Access node由 2 个结构组成的链表。接入节点
【发布时间】:2017-11-11 12:41:30
【问题描述】:

我正在努力学习基础知识,谁会想到,指针正在杀死我。我想创建一个基本的链表。为此,我创建了两个结构,一个包含 3 个指针的linkedList,一个用于第一个节点,一个用于当前节点,一个用于最后一个节点。我希望这可以更轻松地插入新节点。 然后我有节点,它们只是一个值和一个指向新节点的指针。

typedef struct linkedList linkedList;
struct linkedList{
    struct node* start;
    struct node* last;
    struct node* current;
};

typedef struct node node;
struct node{
    int value;
    struct node* next;
};

我用

创建了一个新节点
node* newNode(int value){
    node* newNode = (node*)malloc(sizeof(node));
    newNode->next = NULL;
    newNode->value = value;
    return newNode
}

现在,我主要创建一个新的链表

linkedList List;
List.start = NULL;
List.current = NULL;
List.last = NULL;

node* node1 = newNode(1);

List.start = node1;
List.current = node1;
List.last = node1;

现在,我如何使用链表访问节点的内容? 如果我这样做,我会如何在 Java 中做到这一点,编译器会说 “错误:在不是结构或联合的东西中请求成员'值'” 如果我尝试

List.start.value = 1;

如果我尝试

(*(List.start)).value = 1;

它编译但给我一个分段错误。 我不确定指针的工作方式,真的。我用谷歌找到的链表都只使用节点,没有要与之交互的主链表。我几乎陷入了学校课程的深渊,所以我对 c 复杂性的了解是有限的。

【问题讨论】:

  • 你需要一本书。
  • @Jim Johnson List.start->value = 1;?
  • @Jim Johnson 并显示演示分段错误问题的最小代码。
  • @JimJohnson.: 它不应该......你可以发布完整的代码。(除非你的 malloc 失败)

标签: c pointers struct linked-list


【解决方案1】:
    List.start.value = 1;

以上行不通。 List.start 会给出node1,所以接下来你需要做node1->value 因为node1 是结构类型的指针。

所以使用下面的语句:

    (List.start)->value = 200;

或者你也可以这样使用:

    (*(List.start)).value = 200;

用这个测试这些:

    printf("accessing : %d\n", node1->value);

【讨论】:

    【解决方案2】:

    您所做的是类似于N 独立节点,它们每个都知道第一个节点、最后一个节点和它自己。

    你说你想创建一个链表?得到任何提示?链接在哪里?您必须将它们连接在一起以形成一个数据结构,这将使访问更容易。

    因此,我将概述我们执行这些操作的标准方式。您将有一些阅读要做。

    你想要实现的是一个叫做Doubly linked list的东西,每个节点都知道另外两个,一个在它之前,另一个在它之后。

    现在这使得插入和删除节点变得更加容易。

    结构节点{ 整数数据; 结构节点 * 下一个; 结构节点 * 上一个; };

    现在你需要创建一个双向链表,你会在main()做这样的事情

    struct node*head= malloc(sizeof(struct node));
    if( head == NULL){
        fprintf(stderr, "%s\n","Error in malloc" );
        exit(1);
    }
    
    insert(head, data);
    ...
    ..
    
    
    void insert( struct node * head, int data){
        struct node*temp= malloc(sizeof(struct node));
        if( temp == NULL){
            fprintf(stderr, "%s\n","Error in malloc" );
            exit(1);
        }
        temp->data = data;
        temp->next = head;
        temp->prev = NULL;
        head = temp;
    }
    

    完成后也删除双向链表。

    你是怎么做到的......

    linkedList List;
    List.start = NULL;
    List.current = NULL;
    List.last = NULL;
    
    node* node1 = newNode(1);
    
    List.start = node1;
    List.current = node1;
    List.last = node1;
    

    现在如果你想访问node1,你可以这样做

    (*List.start).value = 1;
    

    或者

    (List.start)->value = 1;
    

    第一个错误的解释:-

    List.start.value 基本上是试图在node1 的地址上找到值字段。编译器不知道如何从非结构或非联合元素中获取它。

    我所说的关于你上一个错误的小演示:-

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct linkedList linkedList;
    struct linkedList{
        struct node* start;
        struct node* last;
        struct node* current;
    };
    
    typedef struct node node;
    struct node{
        int value;
        struct node* next;
    };
    node* newNode(int value){
        node* newNode = (node*)malloc(sizeof(node));
    
        if( newNode == NULL){
           fprintf(stderr,"error in malloc");
           exit(1);
        }
        newNode->next = NULL;
        newNode->value = value;
        return newNode;
    }
    int main(){
        linkedList List;
        List.start = NULL;
        List.current = NULL;
        List.last = NULL;
    
        node* node1 = newNode(1234);
    
        List.start = node1;
        List.current = node1;
        List.last = node1;
        printf("%d\n", (List.start)->value);
        printf("%d\n", (*List.start).value);// this works
    
    }
    

    【讨论】:

    • @JimJohnson.: 我已经展示了一个示例代码,我可以像您一样访问元素......检查代码。它可能会有所帮助
    • @JimJohnson.:根据您的想法或实现方式,您最终无法获得良好的可管理代码。您需要一个双向链表左右
    猜你喜欢
    • 2016-08-10
    • 2021-02-25
    • 1970-01-01
    • 2012-05-01
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    相关资源
    最近更新 更多