For the following code, the result that I'm aiming for is 4-->5-->, however the result that is outputted is only 4-->
For context, I'm trying to implement a singly linked list using structure and functions only in c++.
Code:
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
};
node* head = NULL;
void insert(int val)
{
node* n = new node();
n->data = val;
if(head == NULL)
{
head = n;
}
else
{
node* temp = head;
while(temp!=NULL)
{
temp = temp->next;
}
temp = n;
}
}
void display()
{
if(head == NULL)
{
cout<<"UNDERFLOW ! LINKED LIST IS EMPTY !"<<endl;
}
else
{
cout<<"LINKED LIST!"<<endl;
node* temp = head;
while(temp!=NULL)
{
cout<<temp->data<<"-->";
temp = temp->next;
}
cout<<endl;
}
}
int main()
{
insert(4);
insert(5);
display();
return 0;
}
insert. What's the value oftempwhen you assignn?