пожалуйста, помогите мне отладить этот код C - PullRequest
0 голосов
/ 23 марта 2011

вот как я скомпилировал: 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 ]

2 голосов
/ 23 марта 2011
Item LISTcurrent ();

должно быть

struct Item LISTcurrent ();

и т.д.

Вы объявили struct с именем Item, но не тип. Если вы не хотите вводить struct каждый раз, когда ссылаетесь на Item, вы также можете ввести для него тип:

typedef struct Item Item;
1 голос
/ 23 марта 2011

Для вашей новой ошибки измените функции LISTbefore и LISTafter, чтобы они брали свои параметры по указателям (LISTbefore(Item* newItem)) или использовали . вместо -> для доступа к элементам newItem.

1 голос
/ 23 марта 2011

В C вы не можете объявить struct Item и затем использовать его как тип с именем Item; вы должны ссылаться на него как struct Item. C ++ отличается в этом как часть реализации своего объекта (поскольку class - это struct, членами которого по умолчанию являются private; обратите внимание, что в C также нет private).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...