0
How do I write and print a linked list?
3 Antworten
0
Do you have knowledge of structures?
0
Yes, but linked lists are confusing
0
You have to create a data-type (let node) of struct type
struct node
{
int info;
int *next;
};
/*
info where you you'll store values
*next is a pointer where you will add address of next node,that's why it's of pointer type
*/
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
/*This will create a pointer of struct type or men allocation*/
then simply
give message to insert value of info
then
cin>>node.info;
/* . is member access operator */
give message for next address
Set p to first position
cin>>node.next;
/*But for this you will be needed to have a knowledge of addresses of node so that you can have assigned it to next pointer
*/
and when you will enter num in node.next.Your linked list will be terminated
/*then simply take a loop FOR PRINTING*/
while(p.next!=NULL)
{
printf("%d ",node.info);
p=p->next;
}
/*It will print all values except last element
because at last node there will be null in next
so you have to print last element outside the loop*/
printf("%d",p->info);
return 0
}.