#include<stdio.h>
#include<malloc.h>
#include<assert.h>
#define unsigned int size_t
typedef int DataType;
在单链表中查找值为data的结点,找到了返回该结点的地址,否则返回NULL
//Node* Find(PNode pHead, DataType data);
//
在单链表pos位置后插入值为data的结点
//void Insert(PNode pos, DataType data);
//
在单链表中删除位置为pos的结点
//void Erase(PNode* pHead, PNode pos);
//
移除单链表中第一个值为data的结点
//void Remove(PNode* pHead, DataType data);
//
移除单链表中所有值为data的结点
//void RemoveAll(PNode* pHead, DataType data);
// 获取单链表总结点的总个数
//size_t Size(PNode pHead);
// 判断结点是否为空
///int Empty(PNode pHead);
typedef struct Node
{
struct Node *_pNext;
DataType _data;
}Node,*pNode;
size_t Size(pNode pHead);
Node *BuyNode(DataType data);
void Insert(pNode *pos,DataType data);
void initList(pNode *pHead);
Node* Find(pNode pHead, DataType data);
void PushBack(pNode *pHead,DataType data);
void PopBack(pNode *pHead);
void PushFront(pNode *pHead,DataType data);
void PopFront(pNode *pHead);
void Print_List(pNode pHead);
void Remove(pNode *pHead,DataType data);
void RemoveAll(pNode* pHead, DataType data);
void Erase(pNode* pHead, pNode pos);
size_t Size(pNode pHead)
{
pNode cur=NULL;
size_t count=0;
assert(pHead);
cur=pHead;
if(NULL==pHead)
{
return 0;
}
else
{
while(cur!=NULL)
{
cur=cur->_pNext;
count++;
}
return count;
}
}
void Erase(pNode* pHead,pNode pos)
{
pNode cur=NULL;
pNode pre=NULL;
assert(pHead);
assert(pos);
cur=(*pHead)->_pNext;
pre=(*pHead);
if(NULL==*pHead)
{
return;
}
if(pos==(*pHead))
{
*pHead=(*pHead)->_pNext;
free(pos);
}
else
{
while(cur)
{
if(cur==pos)
{
pre->_pNext=cur->_pNext;
free(pos);
}else
{
pre=pre->_pNext;
cur=cur->_pNext;
}
}
}
}
void RemoveAll(pNode* pHead, DataType data)
{
pNode cur=NULL;
pNode pre=NULL;
assert(pHead);
pre=cur=*pHead;
cur=cur->_pNext;
if(NULL==pre)
{
return;
}
if((*pHead)->_data==data)
{
pNode pTemp=(*pHead);
(*pHead)=(*pHead)->_pNext;
free(pTemp);
}
while(NULL!=cur)
{
if(cur->_data==data)
{
pre->_pNext=cur->_pNext;
free(cur);
cur=pre->_pNext;
}else
{
pre=cur;
cur=cur->_pNext;
}
}
}
void Remove(pNode *pHead,DataType data)
{
pNode cur=NULL;
pNode pre=NULL;
assert(pHead);
cur=*pHead;
if(cur==NULL)
{
return;
}
if((*pHead)->_data==data)
{
cur=(*pHead);
(*pHead)=(*pHead)->_pNext;
free(cur);
}else
{
while(cur->_data!=data)
{
pre=cur;
cur=cur->_pNext;
}
pre->_pNext=cur->_pNext;
free(cur);
}
}
void Insert(pNode *pos,DataType data)
{
pNode newNode=BuyNode(data);
assert(pos);
newNode->_pNext=(*pos)->_pNext;
(*pos)->_pNext=newNode;
}
void initList(pNode *pHead)
{
assert(pHead);
*pHead=NULL;
}
Node* Find(pNode pHead, DataType data)
{
assert(pHead);
while((pHead->_data!=data)&&(pHead!=NULL))
{
pHead=pHead->_pNext;
}
if(pHead!=NULL)
{
return pHead;
}
printf("未找到");
return 0;
}
Node *BuyNode(DataType data)
{
pNode newNode=(pNode)malloc(sizeof(Node));
if(newNode==NULL)
{
exit(-1);
}
newNode->_data=data;
newNode->_pNext=NULL;
return newNode;
}
void PushBack(pNode *pHead,DataType data)
{
assert(pHead);
if(*pHead==NULL)
{
*pHead=BuyNode(data);
}else
{
pNode cur=*pHead;
while(cur->_pNext!=NULL)
{
cur=cur->_pNext;
}
cur->_pNext =BuyNode(data);
}
}
void PopBack(pNode *pHead)
{
pNode cur;
if(*pHead==NULL)
{
return;
}else if((*pHead)->_pNext==NULL)
{
free(*pHead);
*pHead=NULL;
}else
{
cur=*pHead;
while(cur->_pNext->_pNext!=NULL)
{
cur=cur->_pNext;
}
free(cur->_pNext);
cur->_pNext=NULL;
}
}
void PushFront(pNode *pHead,DataType data)
{
assert(pHead);
if(NULL==*pHead)
{
*pHead=BuyNode(data);
}else
{
pNode cur=*pHead;
pNode node=BuyNode(data);
node->_pNext=cur->_pNext;
cur->_pNext=node;
}
}
void PopFront(pNode *pHead)
{
pNode cur=NULL;
assert(pHead);
if(NULL==(*pHead))
{
return;
}
cur=*pHead;
(*pHead)=(*pHead)->_pNext;
free(cur);
}
void Print_List(pNode pHead)
{
pNode cur=pHead;
while(cur!=NULL)
{
printf("%d",cur->_data);
cur=cur->_pNext;
}
}
int main()
{
Node *pHead;
pNode n;
initList(&pHead);
PushBack(&pHead,5);
PushBack(&pHead,4);
PushBack(&pHead,5);
PushBack(&pHead,4);
PushBack(&pHead,5);
PushBack(&pHead,4);
PushBack(&pHead,5);
PushBack(&pHead,4);
n=Find(pHead,5);
Insert(&n,898);
//Erase(&pHead,n);
//RemoveAll(&pHead,5);
Print_List(pHead);
printf("\n%d", Size(pHead));
// printf("\n%p",Find(pHead,5));
system("pause");
}