Hey @supratik260699
Found the following issues in your code:
-
struct nodenewnode=(struct node)malloc(sizeof(struct node));
is wrong and the correct way to write this is:
struct nodenewnode=(struct node*)malloc(sizeof(struct node));
-
void printLinkedList(int newdata)
this function takes an integer type argument and you are passing a struct node type argument to this function in your main:
printLinkedList(newnode);
this is wrong,
you need to pass an integer argument to the function. So you basically need to take the n input numbers in the main itself and pass it to the function, because you can’t pass an undefined integer to a function. so you should not take input in the function, take it in the main and pass it to the function, also you need to take n numbers as input, so your loop needs to run n times, i.e from 0 to less than n(i, e. to n-1), so your main function corrected is 
int main()
{
cin>>n;
for(i=0;i<n;i++)
{
int x; cin >> x;
printLinkedList(x);
}
display();
}
- Your printLinkedList function has errors too.
The logic should go like:
1. when the node is being inserted, if head is NULL at that time, you need to create that node as head,
2. if the head is not NULL while you create your newnode, you need to put that new node at the end of linked list, so you iterate till the end of the linked list and attach the newnode at the end.
The corrected function code is:
void printLinkedList(int newdata) {
struct nodenewnode=(struct node)malloc(sizeof(struct node) );
newnode->data=newdata;
newnode->next = NULL;
if(head==NULL) {
head = newnode;
// temp = head;
}
else{
temp = head;
while(temp->next!=NULL){
temp = temp->next;
}
temp->next = newnode;
}
// else temp->next=newnode;//the value of temp next will contain the value of newnode address
// temp=newnode;//thus now temp will move to next linked list
}
Your corrected code which runs well is:
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
struct node*next;
};
int n;
int i;
struct node*temp=NULL,*head=NULL;
void printLinkedList(int newdata) {
struct nodenewnode=(struct node)malloc(sizeof(struct node) );
newnode->data=newdata;
newnode->next = NULL;
if(head==NULL) {
head = newnode;
}
else{
temp = head;
while(temp->next!=NULL){
temp = temp->next;
}
temp->next = newnode;
}
}
void display()
{
temp=head;
while(temp!=NULL){
cout<data;
temp=temp->next;
}
}
int main()
{
cin>>n;
for(i=0;i<n;i++)
{
int x; cin >> x;
printLinkedList(x);
}
display();
}