2012-08-15 60 views
-2

我在C中有一個單獨鏈接列表程序。當我在TC++上編譯它時,它只有2個關於某些聲明(它的罰款)的錯誤。但是當我使用GCC在Ubuntu中編譯它時,它有太多的錯誤。我爲該結構的成員創建了一個名爲NODE的自定義數據類型,但GCC不接受它。正如我已經使用typedef,有一個錯誤說 -C編寫結構的規則是否因Turbo C++編譯器和GCC而異?

expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘struct’ 

任何規則我錯過了?請幫幫我!


這是代碼:

#include<stdio.h> 
typdef struct node 
{ 
    int data; 
    NODE *next; 
}NODE; 

//Creation Of the Nodes with NULL pointers 
NODE* createnewnode() 
{ 
    NODE* nn; 
    nn=(NODE*)malloc(sizeof(NODE)); 
    if(nn==NULL) 
    { 
     printf("Insufficient Memory"); 
     exit(0); 
    } 
    printf("Enter data"); 
    scanf("%d",&nn->data); 
    nn->next=NULL; 
    return(nn); 
} 


// Creation Of the Links 
NODE* createlinkedlist(NODE *hn, int n) 
{ 
    NODE *cn, *nn; 
    for(i=0;i<n;i++); 
    { 
     nn=createnewnode(); 
     if(hn==NULL) 
     { 
      hn=nn; 
     } 
else 
     { 
      cn->next==nn; 
     } 
    cn=nn; 
    return(hn); 
} 

//Display of The Data 
void display(NODE *hn) 
{ 

    NODE *cn; 
    for(cn=hn;cn!=NULL;cn=cn->next) 
    { 
     printf("\t %d, "cn->data); 
    } 
} 

//Linear Searching 
void search(NODE *hn, int n) 
{ 
    NODE *cn; 
    int i, x; 
    printf("Enter the data to be found"); 
    scanf("%d",&x); 
    i=0; 
    while(i<n) 
    { 
     if(x==cn->data) 
     { 
      printf("Data found at %d",i+1); 
      break; 
     } 

     cn=cn->next; 
     i=i++; 
    } 
} 

void main() 
{ 
    int n; 
    NODE* hn=NULL; 
    printf("Enter the number of nodes to be created"); 
    scanf("%d",&n); 
    createlinkedlist(hn,n); 
    display(hn); 
} 

+4

拼寫錯誤'typedef'。 – chris 2012-08-15 08:37:13

+0

如果編譯器給出任何形式的錯誤,代碼就不好。不同的編譯器在第一個之後可能會也可能不會顯示相關的錯誤,但不要指望它。第二個錯誤可能與第一個錯誤有關。 – Lundin 2012-08-15 09:41:39

回答

1

即使糾正typedef你不能使用NODE了自己的聲明中。做NODE預先聲明第一,甚至更好只是node叫它:

typedef struct node node; 
struct node { 
    ... 
    node* next; 
}; 

編輯:等吹毛求疵:

  • 不投的malloc回報。如果你覺得有必要,你在別的地方錯了。 (在這裏,你不包括stdlib.h
  • 因爲長C沒有變量的默認類型了。你的icreatelinkedlist是未知的,沒有現代編譯器會讓這個通過
  • 你至少有一個地方使用==運營商,而不是轉讓=。始終在啓用所有警告的情況下進行編譯,任何體面的編譯器都應該已經找到了。
+0

或刪除typedef並將其稱爲'struct node'。 – 2012-08-15 09:12:50

+0

非常感謝Jens Gustedt。我知道'我'沒有申報。我只是想在'struct''之前得到錯誤 'expected'=',',',';','asm'或'__attribute__'的解決方案。 將盡快找回解決方案。 – 2012-08-16 12:13:53

+0

好吧,我嘗試瞭解決方案,我寫了你提到的結構,並用'node'替換了所有'NODE'。但仍然有這樣的錯誤 http://imageshack.us/f/24/hhhhua.png/ 請忽略未聲明的'我'。 – 2012-08-16 12:25:00

0

//糾正上述問題代碼,但程序員的某些邏輯不正確。

#include <cstdlib> 
#include <iostream> 
#include<stdio.h> 

using namespace std; 

typedef struct node // typedef spell 
{ 
    int data; 
    node *next; // NODE was in upper case it should be in lower case 
    node() //Constructor : i defined it in traditional way as extra 
    { 
     next = NULL; 
     data = 0; 
    } 

}NODE; 


//Creation Of the Nodes with NULL pointers 
NODE* createnewnode() 
{ 
    NODE* nn; 
    nn=(NODE*)malloc(sizeof(NODE)); 
    if(nn==NULL) 
    { 
     printf("Insufficient Memory"); 
     exit(0); 
    } 
    printf("Enter data"); 
    scanf("%d",&nn->data); 
    nn->next=NULL; 
    return(nn); 
} 


// Creation Of the Links 
NODE* createlinkedlist(NODE *hn, int n) 
{ 
    NODE *cn, *nn; 
    int i;   // int i, was not defined, more correction fixed 
    for(i=0;i<n;i++); 
    { 
     nn=createnewnode(); 
     if(hn==NULL) 
     { 
      hn=nn; 
     } 
else 
     { 
      cn->next==nn; 
     } 
    cn=nn; 
    return(hn); 
} 
} 
//Display of The Data 
void display(NODE *hn) 
{ 

    NODE *cn; 
    for(cn=hn;cn!=NULL;cn=cn->next) 
    { 
     printf("\t %d", cn->data); 
    } 
} 

//Linear Searching 
void search(NODE *hn, int n) 
{ 
    NODE *cn; 
    int i, x; 
    printf("Enter the data to be found"); 
    scanf("%d",&x); 
    i=0; 
    while(i<n) 
    { 
     if(x==cn->data) 
     { 
      printf("Data found at %d",i+1); 
      break; 
     } 

     cn=cn->next; 
     i=i++; 
    } 
} 

int main() 
{ 
    int n; 
     NODE* hn=NULL; 
    printf("Enter the number of nodes to be created"); 
    scanf("%d",&n); 
    createlinkedlist(hn,n); 
    display(hn); 
    system("pause"); 
    return 0; 
} 
+0

atleast評論什麼是糾正和什麼不是 – 2015-11-17 07:57:39