簡體   English   中英

嘗試在C中創建一個空的鏈表

[英]Trying to create an empty linked list in C

我正在嘗試創建一個空的鏈接列表,該列表要求用戶提供列表可以容納的最大術語數。 (我沒有將我的代碼添加為簡單的printf)。 然后,我必須創建一個新函數,要求用戶將輸入插入到先前創建的列表中。

我的問題是,如何使create_q()函數返回空列表?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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

typedef struct priority_linked_list {
    struct name *head;
    int current_size;
    int max_size;
}priority_list;

typedef node *Node;
typedef priority_list *List;

void create_q(int max_terms) {

    node *head = NULL;
    node *next = NULL;
    List *current_size = 0;
    List *max_size = max_terms;

}

在C語言中,鏈表通常被實現為一系列存儲在堆中的節點,這些節點相互指向對方。 堆是在程序的整個生命周期中運行的持久性內存區域。

當您通常在C函數中創建變量並且函數返回時,您創建的變量將不再可訪問。 但是,當您在函數中的堆上創建某些東西並返回該函數時,您在堆上分配的數據仍然存在。 但是,您無法訪問它-除非函數返回指針。

因此,對create_q()所做的工作是在堆上創建鏈接列表(使用stdlib.h中的一個名為“ malloc”的函數),然后您將返回一個指向第一個節點的指針,讓主函數知道在堆上的哪里找到第一個節點。 然后,第一個節點中將有一個指針,告訴程序在堆上的哪個位置可以找到第二個節點,依此類推。

但是,您可能以錯誤的方式處理鏈表。 除非這是用於某種家庭作業項目,否則您可能不想創建一個空的鏈表。 鏈表的好處之一是它是一種動態結構,您可以在其中輕松插入新節點。 您仍然可以使用一些變量來跟蹤想要列表達到的最大大小,但是您可能直到需要時才真正想要創建節點。

只要記住什么是鏈表。 它是一組浮在堆上的節點(在C中),每個節點都存儲一些數據,並包含指向下一個浮在堆上的節點的指針。 訪問鏈接列表所需要的只是指向第一個節點的指針。 要添加新節點,您只需在列表中“遍歷”直到到達最后一個節點,然后創建一個新節點並讓最舊的節點指向該節點。

Is this what you had in mind?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct node_t
{
    int value;
    int priority;

    struct node_t *next;
};

static int current_size;
static int max_size;
static struct node_t* head = NULL;

struct node_t* create_q(int);

struct node_t* create_q(int max_terms)
{
    int i; // loop counter/index

    current_size = max_terms;
    max_size = max_terms;

    if( NULL == (head = malloc(sizeof(struct node_t)*max_terms)))
    { // then, malloc failed
        perror("malloc failed for struct node_t list");
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    // set all fields to '0,0,Null'
    memset( head, 0x00, sizeof(struct node_t)*max_terms);

    // set all next links, except last link
    for(i=0;i<(max_terms-1);i++)
    {
        head[i].next = &head[i+1];
    }

    // set last link
    head[i].next = NULL;

    return( head );
} // end function: create_q

我懷疑您正在尋找以下類似內容來創建或初始化優先級鏈接列表。

/*****
 * alloc_q - allocate memory for the priority linked list
 */
struct priority_linked_list *alloc_q(void)
{
    struct priority_linked_list *list;

    list = malloc(sizeof(*list));
    return list;
}

/******
 * init_q - initialize the priority linked list
 */
void init_q(struct priority_linked_list *list, int max_terms)
{
    list->head = NULL;
    list->current_size = 0;
    list->max_size = max_terms;
}

/******
 * create_q - allocate AND initialize the priority linked list
 */
struct priority_linked_list *create_q(int max_terms)
{
    struct priority_linked_list *list;

    list = alloc_q();
    if (list == NULL) {
        return NULL;
    }
    init_q(list, max_terms);
    return list;
}

節點的分配及其在列表中的添加/刪除將分別處理。

上面可能有錯別字(我沒有測試過)。 但是,它足以使您走上想要的道路。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM