您好,我目前正在开发一个项目,在该项目中我正在创建一个非常简单的文件系统,并且正在研究一些 inode 和通用文件缓存实现,我想知道考虑这样的结构:
typedef struct disk_inode {
short type; /* file type */
short nlinks; /* number of directory entries referring to this file
int size; /* file size in bytes */
short inode_indir_idx;
/* pointers to the first NDIRECT blocks */
blknum_t direct[INODE_NDIRECT];
blknum_t indirect; /* The rest of the blocks */
}disk_inode_t;
struct cache{
short blocknr;
char block[512];
};
有没有办法创建一个可以被这两个结构使用的通用列表?这是用 C 编写的,我不能使用任何标准 C 库。
最佳答案
您可以创建一个通用链表,它将 void *
作为其元素。但在大多数情况下,这种实现需要您分配元素。
这是一个简单的例子:
typedef struct list_s
{
void *elm;
struct list_s *next;
struct list_s *prev;
} list_t;
typedef struct
{
int elem1;
int elem2;
int elem3;
} my_struct_t;
int main(void)
{
my_struct_t *elem = malloc(sizeof(my_struct_t));
list_t *list = malloc(sizeof(list_t));
list->prev = NULL;
list->next = NULL;
list->elm = elem;
return 0;
}
关于c - 制作可与不同大小的结构一起使用的通用链表实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50307552/