C++实现无头结点的双向链表

typedef int DataType;  //实现双向链表

struct ListNode
{
	ListNode* _next;  //设置结点,指向下一节点
	ListNode* _prev;  //设置结点,指向上一节点

	DataType _data;   //设置数据结点
	ListNode(const DataType& x)  //将const DataType转换为ListNode类型
		:_next(NULL)
		, _prev(NULL)
	{
		_data = x;
	}
};
class List
{
	typedef ListNode Node;
public:
	List()
		:_head(NULL)
		,_tail(NULL)
	{}

	List(const List& l)   //拷贝构造函数
		:_head(NULL)
		, _tail(NULL)
	{
		Node* cur = l._head;
		while (cur)
		{
			PushBack(cur->_data);
			cur = cur->_next;
		}
	}

	List& operator = (const List& l)
	{
		if (this != &l)
		{
			Node* cur = l._head;
			while (cur)
			{
				PushBack(cur->_data);
				cur = cur->_next;
			}
		} 
	}

	~List()    //释放内存
	{
		Node* cur = _head;
		while (cur)
		{
			Node* del = cur;
			cur = cur->_next;
			delete del;
		}
		_head = _tail = NULL;
	}

public:
	void PushBack(const DataType& x)//尾结点插入
	{
		if (_head == NULL)
		{
			_head = _tail = new Node(x);
		}
		else 
		{
			Node* tmp = new Node(x);
			_tail->_next = tmp;
			tmp->_prev = _tail;
			_tail = tmp;
		}
	}

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

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

	void PopFront()//头结点删除
	{
		if (_head == NULL)
		{
			return;
		}
		else if (_head->_next == NULL)
		{
			delete _head;
			_head = _tail = NULL;
		}
		else
		{
			Node* next = _head->_next;
			delete _head;
			next->_prev = NULL;
			_head = next;

		}
	}

	void Insert(Node* pos, const DataType& x)   //指定位置的插入
	{
		assert(pos);
		//两种情况:
		//1.pos == _head
		//2.pos != _head
		if (pos == _head)
		{
			PushFront(x);
		}
		else
		{
			Node* tmp = new Node(x);
			Node* net = pos;
			Node* prev = net->_prev;

			prev->_next = tmp;
			tmp->_next = net;
			net->_prev = tmp;
			tmp->_prev = prev;
		}
	}

	void Erase(Node* pos)   //删除指定位置
	{
		assert(pos);
		if (pos == _head)
		{
			PopFront();
		}
		else if (pos == _tail)
		{
			PopBack();
		}
		else
		{
			Node* cur = pos;
			Node* prev = cur->_prev;
			Node* next = cur->_next;
			prev->_next = next;
			next->_prev = prev;
			delete cur;
			cur = NULL;
		}
	}
	Node* Find(const DataType& x)    //寻找x的位置
	{
		Node* cur = _head;
		while (cur)
		{
			if (cur->_data == x)
			{
				return cur;
			}
			cur = cur->_next;
		}
		return (Node*)-1;
	}
	
	void Reverse()    //逆置
	{
		//Node* cur = _head;    // 交换next和prev
		//while (cur)
		//{
		//	swap(cur->_prev, cur->_next);
		//	cur = cur->_prev;
		//}
		/*swap(_head, _tail);*/
		Node* head = _head;   //交换头尾结点的数据 
		Node* tail = _tail;
		while (!(head == tail || tail->_next == head))
		{
			swap(head->_data, tail->_data);
			head = head->_next;
			tail = tail->_prev;
		}
	}
	void Print()    //链表的打印
	{
		Node* cur = _head;
		while (cur)
		{
			cout << cur->_data << " ";
			cur = cur->_next;
		}
		cout << endl;
	}
private:
	Node* _head;  //头结点
	Node* _tail;  //尾结点

};

void TextList()
{
	List l1;
	l1.PushBack(1);
	l1.PushBack(2);
	l1.PushBack(3);
	l1.PushBack(4);
	l1.Print();
	List l2(l1);
	l2.Print();
	l2.PopBack();
	l2.Print();
	//l1.PopBack();
	//l1.PopBack();
	//l1.PopBack();
	//l1.PopBack();
	//l1.Print();
	/*l1.PushFront(4);
	l1.PushFront(3);
	l1.PushFront(2);
	l1.PushFront(1);
	l1.Print();*/
	/*l1.PopBack();
	l1.PopBack();
	l1.PopBack();
	l1.PopBack();
	l1.Print();*/
	//l1.PopFront();
	//l1.PopFront();
	//l1.PopFront();
	//l1.PopFront();
	//l1.PopFront();
	//l1.Print();
	/*l1.Insert(l1.Find(2), 6);
	l1.Print();
	l1.Erase(l1.Find(2));
	l1.Print();*/
	l1.Reverse();
	l1.Print();
}
#include<iostream>
using namespace std;
#include<windows.h>
#include<assert.h>
#include"List.h"

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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值