在c中编译链表的错误

Dom*_*ell 2 c

我在我的Mac上用NetBeans完成了我的整个项目,它运行正常.然后我把它放到终端,在学校的ssh服务器上提交它,它给了我一堆编译错误.netbeans是用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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,这是错误.它工作正常,在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
Run Code Online (Sandbox Code Playgroud)

K-b*_*llo 5

在C中,这个定义

struct element{
    int i;
    struct element *next;
};
Run Code Online (Sandbox Code Playgroud)

必须被称为struct element.你在某些地方做得对,而不是全部.一个已知的习语是:

typedef struct element { 
    int i;
    struct element *next;
} element;
Run Code Online (Sandbox Code Playgroud)

这类型定义elementstruct element这样你就不会需要放置struct在类型的前面.这是如此常见,以至于C++类似"为你做这件事"所以struct不再需要了.