输入一个链表的头结点,从尾到头反过来输出每个结点的值。
思想:先将元素进栈,再遍历栈内元素
int reversePrintList(LNode * pHead) {
if (!pHead) {
return 0;
}
StackLink *p = (StackLink*) malloc(sizeof(StackLink));
initStack(p);
while (pHead != NULL) {
push(p, pHead->data);
pHead = pHead->next;
}
ElemType elem;
while (getStackLength(p)) {
pop(p, &elem);
printf("%2d", elem);
}
printf("\n");
return 1;
}