为双向链表创建标题

时间:2013-02-06 05:04:06

标签: c gcc header doubly-linked-list

所以我正在上一门关于操作系统的课程,我们已经开始了这个学期的C编程速成课程。我们的前两个任务很简单,但我不能为我的生活做出这一点。

所以我们的教授让他的TA创建了以下.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

2 个答案:

答案 0 :(得分:1)

应该是:

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

typedef中的错误... dllDll不一致

另请注意,head

中没有struct _Dll

另外,请告诉我Node是什么。

答案 1 :(得分:0)

在问题代码中,您有两个类型名称:

struct Dll

然后输入typedef

dll

关键字struct很重要,在C中你必须使用它,不像C ++。然后在你的代码中你只使用Dll,它不是 C 编译器的已知符号。

这样做也没关系,不需要使用不同的名称:

typedef struct my_struct { ... } my_struct;

排队代码中的另一个问题是,你有参数名称dll,以及typedef名称dll。