Linked list for n elements where n is user defined

/The first line of input contains n elements , the number of elements in the linked list.
The next lines contain one element each, which are the elements of the linked list.
/

#include
using namespace std;
struct node{
int data;
struct node*next;
};
int n;
int i;

struct nodetemp,head=NULL;
struct node
newnode=(struct node
)malloc(sizeof(struct node));

void printLinkedList(int newdata) {
cin>>newnode->data;
newnode->data=newdata;
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
}

void display()
{
temp=head;
while(temp!=0){
cout<data;
temp=temp->next;
}
}
int main()
{
cin>>n;
for(i=0;i<=n;i++)
{

 printLinkedList(newnode);

}
display();
}

Hey @supratik260699
Found the following issues in your code:

  1. 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));

  2. 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 :slight_smile:

int main()
{
cin>>n;

for(i=0;i<n;i++)
{
int x; cin >> x;
printLinkedList(x);
}
display();
}

  1. 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();
}

1 Like

Thank you so much maam thanks for helping

1 Like

Still the code has following errors:

Compiling failed with exitcode 1, compiler output:
prog.cpp: In function ‘void printLinkedList(int)’:
prog.cpp:13:19: error: expected unqualified-id before ‘=’ token
struct nodenewnode=(struct node)malloc(sizeof(struct node) );
^
prog.cpp:14:1: error: ‘newnode’ was not declared in this scope
newnode->data=newdata;
^~~~~~~
prog.cpp:14:1: note: suggested alternative: ‘node’
newnode->data=newdata;
^~~~~~~
node
prog.cpp: In function ‘void display()’:
prog.cpp:32:6: error: ‘data’ was not declared in this scope
cout<data;
^~~~
prog.cpp:32:6: note: suggested alternative: ‘atan’
cout<data;
^~~~
atan

@supratik260699 write this line as

struct node (put asterisk)newnode=(struct node(put asterisk) )malloc(sizeof(struct node) );

and cout << temp->data (not cout < data)
make these changes in your code please.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.