简体   繁体   English

如何在C中使用结构?

[英]How to use a struct in C?

This is code for a linked list in the C programming language. 这是C编程语言中链表的代码。

#include <stdio.h>    /* For printf */
#include <stdlib.h>   /* For malloc */

typedef struct node {
    int data;
    struct node *next; /* Pointer to next element in list */
} LLIST;

LLIST *list_add(LLIST **p, int i);
void list_remove(LLIST **p);
LLIST **list_search(LLIST **n, int i);
void list_print(LLIST *n);

The code is not completed, but I think it's enough for my question. 代码尚未完成,但我认为足以解决我的问题。 Here at the end of struct node "LLIST" is used, and it's also used as a return type in the prototyping of the function list_add . 在此,使用结构节点“ LLIST”的末尾,并且在函数list_add的原型中还将其用作返回类型。 What is going on? 到底是怎么回事?

That's a typedef . 那是一个typedef It's actually doing two things at once. 实际上,它同时执行两项操作。 First, it defines a structure: 首先,它定义一个结构:

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

And then does a typedef : 然后执行typedef

typedef struct node LLIST;

That means LLIST is a type, just like int or FILE or char , that is a shorthand for struct node , your linked-list node structure. 这意味着LLIST是一种类型,就像intFILEchar ,它是struct node (链接列表节点结构)的简写。 It's not necessary - you could replace LLIST with struct node in all of those spots - but it makes it a bit easier to read, and helps hide the implementation from pesky end-users. 没必要-您可以在所有这些位置上用struct node替换LLIST但这使它更易于阅读,并帮助讨厌的最终用户隐藏实现。

LLIST is just another type name for the struct that has been created. LLIST只是已创建结构的另一个类型名称。 In general, the following format will create a type "NAME" that is a "struct x": 通常,以下格式将创建类型“ NAME”,即“ struct x”:

typedef struct x { ... } NAME;

C requires that you reference structs with a "struct" prefix, so it's common to introduce a typedef for less verbose mention. C要求您引用带有“ struct”前缀的结构,因此通常引入typedef以减少冗长的提及。

That is, the declaration of your struct has two parts, and can be rewritten as such: 也就是说,结构的声明包含两个部分,可以这样重写:

struct node {
    int data;
    struct node *next; /* pointer to next element in list */
};

typedef struct node LLIST;

So, LLIST is just another name for struct node (thanks Chris Lutz). 因此, LLIST只是struct node别称(感谢Chris Lutz)。

typedef creates a new "type" in your program, so the return value and types of parameters of those functions are just your struct. typedef在您的程序中创建一个新的“类型”,因此这些函数的返回值和参数类型仅是您的结构。 It is just shorthand for using struct node for the type. 它只是将struct node用于类型的简写。

If you were to create a new node, you could do it like this (using the type): 如果要创建一个新节点,则可以这样做(使用类型):

LLIST *node = malloc(sizeof(LLIST));
node->data = 4;
node->next = someOtherItem;
list_add(node, 1)

Also, with the function prototypes in your question, you don't really need the double pointers; 另外,有了问题原型中的函数原型,您实际上就不需要双指针了。 since the data in your struct is just an int , you could do something like 由于结构中的数据只是一个int ,因此您可以执行以下操作

LLIST *list_add(int data, int position);

then the list_add function would handle the allocation, copy the int into the struct and add it to the linked list. 然后list_add函数将处理分配,将int复制到struct并将其添加到链接列表。

Putting it in at a certain position is as simple as changing the next pointer in the node before it to the address of the newly allocated node, and the next pointer in the new node to point at the next one (the one the node before that one was originally pointing at). 将其放在某个位置就像将它之前的节点中的next指针更改为新分配的节点的地址,然后将新节点中的next指针指向next指针(该节点之前的节点)一样简单。一个人最初指向)。

Keep in mind that (given the rest of your function prototypes) you will have to keep track of pointers to every node you create in order to delete them all. 请记住(考虑到其余的函数原型),必须删除所有创建的节点的指针。

I'm not sure I understand how the search function will work. 我不确定我是否了解搜索功能的工作方式。 This whole thing could be implemented a lot better. 这整个事情可以更好地实现。 You shouldn't have to provide the location of a node when you create it (what if you specify a higher number than there are nodes?), etc. 创建节点时,您不必提供节点的位置(如果指定的数字大于节点数,该怎么办?)等等。

LLIST* is a pointer to a structure defined by the LLIST struct. LLIST*是指向由LLIST结构定义的结构的指针。

You should do 你应该做

LLIST* myList = malloc(sizeof(LLIST)*number_of_elements);

to have some memory allocated for this list. 为该列表分配一些内存。 Adding and removing items requires you to reallocate the memory using realloc. 添加和删​​除项目需要您使用realloc重新分配内存。 I've already written some piece of code for lists (made with arrays). 我已经为列表(用数组制作)写了一些代码。

I might post the code as soon as I'm home, which is currently not the case. 我可能会在回家后立即发布代码,目前情况并非如此。

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

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