Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Question regarding an error message in my compiler to do with my code on linked list.

Post

Question regarding an error message in my compiler to do with my code on linked list.

+4
−2

Can anyone help me, I'm currently learning pointers, this is the code I wrote to try and insert a node at the beginning of the list. However at the part where I included the comment of "error at this line" basically my compiler gave me the error of "In function Insert: Node undeclared". Why is it so?

#include<stdlib.h>
#include<stdio.h>
struct Node{
    int data;
    struct Node* next; 
};

struct Node* head;
void Insert(int x){ 
    Node* temp = (Node*)malloc(sizeof(struct Node)); //error at this line
    temp->data  = x; 
  
    temp->next = head;
   
    head = temp;

    if(head != NULL)
            temp->next = head; 

    head = temp;
}
void Print(){
    struct Node* temp = head; 
 
    printf("List is: ");
 
    while(temp != NULL){
        printf("%d", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

int main(){
    head = NULL; //empty list
    printf("How many numbers?\n");
    int n, i;
    scanf("%d", &n);
    for(int i=0; i < n; i++){
        printf("Enter the number\n");
        scanf("%d", &x);
        Insert(x); 
        Print();
    }
}

First edit:

Update: My program runs, lets me enter the amount of numbers I want, as well as the numbers, however it prints out only the latest number I entered. What could possibly be the problem? Thanks.

#include<stdlib.h>
#include<stdio.h>
struct Node{
    int data;
    struct Node* next; 
};

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

    if(head != NULL)
            temp->next = head; 
    head = temp;
    temp->next = NULL;
}

void Print(){
    struct Node* temp = head; 
    printf("List is: ");
 
    while(temp != NULL){
        printf("%d", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

int main(){
    head = NULL; //empty list
    printf("How many numbers?\n");
    int n, i;
    scanf("%d", &n);
    for(int i=0; i < n; i++){
        printf("Enter the number\n");
        scanf("%d", &x);
        Insert(x); 
        Print();
    }
}
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

You are attempting to use a data type "Node", but the data type you have defined is a "struct Node"... (4 comments)
You are attempting to use a data type "Node", but the data type you have defined is a "struct Node"...
elgonzo‭ wrote almost 3 years ago · edited almost 3 years ago

See comment topic title, obviously...

(There is a way to make the data type available as a data type named "Node", but i won't tell you here right now because i believe doing so would confuse you even more...)

hamburgersarecool‭ wrote almost 3 years ago · edited almost 3 years ago

After I added the struct to become " struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); " , the program ran. But after entering the amount of numbers I wanted as well as entering the first number, it printed out a loop of that "first number" I entered, infinitely. Why is it like that?

elgonzo‭ wrote almost 3 years ago · edited almost 3 years ago

Why is it like that?

If you have difficulties understanding what your program is doing and just looking at the source code doesn't help you get an understanding, then it's time to use a debugger to inspect and observe what your code/program is actually doing, and why it behaves the way it does... :-)

If you don't know yet how to use a debugger to your advantage, this is an excellent time learning how to do so. I don't know what build tools/IDE you are using and thus what debugger(s) are available to you. But in case you are using Visual Studio, here is a neat tutorial for Visual Studio's C/C++ debugger: https://docs.microsoft.com/en-us/visualstudio/debugger/getting-started-with-the-debugger-cpp?view=vs-202

elgonzo‭ wrote almost 3 years ago · edited almost 3 years ago

I am not trying to be facetious or snarky here. A debugger is a fundamental software development tool. I would go as a far as saying that a debugger is almost as fundamental as the text editor function required to write and edit source code and the compiler or interpreter required for processing the source code. Trying to write software without being able to debug your own code is like attempting to drive a car with all tires flat. It might just work out with skill and a sensitive steering hand, but don't start complaining if you can't keep the car on the road. Sure, you could drive very very slowly, but then you would still not get anywhere in any reasonable time... ;-)