用C++简单实现单链表

SList.h

#ifndef __SLIST_H__
#define __SLIST_H__

#include<iostream>
#include<assert.h>

using namespace std;

typedef int DataType;

struct SListNode
{
	SListNode* _next; 
	DataType _data; 

	SListNode(const DataType &x)
		:_data(x)
		,_next(NULL)
	{

	}

};

class SList
{

	typedef SListNode Node;

public:
	SList()                //构造函数
		:_head(NULL)
		,_tail(NULL)
	{

	}

	SList(const SList &sl)          //拷贝构造
		:_head(NULL)
		,_tail(NULL)
	{
		Node *cur = sl._head;
		while (cur)
		{
			if (_head == NULL)
			{
				_head = _tail = new Node(cur->_data);
			}
			else
			{
				_tail->_next = new Node(cur->_data);
				_tail = _tail->_next;
			}
			cur = cur->_next;
		}
	}

	~SList()            //析构函数
	{
		Node *cur = _head;
		
		while(cur)
		{
			Node *del = cur;
			cur = cur->_next;
			delete del;
		}

		_head = _tail = NULL;
	}

	void PushBack(const DataType &x)           //尾插
	{
		if (_head == NULL)
		{
			_head = _tail = new Node(x);
		}
		else
		{
			_tail->_next = new Node(x);
			_tail = _tail->_next;
		}
	}

	void PopBack()                   //尾删
	{
		if (_head == NULL)
		{
			return;
		}
		else if (_head->_next == NULL)
		{
			delete _head;
			_head = _tail = NULL;
		}
		else
		{
			Node *prev = _head;
			while (prev->_next != _tail)
			{
				prev = prev->_next;
			}
			_tail = NULL;
			_tail = prev;
			_tail->_next = NULL;
		}
	}

	void PushFront(const DataType &x)            //头插
	{
		if (_head == NULL)
		{
			_head = _tail = new Node(x);
		}
		else
		{
			Node *tmp = new Node(x);
			tmp->_next = _head;
			_head = tmp;
		}
	}

	void PopFront()                 //头删
	{
		if (_head == NULL)
		{
			return;
		}
		else if (_head->_next == NULL)
		{
			delete _head;
			_head = _tail = NULL;
		}
		else
		{
			Node *cur = _head->_next;
			delete _head;
			_head = cur;
		}
	}

	Node* Find(const DataType &x)            //查找
	{
		Node *cur = _head;
		while (cur)
		{
			if (cur->_data == x)
			{
				return cur;
			}
			cur = cur->_next;
		}
		return (Node *)-1;
	}

	void Insert(Node *pos, const DataType &x)         //插入
	{
		assert(pos >= 0);
		Node *cur = _head;

		if (cur == pos)
		{
			PushFront(x);
		}
		else
		{
			Node *prev = cur;
			Node *tmp = new Node(x);
			while (cur != pos)
			{
				prev = cur;
				cur = cur->_next;
			}

			tmp->_next = cur;
			prev->_next = tmp;
		}

	}

	void Erase(Node* pos)              //删除
	{
		assert(pos);
		if (pos == _head) 
		{
			PopFront();
		}
		else if (pos == _tail)
		{
			PopBack();
		}
		else
		{
			Node *cur = _head;
			Node *prev = cur;
			while (cur != pos)
			{
				prev = cur;
				cur = cur->_next;
			}
			prev->_next = cur->_next;
		}

	}

	void Reverse()                   //逆置
	{
		assert(_head);
		Node *cur = _head;
		Node *NewHead = NULL;
		while (cur)
		{
			Node* tmp = cur;
			cur = cur->_next;
			tmp->_next = NewHead;
			NewHead = tmp;
			if (NewHead == _head)
			{
				_tail = NewHead;
			}
		}
		_head = NewHead;
	}

	void Print()             //打印
	{
		Node *cur = _head;
		while (cur)
		{
			cout << cur->_data << " ";
			cur = cur->_next;
		}
		cout << endl;
	}
private:
	Node* _head;             //头指针
	Node* _tail;             //尾指针
};

#endif // !__SLIST_H__

Test.cpp

#include"SList.h"

void Test()
{
	SList sl1;
	sl1.PushBack(1);
	sl1.PushBack(2);
	sl1.PushBack(3);
	sl1.PushBack(4);
	sl1.Print();

	sl1.Find(1);
	sl1.Insert(sl1.Find(1), 5);
	sl1.Print();

	sl1.Insert(sl1.Find(3), 6);
	sl1.Print();

	sl1.Insert(sl1.Find(4), 7);
	sl1.Print();

	sl1.Erase(sl1.Find(5));
	sl1.Erase(sl1.Find(6));
	sl1.Erase(sl1.Find(7));
	sl1.Print();

	sl1.Reverse();
	sl1.Print();

	sl1.PopBack();
	sl1.PopBack();
	sl1.PopBack();
	sl1.PopBack();
	sl1.PopBack();
	sl1.PopBack();
	sl1.PopBack();
	sl1.Print();

	sl1.PushFront(1);
	sl1.PushFront(2);
	sl1.PushFront(3);
	sl1.PushFront(4);
	sl1.Print();
	sl1.PopFront();
	sl1.Print();
	sl1.PopFront();
	sl1.PopFront();
	sl1.PopFront();
	sl1.PopFront();
	sl1.Print();
}

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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值