#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;
}
C语言:【单链表】逆置反转单链表
最新推荐文章于 2023-11-06 21:48:46 发布