[英]please help me debug this c code
this is how i compiled: gcc -o adt adt.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;
}
got these errors 得到了这些错误
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'
Thanks! 谢谢! now have another problem
现在有另一个问题
#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;
}
error 错误
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')
Item LISTcurrent ();
should be 应该
struct Item LISTcurrent ();
etc. 等等
You have declared a struct
called Item
, but not a type. 您已经声明了一个名为
Item
的struct
,但没有声明类型。 If you want to avoid having to type out struct
every time you refer to an Item
, you can introduce a type for it as well: 如果您希望避免每次引用
Item
都必须键入struct
,则也可以为其引入类型:
typedef struct Item Item;
In C, you can't declare a struct Item
and then use it as a type named Item
; 在C语言中,您不能声明
struct Item
,然后将其用作名为Item
的类型; you must refer to it as struct Item
. 您必须将其称为
struct Item
。 C++ differs about that, as part of its object implementation (since a class
is a struct
whose members are private
by default; note that C doesn't have private
either). C ++在其对象实现方面有所不同(因为
class
是默认情况下成员为private
的struct
;请注意,C也不具有private
)。
For your new error, change the LISTbefore
and LISTafter
function to either take their parameter by pointers ( LISTbefore(Item* newItem)
) or use .
对于新错误,请将
LISTbefore
和LISTafter
函数更改为通过指针( LISTbefore(Item* newItem)
)或使用来获取其参数.
instead of ->
to access elements of newItem
. 而不是
->
访问newItem
元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.