单链表相比于顺序表来说,优点在于头部,中间插入比较高效,但操作较为复杂,空间利用率低,并且CPU顺序表比链表缓存效率高。下面来看代码实现:
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
typedef int DataType;
typedef struct Node
{
DataType data;
struct Node* next;
}*PNode,Node;
//初始化单链表
void InitList(PNode* pHead)
{
*pHead = NULL;
}
//创建一个节点
static PNode NewNode(DataType data)
{
PNode newNode = (PNode)malloc(sizeof(Node));
if (newNode)
{
newNode->data = data;
newNode->next = NULL;
}
return newNode;
}
void PushBack(PNode* pHead, DataType data)
{
PNode pcur = NULL;
assert(pHead);
if (*pHead == NULL)
{
*pHead = NewNode(data);
}
else
{
pcur = *pHead;
while (pcur->next)
{
pcur = pcur->next;
}
pcur->next = NewNode(data);
}
}
void PopBack(PNode* pHead)
{
PNode pcur = *pHead;
PNode pdel = NULL;
assert(pHead);
while (pcur->next->next)
{
pcur = pcur->next;
}
pdel = pcur->next;
pcur->next = NULL;
free(pdel);
}
void PushFront(PNode* pHead, DataType data)
{
PNode pcur = *pHead;
assert(pHead);
*pHead = NewNode(data);
(*pHead)->next = pcur;
}
void PopFront(PNode* pHead)
{
PNode pcur = *pHead;
assert(pHead);
if (*pHead == NULL)
{
return;
}
*pHead = pcur->next;
free(pcur);
}
PNode Find(PNode pHead, DataType data)
{
PNode pcur = pHead;
assert(pHead);
while (pcur)
{
if (pcur->data == data)
{
return pcur;
}
pcur = pcur->next;
}
return NULL;
}
void Insert(PNode pos, DataType data)
{
PNode pnew = NULL;
PNode pcur = pos->next;
if (pos == NULL)
{
return;
}
pnew = NewNode(data);
pos->next = pnew;
pnew->next = pcur;
}
void Erase(PNode* pHead, PNode pos)
{
PNode pcur = *pHead;
assert(pHead);
if (pos == NULL)
{
return;
}
while (pcur->next != pos)
{
pcur = pcur->next;
}
pcur->next = pos->next;
free(pos);
}
void Remove(PNode* pHead, DataType data)
{
PNode pos = NULL;
assert(pHead);
if (*pHead == NULL)
{
return;
}
else if ((pos = Find(*pHead, data)) == NULL)
{
return;
}
else
{
Erase(pHead, pos);
}
}
void RemoveAll(PNode* pHead, DataType data)
{
PNode ppre = NULL;
PNode pcur = NULL;
PNode pdel = NULL;
assert(pHead);
if (*pHead == NULL)
{
return;
}
else
{
ppre = *pHead;
pcur = ppre->next;
while (pcur)
{
if (pcur->data == data)
{
ppre->next = pcur->next;
free(pcur);
pcur = ppre->next;
}
else
{
ppre = pcur;
pcur = pcur->next;
}
}
if ((*pHead)->data == data)
{
pdel = *pHead;
*pHead = pdel->next;
free(pdel);
}
}
}
size_t Size(PNode pHead)
{
PNode pcur = pHead;
size_t count = 0;
while (pcur)
{
pcur = pcur->next;
count++;
}
return count;
}
PNode Front(PNode pHead)
{
if (pHead == NULL)
{
return NULL;
}
else
{
return pHead;
}
}
PNode Back(PNode pHead)
{
if (pHead == NULL)
{
return NULL;
}
else
{
PNode pcur = pHead;
while (pcur->next)
{
pcur = pcur->next;
}
return pcur;
}
}
int Empty(PNode pHead)
{
if (pHead == NULL)
{
return 1;
}
else
{
return 0;
}
}
void PrintList(PNode pHead)
{
PNode pcur = pHead;
assert(pHead);
while (pcur)
{
printf("%d->", pcur->data);
pcur = pcur->next;
}
printf("NULL\n");
}
void PrintListFromTail2Head(PNode pHead)
{
if (pHead)
{
PNode pcur = pHead;
pHead = pHead->next;
PrintListFromTail2Head(pHead);
printf("<-%d", pcur->data);
}
else
{
printf("NULL");
}
}
void DeleteNotTailNode(PNode pos)
{
PNode pcur = NULL;
if (pos == NULL)
{
return;
}
pos->data = pos->next->data;
pcur = pos->next;
pos->next = pcur->next;
free(pcur);
}
void InsertNotHeadNode(PNode pos, DataType data)
{
PNode pnew = NULL;
if (pos == NULL)
{
return;
}
pnew = NewNode(data);
pnew->next = pos->next;
pos->next = pnew;
(pos->data) ^= (pnew->data);
(pnew->data) ^= (pos->data);
(pos->data) ^= (pnew->data);
}
int main()
{
return 0;
}