Linked List.I am unable to display the nodes in the list

If i give 1 as size and 1 elemnt it prints "1->"But when I try to give size as 5 and element as 1 2 3 4 5 the compiler doesnt print anything…Kindly help me with this.I am new to linked list concept

using namespace std;
void display();
struct node
{
int data;
struct node* link;
};
struct node* root;
int main()
{
int n;
cin>>n;
struct node* temp;
for(int i=0;i<n;i++)
 {
if(root==NULL)
	{		
	temp=(struct node *)malloc(sizeof(struct node));
	cin>>temp->data;
	temp->link=NULL;
	root=temp;
}
else
{
	struct node* p;
		 temp=(struct node *)malloc(sizeof(struct node));
			
			cin>>temp->data;
			temp->link=NULL;
			root=p;
			while(p->link=NULL)
			{
				p=p->link;
				p->link=temp;}}}
				display();
				return 0 ;}
				
			void display()
			{
			
				struct node* temp=root;
				while(temp!=NULL)
	{
			cout<<temp->data<<"->";	
		temp=temp->link;
}}

First of all please atleast paste code which is neatly indented and formatted. Its irritating to debug such a code. Here you go, a stack implementation.

This will give 5->4->3->2->1->

if you want 1->2->3->4->5-> then try out queue implementation yourself.

#include<bits/stdc++.h>
using namespace std;
void display();
struct node
{
int data;
struct node* link;
};
struct node* root;
int main()
{
    int n;
    cin>>n;
    struct node* temp;
    for(int i=0;i<n;i++)
    {
        if(root==NULL)
        {       
            temp=(struct node *)malloc(sizeof(struct node));
            cin>>temp->data;
            temp->link=NULL;
            root=temp;
        }
        else
        {
            struct node* p;
            temp=(struct node *)malloc(sizeof(struct node));
            cin>>temp->data;
            temp->link = root;
            root = temp;
        }
    }
    display();
    return 0 ;
    
}

void display()
{
    struct node* temp=root;
    while(temp!=NULL)
    {
        cout<<temp->data<<"->"; 
        temp=temp->link;
    }
}