not displaying output why?

0 votes
asked Aug 11, 2018 by Akshay Baser (120 points)
#include<iostream>
using namespace std;
struct node
{
    int data;
    struct node *next;
};
struct node *p=NULL;
int main()
{   
    struct node *head,*temp;
    int i,n;
    cout<<"Enter no. of nodes";
    cin>>n;
    head=p;
    head=(struct node*)malloc(sizeof(struct node));
    cout<<"\nEnter data of first node ";
    cin>>head->data;
    head->next=NULL;
    temp=head;
    for(i=0;i<n;i++)
    {
        temp=(struct node*)malloc(sizeof(struct node));
        cout<<"\nEnter data";
        cin>>temp->data;
        temp->next=NULL;
        temp=temp->next;
    }
    temp=head;
    while(temp->next!=NULL)
    {
        cout<<"\t"<<temp->data;
        temp=temp->next;
    }
    
    return 0;
}

1 Answer

0 votes
answered Aug 11, 2018 by virendra singh (140 points)
Head is not linked with the other created nodes...... and variable temp is getting modified many times.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...