this is my code. i wanted to print all my list datas. but i cant cause when i write while(llist->next != NULL) llist->next is NULL but i don’t know why. please help me 🙂
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct rame
{
int data;
struct rame *next;
};
int main()
{
struct rame *llist;
llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;
llist->next = llist;
llist->next->data = 15;
llist->next->next->data = 20;
llist->next->next->next->data = 25;
llist->next->next->next->next = NULL;
printf("test\n");
if(llist->next == NULL)
printf("%d\n",llist->data);
else
while(llist->next != NULL)
{
printf("%d\n",llist->data);
llist = llist->next;
}
system("pause");
return 0;
}
hey i did everithing but my LOOP doesnt print the last data. help me 🙁
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct rame
{
int data;
struct rame *next;
};
int main()
{
struct rame *llist;
llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;
llist->next = (rame*)malloc(sizeof(struct rame));
llist->next->data = 15;
llist->next->next = (rame*)malloc(sizeof(struct rame));
llist->next->next->data = 20;
llist->next->next->next = (rame*)malloc(sizeof(struct rame));
llist->next->next->next->data = 25;
llist->next->next->next->next = (rame*)malloc(sizeof(struct rame));
llist->next->next->next->next = NULL;
printf("test\n");
while(llist->next != NULL)
{
printf("%d\n",llist->data);
llist = llist->next;
}
system("pause");
return 0;
}
In your code
allocates one memory location to
llist, and the data of this location is assigned10Next you do:
The first line assigns the
nextlink of thellistto itself, which makes the below state of the listNow executing
llist->nextpoints tollistitself and thusllist->next->datasimply addresses to thelist->dataso the value 10 is changed.In the other linking you have done, how many number of times
->next->next->....->nextyou use does not matter as it will point to the same location.To test the thing print the address of
llistand the address ofllist->next. You have address ofllistandllist->nextidentical. This meansllist->data, andllist->next->dataare the same. and any number of indirection throughnextfield is the same. So after the final assignment thellist->datais 25, other values previously assigned is overwritten by this.At the last step you do:
llist->next->next->next->next = NULL;This actually makes the above diagram to:
This results in
if(llist->next == NULL)condition to be true and thus only the first node’s contents in printed, which is the last value you have inserted =25To get the proper effect, you need to allocate a new node for each next link, like for example in the context of your code:
In this case the diagram becomes
Now you can do linking in a chain fashion by doing
llist->next->next = (rame*)malloc(sizeof(struct rame));andllist->next-next->data = 5486etc.Recommended is instead of writing a chain of
nexts you can store the address of the last node temporarily in a temporary variable saytempand access the data element through them, like:Although actually you should link these with a loop with something like the following structure
You need to store the list head pointer in some variable, so that with that you can traverse the entire list by following the links. Note that if you loose that head pointer, then you will not be able to get the list.