学到链表,代码malloc的p不用free吗?请教!请教!
typedef struct _node{int value;
struct _node *next;
struct _node *prev; //previous
}Node;
int main(int argc, char const *argv[])
{
//input number
int number;
Node *head = NULL;
Node *rlast; //reallast
do
{
scanf("%d", &number);
if(number != -1)
{
Node *p = (Node*)malloc(sizeof(Node));
p->value = number;
p->next = NULL;
p->prev = NULL;
//find the last
Node *last = head;
if(last)
{
while(last->next)
{
last = last->next;
}
p->prev = last;
last->next = p;
}
else
head = p;
rlast = p;
printf("%p %p ", rlast, p); //发现rlast和p的地址一样,每次都重新申请p,之前的p空间占着不用了(没free)?
}
}while(number != -1);
//output number
Node *a;
for(a = rlast;a;a = a->prev)
{
printf("%d ", a->value);
}
return 0;
}
[此贴子已经被作者于2016-12-14 22:31编辑过]