首页 > 代码库 > 链表的创建,遍历,清除
链表的创建,遍历,清除
node *creatline(int n)
{
node *head=(node *)malloc(sizeof(node));
head->data=http://www.mamicode.com/rand()%100;
head->next=NULL;
node *p=head;
int i=0;
while (i<n-1) {
p->next=(node *)malloc(sizeof(node));
p->next->data=http://www.mamicode.com/rand()%100;
p->next->next=NULL;
p=p->next;
i++;
}
return head;
}
void printline(node *p)
{
while (p!=NULL) {
printf("%d\n",p->data);
p=p->next;
}
}
void releaseline(node *p)
{
node *q=p->next;
while (p!=NULL)
{
printf("%d\n",p->data);
free(p);
p=q;
if (q!=NULL)
{
q=p->next;
}
}
}
void releaseline2(node *p)
{
node *q=p->next;
printf("%d\n",p->data);
free(p);
if (q!=NULL) {
releaseline2(q);
}
}
链表的创建,遍历,清除