The output of this code is not something that I desired.
CODE |
---|
include
include
struct node
{
int data;
struct node *link;
};
struct node add_at_empty(struct node *head, int data)
{
struct node *temp;
temp= (struct node)malloc(sizeof(struct node));
temp->data=data;
temp->link=NULL;
head=temp;
return head;
}
struct node add_at_end(struct node *head, int data)
{
struct node *temp1;
temp1= (struct node)malloc(sizeof(struct node));
struct node *ptr=head;
temp1->data=data;
temp1->link=NULL;
if(ptr=head)
{
head->link=temp1;
ptr=ptr->link;
}
else
{
ptr->link=temp1;
ptr=temp1;
}
return head;
}
struct node *print_data(struct node *head)
{
struct node *traverse;
traverse=head;
while(traverse!=NULL)
{
std::cout<data<<" ";
traverse=traverse->link;
}
return head;
}
int main()
{
struct node head=NULL;
struct node *curr=NULL;
int nodes;
head=(struct node)malloc(sizeof(struct node));
curr=(struct node*)malloc(sizeof(struct node));
std::cout<<"enter Number of nodes required"<
std::cin>>nodes;
std::cout<<"Enter the data of first node "<
std::cin>>head->data;
head=add_at_empty(head, head->data);
nodes=nodes-1;
for(int i=0;i<nodes;i++)
{
std::cout<<"Enter data of next node "<<std::endl;
std::cin>>curr->data;
add_at_end(head, curr->data);
}
head=print_data(head);
return 0;
}
Top comments (0)