带头节点的单链表
.h
# pragma once
# include<stdio.h>
# include<stdlib.h>
# include<string.h>
# include<assert.h>
typedef int DataType;
typedef struct Node
{
struct Node* _pNext;
DataType _data;//自己的值域
}Node, *PNode;
PNode BuyNodeH(DataType data);
void SListInitH(PNode *pHead);
void SListHPushBack(PNode *pHead,DataType data);void SListHPushBack(PNode *pHead,DataType data);
void SListHPushFront(PNode pHead, DataType data);
void PopFront(PNode pHead);
void DestroyList(PNode *pHead);
.c
无论传值还是传地址都会生成临时变量
# include"lianbiao2.h"
PNode BuyNodeH(DataType data)
{
PNode pNewNode = (PNode)malloc(sizeof(Node));
if (NULL == pNewNode)
{
assert(pNewNode);
return NULL;
}
}
void SListInitH(PNode *pHead)
//要改变指针的指向需要使用二级指针
{
assert(pHead);//pHead存放外部指针的地址,一定不会为空
*pHead = BuyNodeH(0);
}
void SListHPushBack(PNode* pHead,DataType data)
{
PNode pCur = pHead;
while (pCur->_pNext)
{
pCur = pCur->_pNext;
}
pCur->_pNext = BuyNodeH(data);
}void SListHPopBack(PNode *pHead)
void SListHPushFront(PNode pHead, DataType data)
{
PNode pNewNode = BuyNodeH(data);
pNewNode->_pNext = pHead->_pNext;
pHead = pNewNode;
}
void PopFront(PNode pHead)
{
PNode pDelNode = NULL;
if (pDelNode)
{
pDelNode = pHead->_pNext;
pHead = pDelNode->_pNext;
free(pDelNode);
}
}
//1、计算长度若换成类型,则不方便
//2、清空不清空头
void DestroyList(PNode *pHead)
{
clear(*pHead);
free(*pHead);
*pHead = NULL;
}