不含头结点的单链表(自用)
#include<stdio.h>#include<stdlib.h>
#include<malloc.h>
#include<windows.h>
typedef int SLDataType;
typedef struct SListNode
{
struct SListNode*_pNext;
SLDataType _data;
}SListNode;
void SListInit(SListNode** pHead){ //初始化
assert(pHead);
*pHead = NULL;
}
SListNode* SListNewNode(SLDataType data){ //创建新节点
SListNode*_pNew = (SListNode*)malloc(SListNode));
if(_pNew == NULL){
exit(0);
}
else{
_pNew->_data = data;
_pNew->_pNext = NULL;
retuen _pNew;
}
}
voidSListPrint(SListNode* pHead){ //打印链表
SListNode* pCur = pHead;
for(pCur;pCur != NULL;pCur = pCur->_pNext
{
printf("%d--->",pCur->_data);
}
printf("NULL\n");
}
void SLlistPushBack(SListNode** pHead,SLDataType data){ //尾插
assert(pHead);
SListNode* NewNode = SListNewNOde(data);
if(*pHead == NULL)
{
*pHead = NewNode;
return;
}
SListNode* pTail = *pHead;
while(pTail->_pNext)
{
pTail = pTail->_pNext;
}
pTail->_pNext = NewNode;
}
void SListPopBack(SListNode** pHead){ //尾删
assert(pHead);
if(NULL == *pHead);
{
return;
}else if(NULL == (*pHead)->_pNext){
free(*pHead);
*pHead = NULL;
}
else {
SListNode* pCur = *pHead;
while(pCur->_pNext->_pNext){
pCur = pCur->_pNext;
}
free(pCur->_pNext->_pNext);
pCur-> _pNext = NULL;
}
}
void SListPushFront(SListNode** pHead,SLDataType data) //头插
{
assert(pHead);
SListNode* NewNode = SListNewNode(data);
NewNode->_pNext = (*pHead);
*pHead = NewNode;
}
void SListPopFront(SListNode** pHead) //头删
{
assert(pHead);
SListNode* pDel = *pHead;
if((*pHead) == NULL)
{
printf("链表为空!\n");
return;
}
eise{
*pHead = pDel->_pNext;
free(pDel);
}
}
SListNode* SListFind(SListNode** pHead,SLDataType data){ //查找
assert(pHead);
if(*pHead == NULL)
{
return NULL;
}
SListNode* pFind = *pHead;
while(pFind){
if(pFind->_data == data){
return pFind;
}
pFind = pFind->_pNext;
}return NULL;
}
void SListInsert(SListNode** pHead,SListNode* pos,SLDataType data) //任意位置插入
{
SListNode*_pNew = (SListNode*)SListNewNode(data);
if(NULL == pos){
return;
}
_pNew->_pNext = pos->_pNext;
pos->_pNext = _pNew;
}
void SListErase(SListNode** pHead,SListNode* pos){ //任意位置删除
assert(pHead);
assert(pos);
if (pos == NULL||*pHead == NULL){
return;
}
if(pHead == pos){
*pHead = pos->_pNext;
}
else{
SListNode * posPre = *pHead;
while(posPre->_pNext != pos){
posPre = posPre->_pNext;
}
posPre->_pNext = pos->_pNext;
free(pos);
}
}
void menu()
{
printf("***********欢迎使用电脑设备管理系统**************\n");
printf("***************请选择你的功能********************\n");
printf("1.初始化*****************************2.创建新节点\n");
printf("3.头插*****************************4.头删\n");
printf("5.尾插*****************************6.尾删\n");
printf("7.查找*****************************8.插入\n");
printf("9.删除 ******************************10.输出\n");
}
main()
{
}
页:
[1]