在c中编译链表的错误

时间:2011-10-20 23:08:33

标签: 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);
        }
    }
}

此外,还有错误。它工作正常,并且在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

5 个答案:

答案 0 :(得分:5)

在C中,这个定义

struct element{
    int i;
    struct element *next;
};

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

typedef struct element { 
    int i;
    struct element *next;
} element;

哪个typedef elementstruct element,因此您无需将struct放在该类型的前面。这种情况非常普遍,以至于C ++会“为你做这件事”,因此不再需要struct

答案 1 :(得分:4)

它是C,你不能使用裸element,它是struct element

答案 2 :(得分:1)

您的问题与您宣布struct element与使用方式的方式有关。

typedef struct element{
    int i;
    struct element *next;
} element;

会给你更满意的结果。

或者,您可以保留struct原样,只要您使用裸element,而不是写struct element

<强>顺便说一句:

这与您的确切问题无关,但我想我会分享(因为我的编译器给了我一个关于它的警告);你有时会写:

current->i = NULL;

在这种情况下,i是一个整数。在C中,NULL传统上仅使用 作为指针类型。为了不混淆,您应该将作业更改为:

current->i = 0;

...或者,如果i实际上 意味着指向某处的内存(给定上面的上下文,我认为它不是),将其声明为指针类型。

答案 3 :(得分:1)

如果要以这种方式使用它,则需要输入你的结构。否则,您需要在任何地方使用struct关键字。查看documentation

所以你想说:

typedef struct element {
   int i;
   element *next;
} element;

声明元素时。否则,编译器每次使用它时都不知道该元素是一个结构。

答案 4 :(得分:0)

h = (element *) malloc(sizeof(element));
h = NULL;
headPointer = (element **) malloc(sizeof(element));

这是你的第一个错误。 element是结构标记,而不是typedef。 (这已由其他人处理。)

在循环中:

headPointer = &h;

两个指针都已分配(malloc()d)值。通过重新分配它们,你会丢失以前的malloc()d。

'headPointer'的东西是个好主意。它是一个可以指向其他指针的指针。您可以使用它来让被调用的函数改变调用者指针的on值。但是你的用法不对。通常情况下,来电者应该这样做:

struct clown *pipo=NULL;
...
my_function( &pipo);

在函数内部,调用者的指针可以改变:

void my_function (struct clown **ppp) {
   ...
   *ppp = malloc (sizeof (struct clown));
   ...
 }

函数返回后,调用者可以使用指针,它有望指向新的小丑。 就是这样。