算法描述1:
利用两根指针*p,*q,
让指针p表尾,
指针q指向表头
遍历让指针q指向指针p前面的节点,
之后输出指针p的数据域,链表长度减一(p=q)
再次让指针q指向表头,遍历到指针p的前一个元素,输出p的数据域,
重复上述动作,直到指针p指向表头,跳出循环,输出指针p的数据域。
void Reserve_Print_Link(ElemSN *h)
{
ElemSN *p = h, *q = NULL;
//先让指针p指向链表的尾部
while (p->next)
{
p = p->next;
}
while (p - h)
{
q = h;
while (q->next - p)//循环让指针q指向p的前一个元素
{
q = q->next;
}
printf("%5d",p->data);
p = q;//指针p向前移动
}
printf("%5d\n",p->data);//输出头结点数据域
}
算法描述2:
利用“栈”进行存储,之后打印链表
void Reserve_Print_Link(ElemSN *h)
{
ElemSN *p = NULL;
int stack[100], top = -1;
for (p = h; p; p = p->next)
{
stack[++top] = p->data;
}
while (top + 1)
{
printf("%5d", stack[top--]);
}
printf("\n");
}
算大描述3:
利用递归的方式打印链表
void Reserve_Print_Link(ElemSN *h)
{
if (h->next)
{
Reserve_Print_Link(h->next);
}
printf("%5d",h->data);
}