list.h文件
#include<stdio.h> #include<stdlib.h> //定义单链表类型 typedef int DataType; typedef struct Node { struct Node* _pNext; DataType _data; }Node, *PNode; // 链表初始化 void SListInit(PNode* pHead); //申请一个结点 PNode BuySListNode(DataType data); // 尾插 void SListPushBack(PNode* pHead, DataType data); // 尾删 void SListPopBack(PNode* pHead); // 头插 void SListPushFront(PNode* pHead, DataType data); // 头删 void SListPopFront(PNode* pHead); // 查找值为data的结点,返回该结点在链表中的位置 PNode SListFind(PNode pHead, DataType data); // 在链表pos位置后插入结点data void SListInsert(PNode* pHead, PNode pos, DataType data); // 删除链表pos位置上的结点 void SListErase(PNode* pHead, PNode pos); // 销毁单链表 void SListDestroy(PNode* pHead); // 求链表中结点的个数 int SListSize(PNode pHead); // 将链表中的结点清空 void SListClear(PNode* pHead); // 获取链表中的最后一个结点,返回该结点的地址 PNode SListBack(PNode pHead); //打印链表 void PrintList(PNode* PHead);
list.c文件
#define _CRT_SECURE_NO_WARNINGS #include"list.h" #include<assert.h> void SListInit(PNode* pHead) //链表初始化 { assert(pHead); *pHead = NULL; } PNode BuySListNode(DataType data)//申请一个结点 { PNode PNewNode = (PNode *)malloc(sizeof(Node)); if (PNewNode == NULL) { return NULL; } PNewNode->_pNext = NULL; PNewNode->_data = data; return PNewNode; } void SListPushBack(PNode* pHead, DataType data)// 尾插 { assert(pHead); if (NULL == *pHead) { pHead = BuySListNode(data); } else { PNode pCur = NULL; pCur = *pHead; while (pCur->_pNext) { pCur = pCur->_pNext; } pCur->_pNext = BuySListNode(data); } } void SListPopBack(PNode* pHead) // 尾删 { assert(pHead); if (NULL == *pHead) //空链表 return NULL; else if (NULL == (*pHead)->_pNext) //只有一个节点 { PNode TmpNode = *pHead; free(TmpNode); TmpNode = NULL; *pHead = NULL; } else { PNode pCur = *pHead; while (pCur->_pNext->_pNext) { pCur = pCur->_pNext; } pCur->_pNext = NULL; } } void SListPushFront(PNode* pHead, DataType data)// 头插 { PNode pPre = NULL; PNode PNewNode = NULL; assert(pHead); if (NULL == *pHead) //空链表 return NULL; PNewNode = BuySListNode(data); pPre = *pHead; PNewNode->_pNext = pPre; *pHead = PNewNode; } void SListPopFront(PNode* pHead)// 头删 { PNode pDelNode = NULL; assert(pHead); if (NULL == *pHead) //空链表 return NULL; pDelNode = *pHead; *pHead = pDelNode->_pNext; free(pDelNode); } // 查找值为data的结点,返回该结点在链表中的位置 PNode SListFind(PNode pHead, DataType data) { PNode pCur = pHead; while (pCur) { if (data == pCur->_data) { return pCur; } pCur = pCur->_pNext; } return NULL; } // 在链表pos位置后插入结点data void SListInsert(PNode* pHead, PNode pos, DataType data) { PNode PNewNode = NULL; assert(pHead); if (NULL == *pHead || NULL == pos) return; PNewNode = BuySListNode(data); PNewNode->_pNext = pos->_pNext; pos->_pNext = PNewNode; } // 删除链表pos位置上的结点 void SListErase(PNode* pHead, PNode pos) { assert(pHead); if (NULL == *pHead || NULL == pos) return; if (pos == *pHead) { SListPopFront(pHead); } else { PNode pCur = *pHead; while (pCur && pCur->_pNext != pos) { pCur = pCur->_pNext; } if (pCur) { pCur->_pNext = pos->_pNext; free(pos); } } } void SListClear(PNode* pHead)// 将链表中的结点清空 { PNode pDelNode = NULL; assert(pHead); while (*pHead) { pDelNode = *pHead; *pHead = pDelNode->_pNext; free(pDelNode); } } void SListDestroy(PNode* pHead)// 销毁单链表 { SListClear(pHead); } int SListSize(PNode pHead) // 求链表中结点的个数 { int count = 0; PNode pCur = pHead; assert(pHead); if (NULL == pHead) return; pCur = pHead; while (pCur) { count++; pCur = pCur->_pNext; } return count; } // 获取链表中的最后一个结点,返回该结点的地址 PNode SListBack(PNode pHead) { PNode pCur = pHead; assert(pHead); while (pCur) { pCur = pCur->_pNext; } return pCur; } void PrintList(PNode* PHead)//打印单链表 { PNode pCur = *PHead; assert(PHead); while (pCur) { printf("%d->", pCur->_data); pCur = pCur->_pNext; } printf("NULL\n"); }
文章来源: 链表及其基本操作