我在mac上的NetBeans中完成了我的整个项目,它运行得很好。然后我把它放进终端,在学校的ssh服务器上提交,它给了我一堆编译错误。netbeans是用c++编译的吗?我是c的新手,所以任何帮助都是非常感谢的。这是代码。
/*
* File: LinkedList.c
* Author: dpiganell
*
* Created on October 17, 2011, 2:31 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct element{
int i;
struct element *next;
};
void insert(struct element**, struct element*);
void purge (struct element*, struct element*);
void printList(struct element*);
void printListBackwards(struct element*);
struct element *h;
int main()
{
// HEAD
h = (element *) malloc(sizeof(element));
h = NULL;
// NEW VALUE
struct element *n = NULL;
// POINTER TO THE POINTER OF HEAD
struct element **headPointer;
headPointer = (element **) malloc(sizeof(element));
int a;
a = 1;
while(a >= 0)
{
n = (element *) malloc(sizeof(element));
printf("nPlease enter an integer value: ");
scanf("%d", &a);
if(a >= 0)
{
n->i = a;
n->next = NULL;
headPointer = &h;
insert(headPointer, n);
n = n++;
}
printList(h);
printListBackwards(h);
}
return 0;
}
void insert(struct element **head, struct element *n)
{
element *nptr, *pptr, *cptr;
// NEW POINTER, PREVIOUS POINTER, CURRENT POINTER
nptr = (element *) malloc(sizeof(element));
int purged;
purged = -1;
if(nptr != NULL)
{
nptr->i = n->i;
nptr->next = NULL;
cptr = *head;
pptr = NULL;
}
while(cptr != NULL && n->i >= cptr->i)
{
if(cptr->i == n->i && purged != 1)
{
purged = 1;
purge(cptr, pptr);
}
else
{
pptr = cptr;
cptr = cptr->next;
}
}
if(pptr == NULL && purged < 0)
{
nptr->next = *head;
*head = nptr;
}
else if(purged < 0)
{
pptr->next = nptr;
nptr->next = cptr;
}
}
void purge(struct element *current, struct element *predecessor)
{
element *ptr = (element *) malloc(sizeof(element));
// ERASING THE HEAD
if(predecessor == NULL)
{
if(current->next == NULL)
{
current->next = NULL;
current->i = NULL;
current = NULL;
free(current);
h = NULL;
}
else
memcpy(current, current->next, sizeof(element));
}
// ERASING THE TAIL
else if(current->next == NULL)
{
current->i = NULL;
free(current->next);
free(current);
predecessor->next = NULL;
}
// ERASING ANYTHING IN THE MIDDLE OF THE LIST
else
{
ptr = current->next;
predecessor->next = ptr;
current->i = NULL;
free(current->next);
}
}
void printList(struct element *head)
{
if(head == NULL)
printf("The list is empty.");
else
{
struct element *ptr;
ptr = (element*) malloc(sizeof(element));
ptr = head;
int a;
a = 1;
printf("Forwards: ");
while(a > 0)
{
printf("%d ", ptr->i);
if((ptr->next) == NULL)
a = -1;
else
ptr = ptr->next;
}
}
}
void printListBackwards(struct element *ptr)
{
if(ptr == NULL)
{
// DO NOTHING BECAUSE IT WILL BE PRINTED ALREADY THAT IT IS EMPTIED
}
else
{
element *cptr = (element *) malloc(sizeof(element));
cptr = ptr;
if(ptr->next == NULL)
printf("nBackwards: %d ", ptr->i);
else
{
printListBackwards(ptr->next);
printf("%d ", ptr->i);
}
}
}
此外,以下是错误。它在netbeans中运行良好,编译良好。
LinkedList.c:14: warning: useless keyword or type name in empty declaration
LinkedList.c: In function `main':
LinkedList.c:26: error: `element' undeclared (first use in this function)
LinkedList.c:26: error: (Each undeclared identifier is reported only once
LinkedList.c:26: error: for each function it appears in.)
LinkedList.c:26: error: syntax error before ')' token
LinkedList.c:34: error: syntax error before ')' token
LinkedList.c:40: error: syntax error before ')' token
LinkedList.c: In function `insert':
LinkedList.c:65: error: `element' undeclared (first use in this function)
LinkedList.c:65: error: `nptr' undeclared (first use in this function)
LinkedList.c:65: error: `pptr' undeclared (first use in this function)
LinkedList.c:65: error: `cptr' undeclared (first use in this function)
LinkedList.c:68: error: syntax error before ')' token
LinkedList.c: In function `purge':
LinkedList.c:110: error: `element' undeclared (first use in this function)
LinkedList.c:110: error: `ptr' undeclared (first use in this function)
LinkedList.c:110: error: syntax error before ')' token
LinkedList.c: In function `printList':
LinkedList.c:153: error: `element' undeclared (first use in this function)
LinkedList.c:153: error: syntax error before ')' token
LinkedList.c: In function `printListBackwards':
LinkedList.c:179: error: `element' undeclared (first use in this function)
LinkedList.c:179: error: `cptr' undeclared (first use in this function)
LinkedList.c:179: error: syntax error before ')' token
在C中,这个定义是
struct element{
int i;
struct element *next;
};
必须称为CCD_ 1。你在某些地方做对了,而不是全部。一个众所周知的成语是:
typedef struct element {
int i;
struct element *next;
} element;
哪个类型将element
定义为struct element
,这样就不需要将struct
放在类型前面。这太常见了,以至于C++有点"为你做这件事",所以不再需要struct
。
它是C,不能使用裸element
,它是struct element
。
您的问题与如何声明struct element
以及如何使用它有关。
typedef struct element{
int i;
struct element *next;
} element;
会给你更满意的结果。
或者,您可以将struct
保持原样,无论您在哪里使用裸struct element
0,都可以编写struct element
。
BTW:
这与你的确切问题无关,但我想我会分享(因为我的编译器给了我一个警告(;你有时会写:
current->i = NULL;
在这种情况下,i
是一个整数。在C中,NULL
传统上仅用于指针类型。为了避免混淆,您应该将分配更改为:
current->i = 0;
或者,如果i
实际上是,意味着指向某个地方的内存(给定上面的上下文,我认为不是(,则将其声明为指针类型。
如果要以这种方式使用结构,则需要对其进行typedef。否则,您需要到处使用struct关键字。检查文件
所以你想说:
typedef struct element {
int i;
element *next;
} element;
当您声明元素时。否则,每次使用元素时,编译器都不知道它是一个结构。
h = (element *) malloc(sizeof(element));
h = NULL;
headPointer = (element **) malloc(sizeof(element));
这是您的第一个错误。元素是struct标记,而不是typedef。(这已经由其他人处理了。(
循环中:
headPointer = &h;
两个指针都被分配了(malloc((d(值。通过重新分配它们,您将丢失以前malloc((d的内容。
"headPointer"是个好主意。它是一个可以指向其他指针的指针。您可以使用它来让被调用的函数更改调用方指针上的值。但是你的用法不对。通常,呼叫者应该这样做:
struct clown *pipo=NULL;
...
my_function( &pipo);
在函数内部,调用方的指针可以更改:
void my_function (struct clown **ppp) {
...
*ppp = malloc (sizeof (struct clown));
...
}
函数返回后,调用方可以使用指针,它有望指向新的小丑。仅此而已。