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.
Post History
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 th...
#4: Post edited
- 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?
- ```//Inserting a node at the beginning of linked list
- #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:
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.- ```//Inserting a node at the beginning of linked list
- #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();
- }
- }
- ```
- 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?
- ```//Inserting a node at the beginning of linked list
- #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.
- ```//Inserting a node at the beginning of linked list
- #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();
- }
- }
- ```
#3: Post edited
- 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?
- ```//Inserting a node at the beginning of linked list
- #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();
- }
- }
- ```
- 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?
- ```//Inserting a node at the beginning of linked list
- #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:
- 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.
- ```//Inserting a node at the beginning of linked list
- #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();
- }
- }
- ```
#2: Post edited
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 I don't understand why is there such an error. On my compiler, the error it gave me was "In function Insert: Node undeclared".- ```//Inserting a node at the beginning of linked list
- #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).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();
- }
- }
- ```
- 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?
- ```//Inserting a node at the beginning of linked list
- #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();
- }
- }
- ```
#1: Initial revision
Question regarding an error message in my compiler to do with my code on linked list.
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 I don't understand why is there such an error. On my compiler, the error it gave me was "In function Insert: Node undeclared". ```//Inserting a node at the beginning of linked list #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).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(); } } ```