单项链表的基本实现

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>




typedef int DataType;


typedef struct SListNode
{
struct SListNode* _next;
DataType _data;
}SListNode;




SListNode* BuySListNode(DataType x)
{
SListNode *NewNode;
NewNode = (SListNode*)malloc(sizeof(SListNode));
if (NewNode == NULL)
{
printf("空间分配失败\n");
}
else
{

NewNode->_data = x;
NewNode->_next = NULL;
}
return NewNode;


}
void SListPrint(SListNode* pHead)
{
assert(pHead);
SListNode*cur = pHead;
while (cur)
{
printf("%d", cur->_data);
cur = cur->_next;
}
}
void SListDestory(SListNode** ppHead)
{
assert(*ppHead);
SListNode *cur;
cur = *ppHead;
while (*ppHead)
{
cur = *ppHead;

*ppHead = (*ppHead)->_next;
free(cur);

}

}


void SListPushBack(SListNode** ppHead, DataType x)
{

SListNode *cur=NULL;
SListNode *NewNode;
cur = *ppHead;
NewNode = BuySListNode(x);
if (*ppHead == NULL)
{
*ppHead = NewNode;
(*ppHead)->_next = NULL;

}
else
{
while (cur->_next)
{
cur = cur->_next;
}
cur->_next = NewNode;
NewNode->_next = NULL;
}
/*SListNode* newNode;
SListNode* tmp;
newNode = BuySListNode(x);
tmp = *ppHead;
if (NULL == *ppHead)
{
*ppHead = newNode;
(*ppHead)->_next = NULL;
}
else
{
while (tmp->_next)
{
tmp = tmp->_next;
}
tmp->_next = newNode;
newNode->_next = NULL;
}*/
}
void SListPopBack(SListNode** ppHead)
{
assert(*ppHead);
SListNode *cur;
SListNode *cur2;
cur = *ppHead;
if (cur->_next == NULL)
{
free(*ppHead);
}
else
{


while (cur->_next->_next)
{
cur = cur->_next;
}
cur2 = cur->_next;
free(cur2);
cur->_next = NULL;
}


}
void SListPushFront(SListNode** ppHead, DataType x)
{

SListNode *cur;
SListNode *NewNode;
NewNode = BuySListNode(x);
if (*ppHead ==NULL)
{
*ppHead = NewNode;
(*ppHead)->_next = NULL;
}
else
{
cur = (*ppHead)->_next;
(*ppHead)->_next =NewNode;
NewNode->_next = cur;
}
}
void SListPopFront(SListNode** ppHead)
{
assert(*ppHead);
SListNode *cur;
if ((*ppHead)->_next == NULL)
{
free(*ppHead);
}
else
{
cur = (*ppHead)->_next;
free(*ppHead);
*ppHead = cur;


}
}
SListNode* SListFind(SListNode* pHead, DataType x)
{
assert(pHead);
SListNode *pos;
pos = pHead;
while (pos)
{
if (pos->_data == x)
{
return pos;
}
pos = pos->_next;
}
return NULL;
}
void SListInsest(SListNode** ppHead, SListNode* pos, DataType x)
{
assert(*ppHead);
SListNode *cur;
SListNode *NewNode;

if (pos == *ppHead)
{
SListPushFront(ppHead, x);
return;
}
else
{
NewNode = BuySListNode(x);
cur = *ppHead;
while ((cur != pos)&&(cur!=NULL))
{
cur = cur->_next;
}
if (cur == NULL)
{
printf("没有找到对应位置\n");
return;
}
pos = cur->_next;
cur->_next = NewNode;
NewNode->_next = pos; 


}
return;
}
void SListErase(SListNode** ppHead, SListNode* pos)
{
assert(*ppHead);
SListNode *cur=*ppHead;
SListNode *cur2;
if (pos == *ppHead)
{
SListPopFront( ppHead);
return;
}
else
{
while ((cur->_next != pos) && (cur!=NULL))
{
cur = cur->_next;
}
cur2 = cur->_next;
cur->_next = cur2->_next;
free(cur2);
cur2 = NULL;
}
}
int main()
{
SListNode* SList;
SList = BuySListNode(1);
SListPushBack(&SList, 2);
SListPushBack(&SList, 3);
SListPushBack(&SList, 4);
SListPushBack(&SList, 5);
SListPrint(SList);
SListPopBack(&SList);
SListPushFront(&SList, 8);
SListPrint(SList);
SListInsest(&SList, SListFind(SList, 2), 9);
SListPopFront(&SList);
SListPrint(SList);
SListErase(&SList, SListFind(SList, 2));
SListPrint(SList);
SListDestory(&SList);
SListPrint(SList);
system("pause");
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值