Я выполнил весь свой проект в NetBeans на моем mac, и он отлично работает. Затем я отправляюсь на терминал, чтобы отправить его на наш ssh-сервер для школы, и это дает мне кучу ошибок компиляции. Разве netbeans компилируется в C++ или что-то в этом роде? Я новичок в 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
Вы пытаетесь использовать тип, который вы не определяли. –
Вы уверены, что указали нам * точный * код, который дал вам эти ошибки? Когда я его компилирую, первая ошибка, которую я вижу, заключается в том, что 'element' не объявлен (в C это имя типа' struct element'). Я также не могу воспроизвести ошибки, которые вы видите на C++. –
@KeithThompson Ошибки не на C++. На этот вопрос был дан ответ, хотя спасибо за помощь. Я скомпилировал его в netbeans в компиляторе C++, и он сработал. Но когда я использовал gcc, это не работало, чтобы просто разобраться. Всем спасибо. –