簡體   English   中英

在c中編譯鏈表的錯誤

[英]Compiling error for linked list in 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

在C中,這個定義

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

必須被稱為struct element 你在某些地方做得對,而不是全部。 一個已知的習語是:

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

哪個typedefs element作為struct element因此您不需要在該類型前面放置struct 這是如此常見,以至於C ++會“為你做這件事”,因此不再需要struct

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

您的問題與聲明struct element與使用方式有關。

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

會給你更滿意的結果。

或者,您可以保留struct ,無論您使用裸element ,還是編寫struct element

BTW:

這與你的確切問題無關,但我想我會分享(因為我的編譯器給了我一個關於它的警告); 你有時會寫:

current->i = NULL;

在這種情況下, i是整數。 在C中, NULL傳統上用於指針類型。 為了不混淆,您應該將作業更改為:

current->i = 0;

...或者,如果i實際上指某個地方的內存(給定上面的上下文,我認為它不是),將其聲明為指針類型。

如果你打算以這種方式使用它,你需要輸入你的結構。 否則,您需要在任何地方使用struct關鍵字。 檢查文檔

所以你想說:

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

當你聲明元素時。 否則,編譯器每次使用它時都不知道該元素是一個結構。

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));
   ...
 }

函數返回后,調用者可以使用指針,它有望指向新的小丑。 就這樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM