#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<assert.h> #include<stdlib.h> typedef int DataType; typedef struct SListNode { DataType data; struct SListNode* next; }SListNode; SListNode* BuyNode( DataType x) { SListNode* next = (SListNode*)malloc(sizeof(SListNode)); next->data = x; next->next = NULL; return next; } void PushBack(SListNode* & ppHead, DataType x) { if (ppHead == NULL) { ppHead = BuyNode(x); } else { SListNode* tail = ppHead; while (tail->next != NULL) { tail = tail->next; } tail->next = BuyNode(x); } } void PrintSNodeList(SListNode* ppHead) { while (ppHead) { printf("%d->",ppHead->data); ppHead = ppHead->next; } printf("\n"); } void TailToHeadPrint(SListNode* ppHead) { if (ppHead == NULL) { return; } else { TailToHeadPrint(ppHead->next); printf("%d->", ppHead->data); } } void Test1() { SListNode* List = NULL; PushBack(List, 1); PushBack(List, 2); PushBack(List, 3); PushBack(List, 4); PushBack(List, 5); PrintSNodeList(List); TailToHeadPrint(List); } int main() { Test1(); system("pause"); return 0; }
原文:http://10740184.blog.51cto.com/10730184/1734401