C++实现单链表(类和对象)——简单实现

"test.cpp"

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
#include "LinkList.h"

void Test()
{
	LinkList list;
	for (int i = 1;i < 6;i++)
	{
		list.PushBack(i);
	}
	cout<<list<<endl;

	list.PushFrond(10);
	list.PushFrond(9);
	list.PushFrond(8);
	cout<<list<<endl;

	list.PopBack();
	list.PopBack();
	cout<<list<<endl;
	list.PopFrond();
	list.PopFrond();
	cout<<list<<endl;

	LinkListNode* cur = list.Find(9);
	if (cur != NULL)
	{
		cout<<*cur<<endl;
	}
	
	list.Insert(list.Find(3),4);
	cout<<list<<endl;
	list.Insert(list.Find(1),100);
	cout<<list<<endl;
	list.Insert(list.Find(10),11);
	cout<<list<<endl;

	list.Remove(list.Find(4));
	cout<<list<<endl;
	list.Remove(list.Find(10));
	cout<<list<<endl;
	list.Remove(list.Find(100));
	cout<<list<<endl;

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


"LinkList.h"

#pragma once 
typedef int DataType;
struct LinkListNode
{
	LinkListNode(DataType data)
		:_data(data)
		,_next(NULL)
	{}
	DataType _data;
	LinkListNode* _next;
};

class LinkList
{
	friend ostream& operator<<(ostream& os,LinkList& list);
	typedef LinkListNode Node;
public:
	LinkList()
		:_head(NULL)
		,_tail(NULL)
	{}
	~LinkList()
	{
		Node* cur = _head;
		while (cur)
		{
			Node* tmp = cur;
			cur = cur->_next;
			delete tmp;
			tmp = NULL;
		}
	}
public:
	void PushBack(const DataType data)
	{
		if ((_head == NULL) && (_tail == NULL))
		{
			_head = new Node(data);
			_tail = _head;
		}
		else
		{
			Node* tmp = new Node(data);
			_tail->_next = tmp;
			_tail = _tail->_next;
		}
	}
	void PushFrond(const DataType data)
	{
		Node* cur = new Node(data);
		if ((_head == NULL) && (_tail == NULL))
		{
			_head = cur;
			_tail = _head;
		} 
		else
		{
			cur->_next = _head;
			_head = cur;
		}
	}
	void PopBack()
	{
		if ((_head == NULL) && (_tail == NULL))
		{
			cout<<"链表已空"<<endl;
			return;
		} 
		else
		{
			Node* del = _head;
			Node* delprev = NULL;
			while (del != _tail)
			{
				delprev = del;
				del = del->_next;
			}
			_tail = delprev;
			_tail->_next = NULL;//尾指针的下一个位置一定要记得置空,否则程序中断
			delete del;
			del = NULL;
		}
	}
	void PopFrond()
	{
		if ((_head == NULL) && (_tail == NULL))
		{
			cout<<"链表已空"<<endl;
		} 
		else
		{
			Node* del = _head;
			_head = _head->_next;
			delete del;
			del = NULL;
		}
	}
	Node* Find(const DataType data)
	{
		if ((_head == NULL) && (_tail == NULL))
		{
			cout<<"List is Null"<<endl;
			return NULL;
		} 
		else
		{
			Node* cur = _head;
			while (cur)
			{
				if (cur->_data == data)
				{
					return cur;
				} 
				else
				{
					cur = cur->_next;
				}
			}
		}
		return NULL;
	}
	//在某一个节点的后面插入一个节点
	void Insert(Node* cur,const DataType data)
	{
		Node* tmp = new Node(data);
		if (cur == NULL)
		{
			_head = tmp;
			_tail = _head;
		}
		//这个节点是尾结点的情况
		if (cur == _tail)
		{
			cur->_next = tmp;
			_tail = tmp;
		} 
		else
		{
			tmp->_next = cur->_next;
			cur->_next = tmp;
		}
	}
	//删除当前节点
	void Remove(Node* cur)
	{
		if (cur == NULL)
		{
			cout<<"no find"<<endl;
			return;
		}
		Node* tmp = _head;
		if (cur == _head)
		{
			Node* del = _head;
			_head = _head->_next;
			delete del;
			del = NULL;
		}
		else if (cur == _tail)
		{
			while (tmp->_next != cur)
			{
				tmp = tmp->_next;
			}
			_tail = tmp;
			_tail->_next = NULL;
			delete cur;
			cur = NULL;
		}
		else
		{
			//找到删除节点的前一个位置
			while (tmp->_next == cur)
			{
				tmp = tmp->_next;
			}
			tmp->_next = cur->_next;
			delete cur;
			cur = NULL;
		}
	}
private:
	Node* _head;//头指针
	Node* _tail;//尾指针
};

ostream& operator<<(ostream& os,LinkListNode& node)
{
	os<<node._data<<" ";
	return os;
}
ostream& operator<<(ostream& os,LinkList& list)
{
	LinkListNode* cur = list._head;
	while (cur)
	{
		os<<*cur<<" ";
		cur = cur->_next;
	}
	return os;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值