#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 PushFront(SListNode* & ppHead,DataType x) { SListNode* cur = BuyNode(x); cur->next = ppHead; ppHead = cur; } void PrintSNodeList(SListNode* ppHead) { while (ppHead) { printf("%d->", ppHead->data); ppHead = ppHead->next; } printf("\n"); } void Test3() { SListNode* List = NULL; PushFront(List, 1); PushFront(List, 2); PushFront(List, 3); PushFront(List, 4); PushFront(List, 5); PrintSNodeList(List); } int main() { Test3(); system("pause"); return 0; }
原文地址:http://10740184.blog.51cto.com/10730184/1734404