C++实现双向链表

C++实现双向链表

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

typedef int DataType;

typedef struct ListNode
{
	DataType data;
	ListNode* prev;
	ListNode* next;

	ListNode(const DataType d)
		:data(d)
		, prev(NULL)
		, next(NULL)
	{}
}Node;

class List
{
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)
		{
			Clear();
			Node* cur = l._head;

			while (cur)
			{
				PushBack(cur->data);
				cur = cur->next;
			}
		}

		return *this;
	}

	List& operator=(const List& l)
	{
		if (this != &l)
		{
			List tmp(l);
			swap(_head, tmp._head);
			swap(_tail, tmp._tail);
		}

		return *this;
	}*/

	List& operator=(List l)
	{
		if (this != &l)
		{
			Swap(l);
		}

		return *this;
	}

	~List()
	{
		Clear();
	}

	void PushBack(DataType d)
	{
		Node* tmp = new Node(d);
		if (_head == NULL)
		{
			_head = _tail = tmp;
		}
		else
		{
			_tail->next = tmp;
			tmp->prev = _tail;
			_tail = tmp;
			_tail->next = NULL;
		}
	}

	void PopBack()
	{
		if (_head == NULL)
		{
			cout << "链表为空,无法尾删" << endl;
		}
		else if (_head == _tail)
		{
			delete _head;
			_head = _tail = NULL;
		}
		else
		{
			Node* del = _tail;
			_tail = _tail->prev;
			_tail->next = NULL;
			delete del;
		}
	}

	void PushFront(DataType d)
	{
		Node* tmp = new Node(d);
		if (_head == NULL)
		{
			_head = _tail = tmp;
		}
		else
		{
			_head->prev = tmp;
			tmp->next = _head;
			_head = tmp;
			_head->prev = NULL;
		}
	}

	void PopFront()
	{
		if (_head == NULL)
		{
			cout << "链表为空,无法头删" << endl;
		}
		else if (_head == _tail)
		{
			delete _head;
			_head = _tail = NULL;
		}
		else
		{
			Node* del = _head;
			_head = _head->next;
			_head->prev = NULL;
			delete del;
		}
	}

	void Insert(Node* pos, DataType d)  //在pos前插入一个数据
	{
		assert(pos);
		Node* tmp = new Node(d);

		if (pos == _head)
		{
			PushFront(d);
		}
		else
		{
			pos->prev->next = tmp;
			tmp->prev = pos->prev;
			tmp->next = pos;
			pos->prev = tmp;
		}
	}

	void Erase(Node* pos)  //删除pos位置的数据
	{
		assert(pos);

		if (pos == _head)
		{
			PopFront();
		}
		else if (pos == _tail)
		{
			PopBack();
		}
		else
		{
			pos->prev->next = pos->next;
			pos->next->prev = pos->prev;
			delete pos;
		}
	}

	Node* Find(DataType d)
	{
		Node* cur = _head;

		while (cur)
		{
			if (cur->data == d)
			{
				return cur;
			}
			cur = cur->next;
		}

		return NULL;
	}

	void Print()
	{
		Node* cur = _head;
		while (cur)
		{
			cout << cur->data << " ";
			cur = cur->next;
		}
		cout << endl;
	}

private:
	void Clear()
	{
		Node* cur = _head;

		while (cur)
		{
			Node* del = cur;
			cur = cur->next;
			delete[] del;
		}
		_head = _tail = NULL;
	}

	void Swap(List& l)
	{
		swap(_head, l._head);
		swap(_tail, l._tail);
	}

private:
	Node* _head;
	Node* _tail;
};

测试函数如下

#include "List.h"

void Test1()
{
	List l1;
	/*l1.PushBack(1);
	l1.PushBack(2);
	l1.PushBack(3);
	l1.PushBack(4);
	l1.Print();*/

	/*List l2(l1);
	l2.Print();
	
	List l3;
	l3 = l1;
	l3.Print();*/

	/*l1.PopBack();
	l1.Print();
	l1.PopBack();
	l1.PopBack();
	l1.PopBack();
	l1.Print();
	l1.PopBack();*/

	l1.PushFront(4);
	l1.PushFront(3);
	l1.PushFront(2);
	l1.PushFront(1);
	l1.Print();

	l1.PopFront();
	l1.Print();
	l1.PopFront();
	l1.PopFront();
	l1.PopFront();
	l1.Print();
	l1.PopFront();
}

void Test2()
{
	List l1;
	l1.PushBack(1);
	l1.PushBack(2);
	l1.PushBack(3);
	l1.PushBack(4);
	l1.Print();

	Node* res = l1.Find(2);

	/*l1.Insert(res, 0);
	l1.Print();*/

	l1.Erase(res);
	l1.Print();
}

int main()
{
	//Test1();
	Test2();

	system("pause");
	return 0;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值