#pragma once
#include<assert.h>
typedef int DataType;
typedef struct SListNode
{
DataType data;
struct SListNode * next;
}SListNode;
SListNode* _BuyNode(DataType x)
{
SListNode* tmp = (SListNode *)malloc(sizeof( SListNode));
tmp->data = x;
tmp->next = NULL;
return tmp;
}
void PushBack(SListNode * &pHead, DataType x )//引用
{
//1.没有节点
//有结点
if (pHead == NULL)
{
pHead = _BuyNode(x );
}
else
{
SListNode* tail = pHead ;
while (tail->next != NULL )
{
tail = tail->next;
}
tail->next = _BuyNode( x);
}
}
void PrintSlist(SListNode * pHead)
{
SListNode* cur = pHead ;
while (cur)
{
printf( "%d->", cur->data);
cur=cur->next;
}
printf( "NULL");
}
void PopBack(SListNode *& pHead)
{
if (pHead ==NULL)
{
return;
}
else if (pHead->next == NULL)
{
free( pHead);
pHead = NULL ;
}
else
{
SListNode* tail = pHead ;
SListNode*prev = NULL ;
while (tail->next)
{
prev = tail;
tail = tail->next;
}
free(tail);
prev->next = NULL;
}
}
void PushFront(SListNode * &pHead, DataType x )
{
if (pHead = NULL)
{
pHead = _BuyNode(x );
}
else
{
SListNode* tmp = _BuyNode(x );
tmp->next = pHead;
pHead = tmp;
}
}
void PopFront(SListNode *& pHead)
{
if (pHead = NULL)
{
return;
}
else if (pHead->next == NULL)
{
free( pHead);
pHead = NULL ;
}
else
{
SListNode*del = pHead ;
pHead = pHead ->next;
free(del);
}
}
void Invert(SListNode * pos, DataType x )
{
assert(pos );
SListNode* tmp = _BuyNode(x );
tmp->next = pos->next;
pos->next = tmp;
}
SListNode* Find(SListNode * pHead, DataType x )
{
SListNode*cur = pHead ;
while (cur)
{
if (cur->data == x )
{
return cur;
}
cur = cur->next;
}
return NULL ;
}
void PrintFromTailToHead(SListNode *pHead)
{
if (pHead )
{
PrintFromTailToHead( pHead->next);
printf( "%d", pHead ->data);
}
}
void DelNonTailNode(SListNode * pos)
{
assert(pos );
assert(pos ->next);
SListNode*del = pos ->next;
SListNode*next = del->next;
pos->data = del->data;
free(del);
}
void Insert