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;
l开发者_运维百科list->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;
}
llist->next = llist;
llist
's next element is llist
itself. You don't have a linked list per se, just a single element that loops back to itself. So:
llist->next->data = 15;
llist->next->next->data = 20;
llist->next->next->next->data = 25;
All these modify llist->data
. And:
llist->next->next->next->next = NULL;
Sets llist->next
to NULL
.
You'll need to create new list elements (with malloc
) and link them if you want to build a list. For example:
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 = 15;
....
Your loop is not correct: you will always skip the last entry as it's ->next
will be null, and so the loop body will not run.
Try with:
struct rame *cursor = llist;
while (cursor != NULL) {
printf("%d\n", cursor->data);
cursor = cursor->next;
}
You use a second pointer to the list so that llist
is kept unchanged and pointing at the list header. (If you don't do that, you'll never be able to get back to it since it's singly linked.)
In your code
llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;
allocates one memory location to llist
, and the data of this location is assigned 10
Next you do:
llist->next = llist;
llist->next->data = 15;
The first line assigns the next
link of the llist
to itself, which makes the below state of the list
+--------+-----+------+
| llist | 10 | next |-----+
+--------+-----+------+ |
^ |
| |
+-----------------------v
Now executing llist->next
points to llist
itself and thus llist->next->data
simply addresses to the list->data
so the value 10 is changed.
In the other linking you have done, how many number of times ->next->next->....->next
you use does not matter as it will point to the same location.
To test the thing print the address of llist
and the address of llist->next
. You have address of llist
and llist->next
identical. This means llist->data
, and llist->next->data
are the same. and any number of indirection through next
field is the same. So after the final assignment the llist->data
is 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:
+--------+-----+------+
| llist | 10 | next |----->NULL
+--------+-----+------+
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 = 25
To get the proper effect, you need to allocate a new node for each next link, like for example in the context of your code:
llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;
llist->next = (rame*)malloc(sizeof(struct rame)); // we allocate a new location which
// we point to with the initial llist
llist->next->data = 15; // this will now set the data to 15 of
// the node which we allocated on
// the previous step
In this case the diagram becomes
+--------+-----+------+ +-----------------+----+------+
| llist | 10 | next |----->| newly allocated | 15 | next |
+--------+-----+------+ +-----------------+----+------+
Now you can do linking in a chain fashion by doing llist->next->next = (rame*)malloc(sizeof(struct rame));
and llist->next-next->data = 5486
etc.
Recommended is instead of writing a chain of next
s you can store the address of the last node temporarily in a temporary variable say temp
and access the data element through them, like:
llist = (rame*)malloc(sizeof(struct rame));
temp = llist;
temp->data = 5
temp->next = (rame*)malloc(sizeof(struct rame));
temp = temp->next; //now temp contains the address of the newly allocated node above
temp->data = 10;
temp->next = (rame*)malloc(sizeof(struct rame));
temp = temp->next;
temp->data = 15;
.
.
Although actually you should link these with a loop with something like the following structure
list_head = (rame*)malloc(sizeof(struct rame));
temp = list->head;
while (some condition)
{
temp->next = (rame*)malloc(sizeof(struct rame));
temp = temp->next;
//if this is the last node,we assign null to identify this that there is no more nodes after this.
temp->next = NULL;
temp->data = value;
}
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.
llist->next->next->next->next = NULL;
You set the Pointer to NULL.
Draw yourself a sketch containing all allocated memory / struct(s) and the pointers. Then you will see that the snake bites in its tail at first and then its next
is assigned NULL.
llist->next->next->next->next = NULL;
is making llist->next = NULL
because of llist->next = llist;
You need to allocated memory for each node.for a list of two nodes:
llist = (rame*)malloc(sizeof(struct rame));
llist->data = 10;
struct rame *llist2;
llist2 = (rame*)malloc(sizeof(struct rame));
llist2->data =15;
llist2->next = NULL;
llist->next = llist2;
精彩评论