单链表类:class Slist

//单链表
//内容:默认4个,增删6个,打印,查找

#pragma once

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

typedef int DataType;	

//节点类,struct类默认公有
struct SListNode
{
	//构造函数和成员变量都是公有的
	SListNode* _next;
	DataType _data;
	//写了构造函数就不用BuyNode了,可以直接new
	SListNode(DataType x)	//注意L是大写
		:_data(x)
		,_next(NULL)
	{}
};

//单链表类
class Slist
{
	typedef SListNode Node;
public:
	Slist()
		:_head(NULL)
		,_tail(NULL)	//方便尾插
	{}

	Slist(const Slist& s)
		:_head(NULL)
		,_tail(NULL)
	{
		Copy(s);
	}

	~Slist()
	{
		Destory();
	}

	//Slist& operator=(const Slist& s)	//传统写法
	//{
	//	if(this != &s)
	//	{
	//		//释放this,头尾置空否则为野指针,后面PushBack会出错
	//		Destory();
	//		//
	//		Copy(s);
	//	}
	//	//自己给自己赋值,直接走这里,不走if语句
	//	return *this;
	//}

	Slist& operator=(Slist s)	//现代写法,拷贝构造没现代写法,因为没有带参的构造函数
	{
		swap(_head, s._head);
		swap(_tail, s._tail);
		return *this;
	}

	void Destory()	//释放链表
	{
		//_head为空不进入while所以这里无需判定_head不为空
		Node* cur = _head;
		while(cur)
		{
			Node* del = cur;
			cur = cur->_next;
			delete del;
			//Node* next = cur;
			//delete cur;
			//cur = next;
		}
		_head = _tail = NULL;
	}

	void Copy(const Slist& s)
	{
		Node* cur = s._head;
		while(cur)
		{
			PushBack(cur->_data);
			cur = cur->_next;
		}
	}

	void PushBack(DataType x);
	void PushFront(DataType x);
	void Insert(Node* pos, DataType x);
		//1.头插 2.随机
	void PopBack();
	void PopFront();
	void Erase(Node* pos);
		//1.头删 2.尾删 3.随机	
	void Print();
	Node* Find(DataType x);






private:
	//是指向节点的指针SListNode*,不是指向单链表类的指针Slist
	SListNode* _head;
	SListNode* _tail;
};

void Slist::PushBack(DataType x)
{
	//1.空 2.非空
	if(_head == NULL)
	{
		_head = _tail = new Node(x);
	}
	else
	{
		_tail->_next = new Node(x);
		_tail = _tail->_next;
	}
}

void Slist::PushFront(DataType x)
{
	//1.空 2.非空
	if(_head == NULL)
	{
		_head = _tail = new Node(x);
	}
	else
	{
		Node* tmp = new Node(x);
		tmp->_next = _head;
		_head = tmp;
	}
}

void Slist::Insert(Node* pos, DataType x)
{
	//因为能指定位置,则至少有一个节点
	//1.头插 2.随机位置
	assert(pos);
	if(_head == pos)
	{
		PushFront(x);
	}
	else
	{
		Node* prev = _head;
		while(prev->_next != pos)
		{
			prev = prev->_next;
		}
		Node* tmp = new Node(x);
		prev->_next = tmp;
		tmp->_next = pos;
	}
}

void Slist::PopBack()
{
	//1.空 2.1个 3.多个
	if(_head == NULL)
	{
		return;
	}
	else if(_head == _tail)
	{
		delete _tail;
		_head = _tail = NULL;
	}
	else
	{
		Node* prev = _head;
		while(prev->_next != _tail)
		{
			prev = prev->_next;
		}
		delete _tail;
		_tail = prev;
		_tail->_next = NULL;
	}
}

void Slist::PopFront()
{
	//1.空 2.1个 3.多个
	if(_head == NULL)
	{
		return;
	}
	else if(_head == _tail)
	{
		delete _head;
		_head = _tail = NULL;
	}
	else
	{
		Node* del = _head;
		_head = _head->_next;
		delete del;
	}
}

void Slist::Erase(Node* pos)
{
	//1.尾删 2.头删 3.随机
	assert(pos);
	if(pos == _tail)
	{
		PopBack();
	}
	else if(pos == _head)
	{
		PopFront();
	}
	else
	{
		Node* prev = _tail;
		while(prev->_next != pos)
		{
			prev = prev->_next;
		}
		prev->_next = pos->_next;
		delete pos;
	}
}

SListNode* Slist::Find(DataType x)	//这里不能写Node*因为typedef在类里面没在外面
{
	Node* cur = _head;
	while(cur)
	{
		if(cur->_data == x)
		{
			return cur;
		}
		cur = cur->_next;
	}
	return NULL;
}

void Slist::Print()
{
	Node* cur = _head;
	while(cur)
	{
		cout<<cur->_data<<" ";
		cur = cur->_next;
	}
	cout<<endl;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现上述功能的代码: ```cpp #include <iostream> using namespace std; class node{ public: int data; node* next; node(int d): data(d), next(nullptr) {} }; class slist{ public: slist(): head(nullptr) {} void insertFront(int d){ node* newNode = new node(d); newNode->next = head; head = newNode; } void insertBack(int d){ node* newNode = new node(d); if(head == nullptr){ head = newNode; } else{ node* curr = head; while(curr->next != nullptr){ curr = curr->next; } curr->next = newNode; } } bool remove(int d){ if(head == nullptr){ return false; } if(head->data == d){ node* temp = head; head = head->next; delete temp; return true; } node* curr = head; while(curr->next != nullptr && curr->next->data != d){ curr = curr->next; } if(curr->next == nullptr){ return false; } node* temp = curr->next; curr->next = curr->next->next; delete temp; return true; } void print(){ node* curr = head; while(curr != nullptr){ cout << curr->data << " "; curr = curr->next; } cout << endl; } void printReverse(){ printReverseHelper(head); cout << endl; } private: node* head; void printReverseHelper(node* curr){ if(curr == nullptr){ return; } printReverseHelper(curr->next); cout << curr->data << " "; } }; int main(){ slist list; list.insertFront(3); list.insertFront(2); list.insertFront(1); list.print(); // output: 1 2 3 list.insertBack(4); list.print(); // output: 1 2 3 4 list.remove(2); list.print(); // output: 1 3 4 list.printReverse(); // output: 4 3 1 return 0; } ``` 在上述代码中,我们实现了单链表 `slist`,其中包含了正向插入结点的函数 `insertFront`,反向插入结点的函数 `insertBack`,州除结点的函数 `remove`,输出链表所有成员数据域的函数 `print`,以及输出链表所有成员数据域的反向顺序的函数 `printReverse`。在主函数中,我们测试了这些函数的功能,并且最终输出了链表所有成员数据域的反向顺序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值