<pre name="code" class="cpp">#include <stdio.h>
typedef struct _tNODE_
{
int pos;
_tNODE_ *next;
}tNODE;
tNODE *create_node_list(int num)
{
if (num < 1) return NULL;
tNODE *head = NULL, *q = NULL;
int i = 1;
while (num > 0)
{
tNODE *p = new tNODE;
p->pos = i++;
p->next = NULL;
if (head == NULL)
{
head = p;
q = head;
}
else
{
q->next = p;
q = q->next;
}
--num;
}
return head;
}
void show_node_list(tNODE *head)
{
tNODE *p = head;
while (p != NULL)
{
printf("%d--->", p->pos);
p = p->next;
}
printf("NULL\n");
return;
}
tNODE *list_reverse(tNODE *&head)
{
tNODE *p1 = head;
tNODE *p2 = p1->next;
tNODE *p3;
while (p2)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
head->next = NULL;
head = p1;
return head;
}
void destory_list(tNODE *p_head)
{
tNODE *p = p_head;
tNODE *q;
while (p != NULL)
{
q = p->next;
delete p;
p = q;
}
return;
}
int main(void)
{
const int num = 10;
tNODE *p_head = create_node_list(num);
show_node_list(p_head);
list_reverse(p_head);
show_node_list(p_head);
destory_list(p_head);
return 0;
}
单链表的反转问题测试
最新推荐文章于 2022-04-25 21:44:18 发布