Error in a simple program of entering elements at beginning of a linkedlist and displaying it in c programming
This is my code for program to enter elements in beginning of a linked list and print the linked list which is working perfectly fine(i am getting correct output for it) -
#include <stdio.h>
#include <stdlib.h>
// this is insertion of a node in begining of a linked list
typedef struct Node
{
int data;
struct Node *next;
} n;
void Insert(struct Node** head,int x)
{
struct Node *temp = (n *)malloc(sizeof(struct Node));
temp->data = x;
temp->next = *head;
*head = temp;
}
void Print(struct Node* head)
{
struct Node *temp = head;
printf("The linked list is:");
while (temp != NULL)
{
printf("%d,", temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
struct Node *head;
head = NULL;
int n, i, x;
printf("Enter how many numbers you want?\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter the number in the linkedlist\n");
scanf("%d", &x);
Insert(&head,x);
Print(head);
}
return 0;
}
for context the output is
Enter how many numbers you want?
6
Enter the number in the linkedlist
1
The linked list is:1,
Enter the number in the linkedlist
2
The linked list is:2,1,
Enter the number in the linkedlist
3
The linked list is:3,2,1,
Enter the number in the linkedlist
4
The linked list is:4,3,2,1,
Enter the number in the linkedlist
5
The linked list is:5,4,3,2,1,
Enter the number in the linkedlist
6
The linked list is:6,5,4,3,2,1,
now just to see if my concepts of pointers and pointer to structure is clear i modified the Print() function in code as while rest of he program remain same
#include <stdio.h>
#include <stdlib.h>
// this is insertion of a node in begining of a linked list
typedef struct Node
{
int data;
struct Node *next;
} n;
void Insert(struct Node** head,int x)
{
struct Node *temp = (n *)malloc(sizeof(struct Node));
temp->data = x;
temp->next = *head;
*head = temp;
}
void Print(struct Node* head)
{
struct Node *temp = head;
printf("The linked list is:");
// while (temp != NULL)
// {
// printf("%d,", temp->data);
// temp = temp->next;
// }
// printf("\n");
do{
printf("%d,", temp->data);
temp = temp->next;
}while((temp->next)!= NULL);
printf("\n");
}
int main()
{
struct Node *head;
head = NULL;
int n, i, x;
printf("Enter how many numbers you want?\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter the number in the linkedlist\n");
scanf("%d", &x);
Insert(&head,x);
Print(head);
}
Print(head);
return 0;
}
Now while i did the dry run where i am getting correct result ,the output i am getting is wrong,for context the output coming is
Enter how many numbers you want?
5
Enter the number in the linkedlist
1
The linked list is:1,
--------------------------------
Process exited after 5.156 seconds with return value 3221225477
Press any key to continue . . .
while i only made changes in Print() function why is'nt the compiler accepting the rest of the elements in linkedlist as input ? assuming there is some mistake in Print() function
Comments
Post a Comment