C++实现单链表

分3个文件
SList.h是函数声明、数据定义的头文件
SList.cpp是函数实现的源文件
Main.cpp是测试用例

具体代码如下:
//SList.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<memory.h>
//数据类型定义
typedef int DataType;
//结点类型定义
typedef struct ListNode
{
DataType _data;
struct ListNode * _next;
}ListNode;
//初始化
void InitList(ListNode *& head);
//申请结点空间
ListNode * BuyNode(DataType x);
//尾插
void PushBack(ListNode *& pHead,const DataType x);
void PopBack(ListNode *& pHead, DataType &x); //尾删
void PopFront(ListNode* &pHead, DataType &x);//头删
void ShowList(ListNode * pHead); //遍历显示链表
void PushFront(ListNode * & pHead, DataType x);  //头插
void PopFront(ListNode* &pHead, DataType &x);  //前删
ListNode * Find(ListNode *pHead, DataType x); //找到值为x的结点指针
void Insert(ListNode *pos, DataType x);  //在pos之后插入
void Erase(ListNode * &pHead, ListNode *pos);  //删除pos指针指向的结点
void Remove(ListNode* &pHead, DataType x);  //删除值为x的结点
void Reverse(ListNode * &pHead);  //逆序链表

//SList.cpp
// 不带头结点的单链表
#include"SList.h"
void InitList(ListNode * & head)  //初始化
{
head = NULL;
}
ListNode * BuyNode(DataType x)  //申请结点空间
{
ListNode * tmp = (ListNode *)malloc(sizeof(ListNode));
assert(tmp);
tmp->_next = NULL;  //使得这个next不是野指针
tmp->_data = x;
return tmp;
}
void PushBack(ListNode * & pHead, DataType x)  //尾插
{
if (NULL == pHead)
{
//空链表
pHead = BuyNode(x);
}
else
{
ListNode * tail = pHead;
while (tail->_next != NULL)
{
tail = tail->_next;
}
tail->_next = BuyNode(x);
}
}
void PushFront(ListNode * & pHead, DataType x) //头插
{
if (NULL == pHead)
pHead = BuyNode(x);
else
{
ListNode *tmp = BuyNode(x);
tmp->_next = pHead;
pHead = tmp;
}
}
void PopBack(ListNode *  &pHead, DataType &x)  // //尾删
{
if (NULL == pHead)
{
printf("This List is Empty!");
return ;
}
else if (NULL == pHead->_next)
{
free(pHead);
pHead = NULL;
}
else
{
ListNode  *prevtail=NULL,* tail = pHead;
while (tail->_next != NULL)
{
prevtail = tail;
tail = tail->_next;
}
x = tail->_data;
prevtail->_next = NULL;
free(tail);
}
}
//以int为例的打印函数
void ShowList(ListNode * pHead)
{
if (NULL == pHead)
{
printf("This List is Empty");
return;
}
while (pHead != NULL)
{
printf("%d-> ", pHead->_data);
pHead = pHead->_next;
}
printf("NULL \n");
}
ListNode * Find(ListNode *pHead, DataType x) //找到值为x的结点指针
{
ListNode *cur = pHead;
while(cur != NULL)
{
if (cur->_data == x)
return cur;
cur = cur->_next;
}
return NULL; //没有找到
}
void Insert(ListNode *pos, DataType x)  //在pos之后插入
{
assert(pos);  //要是null的话 终止程序
ListNode *tmp = BuyNode(x);
tmp->_next = pos->_next;
pos->_next = tmp;
}
void PopFront(ListNode* &pHead, DataType &x)//头删
{
assert(pHead);
ListNode* tmp = pHead;
pHead = pHead->_next;
free(tmp);
}
void Erase(ListNode * &pHead, ListNode *pos)  //删除pos指针指向的结点
{
ListNode * tmp=NULL,*cur=pHead;

assert(pHead);
if (NULL == pos)
{
printf("pos is NULL\n");
return;
}
if (pHead == pos)
{
pHead = pHead->_next;
}
while (cur->_next != pos)
{
cur = cur->_next;
}
//删除结点
tmp = cur->_next;
cur->_next = tmp->_next;
free(tmp); //释放
}
void Remove(ListNode* &pHead, DataType x)  //删除第一个值为x的结点
{
ListNode *prevCur = pHead, *cur = pHead;
assert(cur);
while (cur!= NULL&&cur->_data !=x  )
{
prevCur = cur;
cur = cur->_next;
}
if (prevCur == cur) //如果找到的是第一个结点
{
ListNode *tmp = pHead;
pHead = pHead->_next;
free(tmp);
}
else if(NULL == cur)  //没有找到的情况
{
printf("No Find!\n");
return;
}
else  //找到了
{
prevCur->_next = cur->_next;
free(cur);
}
}
void Reverse( ListNode * &pHead)  //逆序链表
{

ListNode * cur = pHead,*tmp=NULL;
ListNode * newHead =NULL;
assert(pHead);
while (cur != NULL)
{
tmp = cur;
cur = cur->_next;
tmp->_next = newHead;
newHead = tmp;
}
pHead = newHead;
}

//Main.cpp 测试用例
#include"SList.h"
void Test()
{
ListNode *head;
InitList(head);
PushBack(head, 7);
PushBack(head, 8);
PushBack(head, 9);
PushBack(head, 10);
ShowList(head);
/
PushFront(head, 11);
PushFront(head, 12);
PushFront(head, 13);
ShowList(head);
int x=0;
PopBack(head, x);
ShowList(head);
PopBack(head, x);
ShowList(head);
//

Insert(Find(head, 12), 15);  //12后面插入15
Insert(Find(head, 12), 16);  //12后面插入16
ShowList(head);
//
Reverse(head);
ShowList(head);
//

PopFront(head, x);
ShowList(head);
//
printf("///\n");
Remove(head,130);
ShowList(head);
Erase(head, Find(head, 12));
ShowList(head);
//

}

int main()
{
Test();
system("pause");
return 0;
}

测试用例运行结果:

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值