数据结构与STL:list的实现(双向链表)

声明:
list.h

#pragma once

template<class T>
class list {//list类
	struct node {
		T data;
		node* next;
		node* pre;
	};
	node* head = NULL;
	node* tail = NULL;
	unsigned len;
public:
	class iterator//iterator类
	{
		friend class list<T>;
	private:
		node* nodePtr;
	public:
		iterator() {
			nodePtr = NULL;
		}
		iterator(node* x) {
			nodePtr = x;
		}
		iterator& operator++();
		iterator operator++ (int);
		iterator& operator--();  //pre-decrement
		iterator operator--(int); //post-decrement
		T& operator*();
		bool operator== (const iterator& x);
	};

	list();
	~list();
	list(const list<T>& x);
	void push_front(const T& x);	//头入
	void push_back(const T& x);	//尾入
	iterator insert(iterator position, const T& x);	//按位置插入
	void pop_front();	//头出
	void pop_back();	//尾出
	void erase(iterator position);	//移除某位置
	void erase(iterator first, iterator last);	//移除一段位置
	unsigned size() const;	//返回size
	bool empty() const;	//判断是否为空
	iterator begin();	//指向头部的指针
	iterator end();	//指向结尾的指针
	void splice(iterator position, list<T>& x);	//在position前,插入一段元素
	list<T>& operator=(const list<T>& x);
};

定义
list.cpp

#include<iostream>
#include"list.h"
using namespace std;


template<class T>
typename list<T>::
iterator& list<T>::iterator:: operator++()
{
	nodePtr = nodePtr->next;
	return *this;
}

template<class T>
typename list<T>::
iterator list<T>::iterator::operator++(int)
{
	iterator p = *this;
	nodePtr = nodePtr->next;
	return p;
}

template<class T>
typename list<T>::
iterator& list<T>::iterator::operator--()
{
	nodePtr = nodePtr->pre;
	return *this;
}

template<class T>
typename list<T>::
iterator list<T>::iterator::operator--(int)
{
	iterator temp = *this;
	nodePtr = nodePtr->pre;
	return temp;
}

template<class T>
T& list<T>::iterator::operator*()
{
	return nodePtr->data;
}

template<class T>
bool list<T>::iterator::operator==(const iterator& x)
{
	return nodePtr == x.nodePtr;
}

template<class T>
list<T>::list()
{
	len = 0;
}

template<class T>
list<T>::~list()
{
	node* temp = head;
	while (temp != tail)
	{
		head = head->next;
		delete temp;
		temp = head;
	}
	len = 0;
}

template<class T>
list<T>::list(const list<T>& x)
{
	if (x.head == NULL)
		head = tail = NULL;
	else
	{
		node* p = x.head;
		head = new node();
		head->data = p->data;
		node* s = head;
		while (p != x.tail)
		{
			p = p->next;
			s->next = new node();
			s->next->pre = s;
			s = s->next;
			s->data = p->data;
			len++;
		}
	}
}

template<class T>
void list<T>::push_front(const T& x)
{
	node* newhead = new node();
	newhead->data = x;
	newhead->next = head;
	newhead->next->pre = head;
	newhead->pre = NULL;
	len++;
	if (head == NULL)
	{
		head = newhead;
		tail = head;
	}
	else
	{
		head->pre = newhead;
		head = newhead;

	}
}

template<class T>
void list<T>::push_back(const T& x)
{
	node* newtail = new node();
	newtail->data = x;
	newtail->next = NULL;
	newtail->pre = tail;
	len++;
	if (tail == NULL)
	{
		tail = newtail;
		head = tail;
	}
	else
	{
		tail->next = newtail;
		tail = newtail;
	}
}

template<class T>
typename list<T>::
iterator list<T>::insert(iterator position, const T& x)//从后insert
{
	node* s = new node();
	s->data = x;
	s->pre = position.nodePtr;
	s->next = position.nodePtr->next;
	position.nodePtr->next->pre = s;
	position.nodePtr->next = s;
	if (position.nodePtr == tail)
	{
		tail = s;
	}
	len++;
	return position;
}

template<class T>
void list<T>::pop_front()
{
	len--;
	node* temp = head->next;
	delete head;
	head = NULL;
	if(temp == NULL) 
	{
		tail = NULL;
		return;
	}
	head = temp;
	head->pre = NULL;
}

template<class T>
void list<T>::pop_back()
{
	if (!empty())
	{
		len--;
		node* temp = tail;
		tail->pre->next = NULL;
		tail = tail->pre;
		delete temp;
	}
}

template<class T>
void list<T>::erase(iterator position)
{
	node* p = position.nodePtr;
	if (p != NULL)
	{
		if (p->next != NULL)
		{
			p->pre->next = p->next;
			p->next->pre = p->pre;
		}
		else
		{
			p->pre->next = NULL;
			p = p->pre;
			tail = p;
		}
		len--;
	}

}

