请帮我调试这个c代码

时间:2011-03-23 13:04:38

标签: c

这是我编译的方式:gcc -o adt adt.c

#include <stdlib.h>
#define TRUE 1
#define FALSE 0

typedef struct Item* link;
struct Item { 
  char c; 
  int i;
  link next;
  link previous; 
};


static link curr;

void LISTinit ();                  /* initialise the list */
int  LISTempty ();                 /* is the list empty? */
int  LISTmove (int n);             /* move current position */
Item LISTcurrent ();               /* return element at current position */
void LISTbefore (Item newItem);    /* insert element before current */
void LISTafter (Item newItem);     /* insert element after current */
Item LISTdelete ();                /* delete current element */



void LISTinit (){

  curr = NULL;

}


link newNode (int a, char b) {
  link ls;
  ls = malloc (sizeof (*ls));
  ls->i = a;
  ls->c = b;
  ls->next = NULL;
  ls->previous = NULL;
  return ls;     
}

int  LISTempty (){
    if (curr == NULL){
        return TRUE;
    }
    return FALSE;
}


int  LISTmove (int n);{
    int flag = 0;
    if (LISTempty() == TRUE){
        printf("list is empty\n");
        flag = TRUE;    
    }
    else {
        if ( n < 0 ){
            if( curr->previous != NULL){
                curr = curr->previous;
                flag = LISTmove (n+1);

            }
            else {
                flag = TRUE;
            }

        }
        else if( n > 0){
                if( curr->next != NULL){
                    curr = curr->next;
                    flag = LISTmove (n-1);  
                }
                else{
                    flag = TRUE;
                }
        }else{
         if(curr->previous != NULL || curr->next != NULL )flag = TRUE;
         else flag = FALSE;
        } 
    }
    return flag;
}

Item LISTcurrent (){
    return *curr;
}

void LISTbefore (Item newItem){
  newItem->previous =  curr->previous;
  curr->previous = newItem;
  newItem->next = curr;
  curr = &newItem;
}


void LISTafter (Item newItem){
  newItem->next =  curr->next;
  curr->next = newItem;
  newItem->previous = curr;
  curr = &newItem;
}


Item LISTdelete (){
    if( curr->next == NULL){
        curr->previous->next = NULL;
        curr = curr->previous;
    } else if( curr->previous == NULL){
        curr->next->previous = NULL;
        curr = curr->next;  
    } else {
        curr->previous->next = curr->next;
        curr->next->previous = curr->previous;
        curr = curr->previous;
    }
    return *curr;
}

得到了这些错误

adt.c:19: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'LISTcurrent'
adt.c:20: error: expected ')' before 'newItem'
adt.c:21: error: expected ')' before 'newItem'
adt.c:22: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'LISTdelete'
adt.c:51: error: expected identifier or '(' before '{' token
adt.c:85: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'LISTcurrent'
adt.c:89: error: expected ')' before 'newItem'
adt.c:97: error: expected ')' before 'newItem'
adt.c:105: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'LISTdelete'

谢谢! 现在有另一个问题

#include <stdlib.h>
#include <stdio.h>
#define TRUE 1
#define FALSE 0

typedef struct item Item;
typedef Item* link;

struct item {
  char c; 
  int i;
  link next;
  link previous; 
};


static link curr;

void LISTinit ();                  /* initialise the list */
int  LISTempty ();                 /* is the list empty? */
int  LISTmove (int n);             /* move current position */
Item LISTcurrent ();               /* return element at current position */
void LISTbefore (Item newItem);    /* insert element before current */
void LISTafter (Item newItem);     /* insert element after current */
Item LISTdelete ();                /* delete current element */



void LISTinit (){

  curr = NULL;

}


link newNode (int a, char b) {
  link ls;
  ls = malloc (sizeof (*ls));
  ls->i = a;
  ls->c = b;
  ls->next = NULL;
  ls->previous = NULL;
  return ls;     
}

int  LISTempty (){
    if (curr == NULL){
        return TRUE;
    }
    return FALSE;
}


int  LISTmove (int n){
    int flag = 0;
    if (LISTempty() == TRUE){
        printf("list is empty\n");
        flag = TRUE;    
    }
    else {
        if ( n < 0 ){
            if( curr->previous != NULL){
                curr = curr->previous;
                flag = LISTmove (n+1);

            }
            else {
                flag = TRUE;
            }

        }
        else if( n > 0){
                if( curr->next != NULL){
                    curr = curr->next;
                    flag = LISTmove (n-1);  
                }
                else{
                    flag = TRUE;
                }
        }else{
         if(curr->previous != NULL || curr->next != NULL )flag = TRUE;
         else flag = FALSE;
        } 
    }
    return flag;
}

Item LISTcurrent (){
    return *curr;
}

void LISTbefore (Item newItem){
  newItem->previous =  curr->previous;
  curr->previous = newItem;
  newItem->next = curr;
  curr = &newItem;
}


void LISTafter (Item newItem){
  newItem->next =  curr->next;
  curr->next = newItem;
  newItem->previous = curr;
  curr = &newItem;
}


Item LISTdelete (){
    if( curr->next == NULL){
        curr->previous->next = NULL;
        curr = curr->previous;
    } else if( curr->previous == NULL){
        curr->next->previous = NULL;
        curr = curr->next;  
    } else {
        curr->previous->next = curr->next;
        curr->next->previous = curr->previous;
        curr = curr->previous;
    }
    return *curr;
}

错误

gcc -o adt adt.c
adt.c: In function 'LISTbefore':
adt.c:93: error: invalid type argument of '->' (have 'Item')
adt.c:95: error: invalid type argument of '->' (have 'Item')
adt.c: In function 'LISTafter':
adt.c:101: error: invalid type argument of '->' (have 'Item')
adt.c:103: error: invalid type argument of '->' (have 'Item')

3 个答案:

答案 0 :(得分:2)

Item LISTcurrent ();

应该是

struct Item LISTcurrent ();

您已声明struct名为Item,但不是类型。如果您希望每次引用struct时都不必输入Item,那么您也可以为其引入类型:

typedef struct Item Item;

答案 1 :(得分:1)

在C中,您无法声明struct Item,然后将其用作名为Item的类型;你必须将其称为struct Item。作为其对象实现的一部分,C ++与此不同(因为classstruct,其成员默认为private;请注意C不具有private或者)。

答案 2 :(得分:1)

对于您的新错误,请更改LISTbeforeLISTafter功能,以便通过指针(LISTbefore(Item* newItem))获取参数或使用.代替->访问newItem的元素。