为双向链表创建标题

标签 c gcc header doubly-linked-list

因此,我正在学习操作系统类(class),并且我们在学期开始时开设了 C 编程速成类(class)。我们的前 2 个任务很简单,但我就是无法在我的生活中解决这个问题。

所以我们的教授让他的助教创建了以下 .c 文件:

#include <stdio.h>
#include <stdlib.h>
#include "dll.h"

void init(Dll *dll, int n){
Node *prev = &(dll->head);
prev->prev = NULL;
Node *node;
for(int i=0;i<n;i++){
    node = (Node *)malloc(sizeof(Node));
    node->value = rand();
    node->prev = prev;
    node->next = NULL;
    prev->next = node;
    prev = node;
    printf("%d. ", i+1);
    printf("%d\n", node->value);
}
}
void print(Dll *dll){
Node *current;
current = dll->head.next;
while(current->next != NULL){
    printf("%d\n", current->value);
    current = current->next;
}
printf("%d\n", current->value);
}
void sort(Dll *dll){
int changed = 1;
while(changed==1){
    Node *current;
    current = dll->head.next;
    changed = 0;
    while(current->next != NULL){
        if(current->value > current->next->value){
            int value;
            value = current->value;
            current->value = current->next->value;
            current->next->value = value;
            changed = 1;
        }
        current = current->next;
    }
}

}

int main(){
Dll dll;
init(&dll, 10);
print(&dll);
sort(&dll);
printf("\nSorted:\n");
print(&dll);
return 0;
}

我们的任务是创建我们应该实现的头文件来运行 .c 文件。我花了一整天的时间来研究它,我能想到的最好的办法是:

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

typedef struct Dll {
Dll *prev;
Dll *next;
int value;
} dll;


void *init(Dll *dll, int n);
void sort(Dll *dll);
void print(Dll *dll);

每当我用我的 .h 运行它时,我都会在命令行中收到以下错误(我们在 linux 上使用 gcc):

In file included from dll.c:5:0:
dll.h:5:5: error: unknown type name ‘Dll’
dll.h:6:5: error: unknown type name ‘Dll’
dll.h:10:1: error: unknown type name ‘Dll’
dll.h:12:12: error: unknown type name ‘Dll’
dll.h:13:11: error: unknown type name ‘Dll’
dll.h:14:12: error: unknown type name ‘Dll’
dll.c:7:11: error: unknown type name ‘Dll’
dll.c:22:12: error: unknown type name ‘Dll’
dll.c:31:11: error: unknown type name ‘Dll’
dll.c: In function ‘main’:
dll.c:51:2: error: unknown type name ‘Dll’

我使用了这些类型的头文件的其他示例来制作我的头文件,但出于某种原因,我无法理解这一点。如果你们能给我任何帮助,我将不胜感激。

*编辑:* 当我将名称更改为“Dll”时,它会给我:

In file included from dll.c:5:0:
dll.h:5:5: error: unknown type name ‘Dll’
dll.h:6:5: error: unknown type name ‘Dll’
dll.h:10:1: error: unknown type name ‘Type’
dll.h:12:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
dll.c:7:6: error: conflicting types for ‘init’
dll.h:14:7: note: previous declaration of ‘init’ was here
dll.c: In function ‘init’:
dll.c:8:2: error: unknown type name ‘Node’
dll.c:8:20: error: ‘Dll’ has no member named ‘head’
dll.c:9:6: error: request for member ‘prev’ in something not a structure or union
dll.c:10:2: error: unknown type name ‘Node’
dll.c:11:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
dll.c:11:2: note: use option -std=c99 or -std=gnu99 to compile your code
dll.c:12:11: error: ‘Node’ undeclared (first use in this function)
dll.c:12:11: note: each undeclared identifier is reported only once for each function it         appears in
dll.c:12:17: error: expected expression before ‘)’ token
dll.c:13:7: error: request for member ‘value’ in something not a structure or union
dll.c:14:7: error: request for member ‘prev’ in something not a structure or union
dll.c:15:7: error: request for member ‘next’ in something not a structure or union
dll.c:16:7: error: request for member ‘next’ in something not a structure or union
dll.c:19:22: error: request for member ‘value’ in something not a structure or union
dll.c: In function ‘print’:
dll.c:23:2: error: unknown type name ‘Node’
dll.c:24:15: error: ‘Dll’ has no member named ‘head’
dll.c:25:15: error: request for member ‘next’ in something not a structure or union
dll.c:26:25: error: request for member ‘value’ in something not a structure or union
dll.c:27:20: error: request for member ‘next’ in something not a structure or union
dll.c:29:24: error: request for member ‘value’ in something not a structure or union
dll.c: In function ‘sort’:
dll.c:34:3: error: unknown type name ‘Node’
dll.c:35:16: error: ‘Dll’ has no member named ‘head’
dll.c:37:16: error: request for member ‘next’ in something not a structure or union
dll.c:38:14: error: request for member ‘value’ in something not a structure or union
dll.c:38:31: error: request for member ‘next’ in something not a structure or union
dll.c:40:20: error: request for member ‘value’ in something not a structure or union
dll.c:41:12: error: request for member ‘value’ in something not a structure or union
dll.c:41:29: error: request for member ‘next’ in something not a structure or union
dll.c:42:12: error: request for member ‘next’ in something not a structure or union
dll.c:45:21: error: request for member ‘next’ in something not a structure or union

最佳答案

应该是:

typedef struct _Node{
   struct _Node *next;
   struct _Node *prev;
   int value;
}Node;
typedef struct _Dll{
   Node head;
};

typedef 中的拼写错误.. dllDll 不同

另请注意,struct _Dll 中没有head

另外,请告诉我什么是 Node

关于为双向链表创建标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14721956/

相关文章:

c - C 中的动态链接(使用 dlopen)和 header 包含

python - 如何获得 QTableView 标题中单击的右键单击上下文菜单?

PHP xlsx header

c++ - 找到时间 O(n) 和空间 O(1) 的重复有符号整数

c - 读取单词直到行尾

gcc - 用clang编译时无法查看std::string

linux - Linux 内核 2.6 编译期间丢失系统调用错误

C - #include 和多个 typedef

c++ - 从字符串加载 Gdk Pixbuf

c - 字符输入的 Scanf 特定错误条件