c++模拟实现单链表

**思路:**单链表属于线性表中的连式结构,逻辑上数连续的,但是物理存储上是不连续的,声明链表节点结构,然后就是指针的运用,
test.h

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<iostream>
using namespace std;

typedef int SLTDataType;

typedef struct SListNode
{
	SLTDataType _value;
	struct SListNode* _next;
}SListNode;

class SList
{
public:
	SList();
	~SList();
	SListNode* BuySListNode(SLTDataType x);
	void SListPushFront(SLTDataType x);
	void SListPopFront();
	SListNode* SListFind(SLTDataType x);
	void SListInsert(SLTDataType x);
	void SListErase(SLTDataType x);
	void SlistReverse();
	void SListPrint();

private:
	SListNode* _head;//头结点
};

main.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"test.h"


SList::SList()//构造函数
{
	this->_head = nullptr;
}
SList::~SList()
{
	//判断链表是否为空
	if (this->_head == nullptr)
	{
		cout << "链表为空" << endl;
	}

	while (this->_head != nullptr)
	{
		SListNode* cur = this->_head;
		this->_head = cur->_next;
		delete cur;
	}
}
SListNode* SList::BuySListNode(SLTDataType x)
{
	//SListNode* tmp = new SListNode;
	SListNode* tmp = (SListNode*)malloc(sizeof(SListNode));
	if (tmp != nullptr)
	{
		tmp->_next = nullptr;
		tmp->_value = x;
	}
	return tmp;
}
void SList::SListPushFront(SLTDataType x)
{
	SListNode* tmp = BuySListNode(x);
	tmp->_next = this->_head;
	this->_head = tmp;
}
void SList::SListPopFront()
{
	if (this->_head == nullptr)
	{
		cout << "链表为空" << endl;
		return;
	}

	SListNode* cur = this->_head;
	this->_head = cur->_next;
	delete cur;
}
SListNode* SList::SListFind(SLTDataType x)
{
	if (this->_head == nullptr)
	{
		cout << "链表为空" << endl;
		return nullptr;
	}

	SListNode* cur = this->_head;
	while (cur)
	{
		if (cur->_value == x)
		{
			return cur;
		}
		cur = cur->_next;
	}
	cout << "表中没有该值" << endl;
	return nullptr;
}
void SList::SListInsert(SLTDataType x)
{
	if (this->_head == nullptr)
	{
		cout << "链表为空" << endl;
		return ;
	}

	SListNode*cur, *prev;
	prev = cur = this->_head;
	
	while (cur)
	{
		if (cur->_value == x)
		{

				SListNode* tmp = this->BuySListNode(x);
				tmp->_next = prev->_next;
				prev->_next = tmp;
			
		}

		prev = cur;
		cur = cur->_next;
	}
}
void SList::SListErase(SLTDataType x)
{
	if (this->_head == nullptr)
	{
		cout << "链表为空" << endl;
		return;
	}

	SListNode* cur, *prev = nullptr;
	cur = this->_head;

	while (cur)
	{
		if (cur->_value == x)
		{
			if (cur == this->_head)
			{
				this->_head = cur->_next;
			}
			else
			{
				prev->_next = cur->_next;
			}
			delete cur;
			return;
		}
		prev = cur;
		cur = cur->_next;
	}
}

void  SList::SlistReverse()
{
	if (this->_head == nullptr)
	{
		cout << "链表为空" << endl;
		return;
	}
	else if (this->_head->_next == nullptr)
	{
		return;
	}
	else
	{
		SListNode* cur, *prev, *next;
		cur = this->_head;
		next = prev = nullptr;
		while (cur)
		{
			next = cur->_next;
			cur->_next = prev;
			prev = cur;
			cur = next;
		}
		this->_head = prev;
	}
}

void SList::SListPrint()
{
	if (this->_head == nullptr)
	{
		cout << "链表为空" << endl;
		return;
	}
	else
	{

		SListNode* cur = this->_head;
		while (cur)
		{
			cout << cur->_value << "->";
			cur = cur->_next;
		}
		cout << "NULL" << endl;
	}
}

int main()
{
	SList sl;
	sl.SListPushFront(0);
	sl.SListPushFront(1);
	sl.SListPushFront(2);
	sl.SListPushFront(3);
	sl.SListPushFront(4);

	sl.SListPrint();

	//sl.SListPopFront();
	//sl.SListInsert(3);
	//sl.SListErase(3);
	sl.SlistReverse();

	sl.SListPrint();
    //sl.SListFind(8);
	system("pause");
	return 0;
}

截图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值