template<class T>
void list<T>::erase(iterator first, iterator last)
{
	node* p = first.nodePtr;
	if (first == head)
	{
		head = last.nodePtr->next;
	}
	if (last == tail)
	{
		tail = tail->pre;
	}
	if(!(first == last))
	{
		first++;
		delete p;
		p = first.nodePtr;
	}

}

template<class T>
unsigned list<T>::size() const
{
	return len;
}

template<class T>
bool list<T>::empty() const
{
	return len == 0;
}

template<class T>
typename list<T>::
iterator list<T>::begin()
{
	return iterator(head);
}

template<class T>
typename list<T>::
iterator list<T>::end()
{
	return iterator(tail);
}

template<class T>
void list<T>::splice(iterator position, list<T>& x)
{
	node* p = position.nodePtr;
	len = len + x.len;
	if (head == NULL && tail == NULL)
	{
		head = x.head;
		tail = x.tail;
	}
	else if (p->pre == NULL && p != NULL)
	{
		x.tail->next = p;
		p->pre = x.tail;
		head = x.head;
	}
	else if (p == NULL && p->pre != NULL)
	{
		x.head->pre = p->pre;
		p->pre->next = x.head;
		x.tail->next = p;
		p->pre = x.tail;
		tail = x.tail;
	}
	else
	{
		x.head->pre = p->pre;
		x.tail->next = p;
		p->pre->next = x.head;
		p->pre = x.tail;
	}
	
}

template<class T>
list<T>& list<T>::operator=(const list<T>& x)
{
	node* s = head;
	head->data = x.head->data;
	iterator first = x.begin();
	iterator last = x.last();
	while (!(first->next == last))
	{
		first++;
		s->next->data = first->data;
		s = s->next;
	}
	/*
	node* p = x.head;
	node* s = head;
	while (p != x.tail)
	{
		s->data = p->data;
		s->next = new node();
		s->next = p->next;
		s->pre = p->pre;
		s = s->next;
		p = p->next;
	}
	head = x.head;
	tail = x.tail;
	len = x.len;
	*/
	tail = x.tail;
	len = x.len;
	return *this;
}

测试类
main.cpp

#include<iostream>
#include"list.cpp"
using namespace std;

int main()
{
	list<int>* l = new list<int>;
	for (int i = 0; i <= 5; ++i)
	{
		l->push_back(i);
	}
	list<int>::iterator first = l->begin();
	list<int>::iterator last = l->end();
	cout << *first << " ";
	while (!(first == last))
	{
		first++;
		cout << *first << " ";
	}
	cout << endl;

	for (int i = 0; i <= 5; i++)
	{
		l->push_front(i*3);
	}
	first = l->begin();
	last = l->end();
	cout << *first << " ";
	while (!(first == last))
	{
		first++;
		cout << *first << " ";
	}
	cout << endl;

	for (int i = 0; i <= 5; i++)
	{
		l->pop_front();
	}

	first = l->begin();
	last = l->end();
	cout << *first << " ";
	while (!(first == last))
	{
		first++;
		cout << *first << " ";
	}
	cout << endl;

	/*
	for (int i = 0; i < 4; i++)
	{
		l->pop_back();
	}
	first = l->begin();
	last = l->end();
	while (!(first == last))
	{
		cout << *first << endl;
		first++;
	}
	cout << endl;
	*/

	list<int>* m = new list<int>;
	for (int i = 0; i < 5; ++i)
	{
		m->push_back(i*2);
	}
	first = m->begin();
	last = m->end();
	cout << *first << " ";
	while (!(first == last))
	{
		first++;
		cout << *first << " ";
	}
	cout << endl;

	first = l->begin();

	l->splice(first, *m);
	first = l->begin();
	last = l->end();
	cout << *first << " ";
	while (!(first == last))
	{
		first++;
		cout << *first << " ";
	}
	cout << endl;

	cout << l->empty() << endl;
	cout << l->size() << endl;
	cout << endl;

	first = l->begin();
	last = l->end();


	int x;
	cin >> x;
	l->insert(first,x);
	cout << *first << " ";
	while (!(first == last))
	{
		first++;
		cout << *first << " ";
	}
	cout << endl;

	list<int>*y = new list<int>;
	y = l;
	first = y->begin();
	last = y->end();
	cout << *first << " ";
	while (!(first == last))
	{
		first++;
		cout << *first << " ";
	}
	cout << endl;

	first = l->begin();
	last = l->end();
	last--;
	last--;
	l->erase(first, last);
	first = l->begin();
	last = l->end();
	cout << *first << " ";
	while (!(first == last))
	{
		first++;
		cout << *first << " ";
	}
	cout << endl;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值