How to insert node into linked list

#include<iostream>
using namespace std;

class node
{
int data;
node *next;
public:
node * create(node*head);
void display(node* head);
void insert(node *head);
};



node* node::create(node *head)
{
head=new node;
head->next=NULL;
return head;
}

void node::display(node *head)
{
node *curr;
curr=head->next;
while(curr!=NULL)
{
cout<<curr->data<<"->";
curr=curr->next;
}

}

void node::insert(node*head)
{
node *temp;
temp=new node;
cout<<"\nEnter data\n";
cin>>temp->data;
temp->next=NULL;
node *curr;
curr=head;
while(curr->next!=NULL)
{
curr=curr->next;
}
curr->next=temp;

}

int main()
{
node obj;
node *head;
head=obj.create(head);
obj.insert(head);
obj.insert(head);
obj.insert(head);
obj.insert(head);
obj.display(head);
}


/*********Output*************
Enter data
12

Enter data
32

Enter data
54

Enter data
65
12->32->54->65->
***************************/

Post a Comment

0 Comments