所以我必须创建一些代码来制作一个列表并对它做各种事情。专门打印它,对其进行排序,然后查看其中是否包含值。做到了,运行良好。现在我必须使用这些函数并将它们拆分为单独的文件,然后使用 gcc -c (我不确定我是否正确使用)来获取 .o 文件,以及用于测试程序的 .o . 然后我必须使用 gcc 将我的 .o 文件链接到一个可执行文件中。提示说它将识别 .o 并实现如何链接它们。
所以这是我的问题:为什么下面的代码会返回错误(以下面定义的方式)?我到底应该在命令行中写入什么来链接这些人?
所以代码如下:(首先是 .h 文件,然后是主 .c 文件)
节点.h
typedef struct Node{
int data;
struct Node *next;
struct Node *prev;
}node;
打印.h
#include<stdio.h>
#include"node.h"
void print(node *pointer){
if (pointer == NULL){
return;
}
printf("%d ",pointer->data);
print(pointer->next);
}
初始化.h
#include<stdio.h>
#include"node.h"
int init(node *pointer,int find){
pointer = pointer->next;
while (pointer != NULL){
if (pointer->data == find)//found find
{
printf("The data is in the list.");
return 1;
}
pointer = pointer->next;// Search in the next node.
}
//find is not found
printf("The data is not in the list.");
return 0;
}
排序.h
#include<stdio.h>
#include"node.h"
void swap (node *x, node *y){
int temp = x->data;
x->data = y->data;
y->data = temp;
}
void sort(node*pointer){
int i;
while (pointer->next != NULL){
if (pointer->data>pointer->next->data){
swap(pointer,pointer->next);
}
pointer = pointer->next;
sort(pointer);
}
}
列表.c
#include<stdio.h>
#include<stdlib.h>
#include"node.h"
#lnclude"print.h"
#include"sort.h"
#include"init.h"
int i;
node *p;
node *n;
void insert(node *pointer, int data){
//go through list till ya find the last node
while (pointer->next != NULL){
pointer = pointer->next;
}
//allocate memory for new node and put data in it
pointer->next = (node *)malloc(sizeof(node));
(pointer->next)->prev = pointer;
pointer = pointer->next;
pointer->data = data;
pointer->next = NULL;
}
int main(){
//start is used to point to the first node
//temp is for the last node
node *start, *temp;
int z;
start = (node *)malloc(sizeof(node));
temp = new;
temp->next = NULL;
temp->prev = NULL;
for (z = 0; z < 10; z++){
insert(start,(3*10) - z);
}
init(start,12);
init(start,3);
init(start,27);
init(start,7);
print(start);
sort(start);
print(start);
}
现在,除了 node.h (它始终是一个单独的文件)之外,所有代码都运行得非常好。.h 文件将完美编译,但是当我尝试编译 .c 文件时,它返回错误,声称我正在尝试重新定义每个 .h 文件中的节点。这可能是因为我将它包含在每个 .h 文件中吗?
我也收到错误,我试图将不适当的参数传递给 init 函数,这似乎并非如此。但我可能只是在看东西。
预先感谢您的任何帮助。
编辑:在输入“gcc list.c”时将 main 中的变量从 new 更改为 start 错误
In file included from init.h:2:0,
from list.c:4:
node.h:1:16 error: redefinition of'struct Node'
node.h:1:16 note: originally defined here
node.h:5:2 error: conflicting types for 'node'
node.h:5:2 note: previous declaration of 'node' was here
In file included from sort.h:2:0;
from list.c:5:
node.h:1:16 error: redefinition of'struct Node'
node.h:1:16 note: originally defined here
node.h:5:2 error: conflicting types for 'node'
node.h:5:2 note: previous declaration of 'node' was here
list.c: In function 'main':
list.c:41:1: warning: passing argument 1 of 'init' from incompatible pointer type[enabled by default]
init.h:4:5: note: expected 'struct node *' but argument is of type 'struct node *'
(然后main中的每个单独的init调用都有错误)
list.c:45:1: warning:passing argument 1 of 'print' from incompatible pointer type [enabled by default]
print.h:3:6: note expected 'struct node *' but argument is of type 'struct node *'
(然后还有另一个用于第二个打印功能)