Question

I have a singly linked list program in C. When i compile it on TC++ it has only 2 errors regarding some declaration(its fine). But when i compile it in Ubuntu using GCC, it has way too many errors. I have created a custom datatype called NODE for the members of the structure, but GCC wont accept it. And as i have used typedef, there is an error which says -

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

Any rules i am missing out? Please help me!


This is the code:

#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);
}

Was it helpful?

Solution

Even with corrected typedef you can't use NODE inside its own declaration. Do a forward declaration of NODE first, or even better call it just node:

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

Edit: and other nitpicks:

  • don't cast the return of malloc. If you feel the need for it, you are wrong somewhere else. (Here that you don't include stdlib.h)
  • since long C has no default types for variables anymore. Your i in createlinkedlist is unknown and no modern compiler will let this through
  • you have at least one place where you use a == operator instead of an assignment =. Always compile with all warnings enabled, any decent compiler should have found that.

OTHER TIPS

// correction of above asked code, but some logics of programmer are not /correct.

#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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top