链表的节本操作

                            链表的基本操作

链表是一种链式存储结构,链式存储结构的特点是用一组任意的存储单元存储数据元素。为了能正确表示数据元素之间的线性关系,需引入结点概念。一个结点表示链表中的一个数据元素,节点中除了储存数据元素的信息, 还必须存放指向下一个节点的的指针(单、双链表的最后一个节点除外,它们存储的是一个空指针NULL)。以下是对链表的一些基本操作的代码实现 

 

//头文件

SList.h

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>
#include<assert.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);
PNode SListFind(PNode pHead, DataType data);
void SListInsert(PNode* pHead, PNode pos, DataType data);
void SListErase(PNode* pHead, PNode pos);
void SListDestroy(PNode* pHead);
int SListSize(PNode pHead);
void SListClear(PNode* pHead);
PNode SListBack(PNode pHead);

//源文件

SList.c

#include"SList.h"
// 链表初始化
void SListInit(PNode* pHead)
{
assert(pHead);
*pHead = NULL;
}

//获取新节点
PNode BuySListNode(DataType data)
{
PNode node = (PNode)malloc(sizeof(Node));
if (node)
{
node->_data = data;
node->_pNext = NULL;
}
return node;
}

//尾插
void SListPushBack(PNode* pHead, DataType data)
{
assert(pHead);
PNode newnode = BuySListNode(data);
if (NULL == (*pHead))
{
*pHead = newnode;
return;
}
PNode pCur = * pHead;
while (pCur->_pNext)
{
pCur = pCur->_pNext;
}
pCur->_pNext = newnode;
}

// 尾删 
void SListPopBack(PNode* pHead)
{
assert(pHead);
if (NULL == (*pHead))
{
return;
}
else if (NULL == (*pHead)->_pNext)
{
free(*pHead);
*pHead = NULL;
}
else
{
PNode pCur = *pHead;
PNode prev = pCur;
while (pCur->_pNext)
{
prev = pCur;
pCur = pCur->_pNext;
}
free(pCur);
prev->_pNext = NULL;
}
}

// 头插 
void SListPushFront(PNode* pHead, DataType data)
{
assert(pHead);
PNode newnode = BuySListNode(data);
if (NULL == (*pHead))
{
*pHead = newnode;
}
else
{
if (newnode)
{
newnode->_pNext = *pHead;
*pHead = newnode;
}
}
}

//头删
void SListPopFront(PNode* pHead)
{
assert(pHead);
if (NULL == (*pHead))
{
return;
}
PNode pDel = *pHead;
*pHead = (*pHead)->_pNext;
free(pDel);
}

// 查找值为data的结点,返回该结点在链表中的位置 
PNode SListFind(PNode pHead, DataType data)
{
assert(pHead);
if (NULL == pHead)
{
return NULL;
}
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)
{
assert(*pHead);
if (pos)
{
PNode newNode = BuySListNode(data);
if (newNode)
{
newNode->_pNext = pos->_pNext;
pos->_pNext = newNode;
}
}
}

// 删除链表pos位置上的结点 
void SListErase(PNode* pHead, PNode pos)
{
assert(*pHead);
if ((*pHead) == NULL && (pos == NULL))
{
return;
}
else if ((*pHead == pos))
{
*pHead = pos->_pNext;
}
else
{
PNode pCur = *pHead;
while (pCur->_pNext != pos)
{
pCur = pCur->_pNext;
}
pCur->_pNext = pos->_pNext;
free(pos);
}
}

// 销毁单链表
void SListDestroy(PNode* pHead)
{
assert(*pHead);
PNode psave = *pHead;
while (*pHead)
{
psave = (*pHead)->_pNext;
free(*pHead);
psave = *pHead;
}
}

//求链表中结点的个数
int SListSize(PNode pHead)
{
assert(pHead);
DataType count = 0;
while (pHead)
{
pHead = pHead->_pNext;
count++;
}
return count;
}

// 将链表中的结点清空 
void SListClear(PNode* pHead)
{
assert(*pHead);
PNode p = (*pHead)->_pNext;
PNode q = NULL;
while (p != NULL)
{
q = p->_pNext;
free(p);
p = q;
}
(*pHead)->_pNext = NULL;
}

// 获取链表中的最后一个结点,返回该结点的地址 
PNode SListBack(PNode pHead)
{
assert(pHead);

if (NULL == (pHead))
{
return NULL;
}
else
{
while ((pHead)->_pNext)
{
pHead = pHead->_pNext;
}
return pHead;
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值