When I run this code and input data into the linked list like this :
23 45 55
The output will be:
45 55
The first data is lost!!!!
What's the problem? How can I change the code to get the correct output?
#include<iostream>
using namespace std;
struct node
{
int data;
node* next;
};
int main()
{
char ch;
node* n;
node* t;
node* h;
n= new node();
t=n;
h=n;
cout<<"Linked list is created.Now You can write data into it."<<endl;
cout<<"Enter data into the linked list:";
cin>>t->data;
cout<<"do you want to enter more data into the linked list?(y/n):";
cin>>ch;
if( ch =='y'|| ch =='Y')
do
{
n=new node();
t->next=n;
cout<<"Enter data into the linked list:";
cin>>t->data;
t=t->next;
cout<<"do you want to enter more data into the linked list?(y/n):";
cin>>ch;
}while(ch=='Y'||ch=='y');
t->next=NULL;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<"================================="<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<"Do you want to print data on the linked list?(y/n):";
cin>>ch;
if(ch=='y'||ch=='Y')
{
t=h;
while(t->next != NULL)
{
cout<<t->data<<endl;
t=t->next;
}
}
}
t=t->next;beforecin>>t->data;. Then fix the printing loop accordingly. And use a debugger, please...deletestatements are missing.