-2

Here is my Code. A Simple FILO Linked List but the error i'm getting is weird. its on the Insert Function where i declare temp, saying Node is undeclared. Don't understand how Ive done this like 10 times and no errors. Thank you for your time.

//Linked List: Inserting a node at the beginning (FILO)

#include <stdio.h>
#include <stdlib.h>

struct Node{
    int data;
    struct Node* next;
};

struct Node* head; //Global


void Insert (int x)
{
    struct Node* temp = (Node*) malloc(sizeof(struct Node));
    temp->data = x;
    temp->next = head;
    head = temp;
}

void Print()
{
    struct Node* temp = head;  //We use a temporary because we dont want to lose the reference to the head
    printf ("List is: ");

    while(temp != NULL)
    {
        printf (" %d", temp->data);
        temp = temp -> next;
    }
    printf ("\n");
}


int main(){

    head = NULL; //empty list
    int n,i,x;

    printf ("How many numbers?\n");
    scanf ("%d",&n);

    for (i=0; i<n; i++)
    {
        printf ("Enter the Number\n");
        scanf ("%d",&x);
        Insert(x);
        Print();
    }
}
alk
  • 69,737
  • 10
  • 105
  • 255
bob dole
  • 1
  • 2
  • 3
    `(Node*)` delete this or change to `(struct Node*)` – BLUEPIXY Dec 14 '15 at 08:30
  • 4
    General warning:[do not cast malloc return](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – LPs Dec 14 '15 at 08:30
  • 1
    Besides the fact that casting `malloc()` & Friends is not needed in C (nor recommended) `Node` is not defined, as C is not C++. The code just defines `struct Node`, so use `struct Node`. – alk Dec 14 '15 at 09:07
  • Your program won't compile as *Node* (without struct) is undefined. – August Karlstrom Dec 14 '15 at 09:40
  • You misspelt `struct Node` when casting the return value from `malloc()`. The good news is that you needn't (and shouldn't) cast `void *` like that. Instead, simply write `struct Node* temp = malloc(sizeof *temp);` – Toby Speight Jan 16 '18 at 15:44

1 Answers1

4

DO NOT CAST MALLOC RETURN

The solution of your problem is simply to avoid casting malloc return or use the correct type: struct node*

#include <stdio.h>
#include <stdlib.h>

struct Node{
    int data;
    struct Node* next;
};

struct Node* head; //Global


void Insert (int x)
{
    struct Node* temp = malloc(sizeof(struct Node));
    temp->data = x;
    temp->next = head;
    head = temp;
}

void Print()
{
    struct Node* temp = head;  //We use a temporary because we dont want to lose the reference to the head
    printf ("List is: ");

    while(temp != NULL)
    {
        printf (" %d", temp->data);
        temp = temp -> next;
    }
    printf ("\n");
}


int main(){

    head = NULL; //empty list
    int n,i,x;

    printf ("How many numbers?\n");
    scanf ("%d",&n);

    for (i=0; i<n; i++)
    {
        printf ("Enter the Number\n");
        scanf ("%d",&x);
        Insert(x);
        Print();
    }

    return 0;
}
Community
  • 1
  • 1
LPs
  • 16,045
  • 8
  • 30
  • 61