链表的基本操作

#pragma once


//1. 实现以下链表的基本操作,这部分作业是本次要交的
typedef int DataType;
typedef struct Node
{
struct Node* _pNext;
DataType _data;
}Node, *PNode;


//不带头结点的单链表//
// .h
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
// 链表初始化
void SListInit(PNode* pHead)
{
assert(pHead);
*pHead = NULL;
}

PNode BuySListNode(DataType data)
{
PNode pNewNode = (PNode)malloc(sizeof(Node));
if (NULL == pNewNode)
return NULL;
pNewNode->_pNext = NULL;
pNewNode->_data = data;

return pNewNode;
}

 


// 尾插
void SListPushBack(PNode* pHead, DataType data)
{
PNode pNewNode = NULL;
assert(pHead);
pNewNode = BuySListNode(data);
//空链表
if (NULL == *pHead)
*pHead = pNewNode;
//非空
else
{
PNode pCur = *pHead;
while (pCur->_pNext)
pCur = pCur->_pNext;
pCur->_pNext = pNewNode;

}


}

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

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

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

}


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

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 SListDestroy(PNode* pHead)
{
SListClear(pHead);
}

int SListSize(PNode pHead)
{
int count = 0;
PNode pCur = pHead;
while (pCur)
{
count++;
pCur = pCur
}

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

return count;
}

}

// 将链表中的结点清空
void SListClear(PNode* pHead)
{
PNode pDelNode = NULL;
assert(pHead);

while (*pHead)
{
pDelNode = *pHead;
*pHead = pDelNode->_pNext;
free(pDelNode);
}

}

// 获取结点
PNode BuySListNode(DataType data)
{

PNode pNewNode = (PNode)malloc(sizeof(Node));
if (NULL == pNewNode)
return NULL;

pNewNode->_pNext = NULL;
pNewNode->_data = data;

return pNewNode;
}

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

while (pCur->_pNext)
pCur = pCur->_pNext;

return pCur;
}


void PrintList(PNode pHead)
{
PNode pCur = pHead;
while (pCur)
{
printf("%d---->", pCur->_data);
pCur = pCur->_pNext;
}
printf("NULL\n");
}
void TestSList()
{
PNode pHead;
SListInit(&pHead);
SListPushBack(&pHead, 1);
SListPushBack(&pHead, 2);
SListPushBack(&pHead, 3);
SListPushBack(&pHead, 4);
SListPushBack(&pHead, 5);
SListPushBack(&pHead, 6);
PrintList(pHead);

SListPopBack(&pHead);
PrintList(pHead);

SListPopBack(&pHead);
SListPopBack(&pHead);
SListPopBack(&pHead);
SListPopBack(&pHead);
SListPopBack(&pHead);

}

转载于:https://www.cnblogs.com/lx1997/p/8685577.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值