hello,after watching video,i made same code for inputting a link list but ,…my code is taking input only,it is not printing the linked list.
here the code is:
#include
using namespace std;
class node{
public:
int data;
node* next;
node(int d)
{
data =d;
next=NULL;
}
};
void insertathead(node*&head,int data)
{
node*n=new node(data);
n->next = head;
head=n;
}
void print(nodehead)
{
nodetemp=head;
while(temp!=NULL)
{
cout<data<<"-> ";
temp= temp->next;
}
}
void insertionattail(node*&head,int data)
{
if(head==NULL)
{
node*temp=new node(data);
return;
}
node*tail=head;
while(tail->next!=NULL)
{
tail=tail->next;
}
tail->next=new node(data);
return;
}
int length(node*head)
{
int len=0;
while(head!=NULL)
{
head=head->next;
len++;
}
return len;
}
void insertioninmiddle(node*&head,int data, int p)
{
if(head==NULL||p==0)
{
insertathead(head, data);
}
else if(p>length(head))
{
insertionattail(head,data);
}
else
{
//insertion in the middle
//take p-1 jumps
int jump=1;
nodetemp=head;
while(jump<=p-1)
{temp=temp->next;
jump++;
}
noden=new node(data);
n->next=temp->next;
temp->next=n;
}
}
void builtlist(node*&head)
{
int data;//taking first data
cin>>data;
while(data!=-1)
{
insertionattail(head,data);
cin>>data;
}
}
int main()
{
node*head = NULL;
builtlist(head);
print(head);
return 0;
}