-2

私はCで単一リンクリストプログラムを持っています.TC ++でコンパイルすると、いくつかの宣言に関して2つのエラーしかありません(問題ありません)。しかし、GCCを使用してUbuntuでコンパイルすると、エラーが多すぎます。構造体のメンバー用に NODE というカスタム データ型を作成しましたが、GCC はそれを受け入れません。そして、私はtypedefを使用しているので、次のようなエラーがあります-

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

2 に答える 2

1

修正しても、独自の宣言内でtypedefは使用できません。firstNODEの前方宣言を行うか、単に:NODEnode

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

編集:およびその他のちょっとしたこと:

  • のリターンをキャストしないでmallocください。あなたがそれの必要性を感じているなら、あなたはどこかで間違っています. (ここでは含めませんstdlib.h
  • long C には、変数のデフォルトの型がなくなったためです。あなたのiインcreatelinkedlistは不明であり、最新のコンパイラはこれを通過させません
  • ==割り当ての代わりに演算子を使用する場所が少なくとも 1 つあります=。常にすべての警告を有効にしてコンパイルしてください。適切なコンパイラはそれを検出するはずです。
于 2012-08-15T09:10:07.160 に答える
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;
}
于 2015-11-17T07:36:36.603 に答える