码迷,mamicode.com
首页 > 其他好文 > 详细

自己写个list玩玩

时间:2015-06-15 18:54:37      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

/*如果采用单向链表,end()函数如何表示?
 *所以必须得采用循环链表的方式?
 *
 */

#ifndef LIST
#define LIST

#include<iostream>

using namespace std;

//声明
template <class T>
struct Node{
	T data;
	struct Node<T>* next;
	Node(struct Node<T>* p):data(p->data),next(p->next){}
	Node(){}
};

template <class T>
struct _iterator{//迭代器
	typedef Node<T> ListNode;
	typedef ListNode* pListNode;
	
	_iterator(){}
	_iterator(pListNode p):pToNode(p){}
	_iterator(const _iterator& p):pToNode(p.pToNode){}
	
	const _iterator& operator=(const _iterator& rhs)
	{
		pListNode temp=pToNode;
		pToNode=new ListNode(rhs.pToNode);
		delete temp;
		temp=NULL;
		return *this;		
	}
	
	const _iterator& operator=(const pListNode& rhs)
	{
		pListNode temp=pToNode;
		pToNode=new ListNode(rhs);
		delete temp;
		temp=NULL;
		return *this;
	}
	
	T operator*()
	{
		return (*pToNode).data;
	}
	
	//单向链表,没有自减操作
	_iterator& operator++()
	{
		pToNode=pToNode->next;
		return *this;
	}	
	_iterator operator++(int)
	{
		_iterator tmp=*this;
		++*this;
		return tmp;
	}
	
	bool operator==(const _iterator& p)
	{
		return pToNode==p.pToNode;
	}
	
	bool operator!=(const _iterator& p)
	{
		return pToNode!=p.pToNode;
	}
	
	pListNode pToNode;
};


template <class T>
class ListSelf{
public:
	ListSelf<T>()
	{
		node=new ListNode();
		//node->data=0;//初始化,类型未确定,故不能初始化
		node->next=node;
	}
	
	typedef Node<T> ListNode;
	typedef ListNode* pListNode;
	typedef _iterator<T> iterator;
	
	void push_back(const T& value);
	void push_front(const T& value);
	void pop_back();
	void pop_front();
	
	iterator begin()const
	{return iterator(node->next);}
	iterator end()const
	{return iterator(node);}
	
	T front()const;
	T back()const;	
	void print(ostream& output=cout)const;	
	int size()const;
	
	void erase(iterator& ,int n);
	void erase(iterator& iter);
	
private:
	pListNode node;
};


//实现
template <class T>
void ListSelf<T>::push_back(const T& value)
{
	pListNode p=node;
	while(p->next!=node)
	{
		p=p->next;
	}
	pListNode newNode=new ListNode();
	newNode->data=value;
	newNode->next=node;
	p->next=newNode;
}

template <class T>
void ListSelf<T>::print(ostream& output) const
{
	pListNode p=node;
	if(p->next==node)
		return;
	do{
		p=p->next;
		output<<p->data<<"  ";
	}while(p->next!=node);
	output<<endl;
}

template <class T>
int ListSelf<T>::size() const
{
	pListNode p=node;
	int j(0);
	while(p->next!=node)
	{
		j++;
		p=p->next;
	}
	return j;
}

template <class T>
void ListSelf<T>::push_front(const T& value)
{
	pListNode p=node;
	pListNode q=new ListNode();
	q->data=value;
	q->next=p->next;
	p->next=q;
}

template <class T>
void ListSelf<T>::pop_back()
{
	pListNode p=node;
	pListNode q=p;
	while(p->next!=node)
	{
		q=p;
		p=p->next;
	}
	if(p==q)
		return;
	else
	{
		q->next=p->next;
		delete p;
		p=NULL;
	}	
}

template <class T>
void ListSelf<T>::pop_front()
{
	pListNode p=node;
	if(p->next==node)//throw error
		return;
	p=p->next;
	node->next=p->next;
	delete p;
	p=NULL;
}

template <class T>
T ListSelf<T>::front() const
{
	pListNode p=node;
	if(p->next==node)
		return -1;//should throw error
	p=p->next;
	return p->data;
}

template <class T>
T ListSelf<T>::back() const
{
	pListNode p=node;
	if(p->next==node)
		return -1;//should throw error
	while(p->next!=node)
	{
		p=p->next;
	}
	return p->data;
}

template <class T>
void ListSelf<T>::erase(iterator& iter,int n)
{
	pListNode p=node;
	while(p->next!=iter.pToNode && p->next!=node)
	{
		p=p->next;
	}
	if(p->next==node)
		return;
	int i=0;
	
	do{
		pListNode q=p;
		p=p->next;
		q->next=p->next;
		delete p;
		p=q;		
		i++;
	}while(i<n && p->next!=node);
	
	iter.pToNode=p->next;	
	// iter=p->next;
}

template <class T>
void ListSelf<T>::erase(iterator& iter)
{
	pListNode p=node;
	while(p->next!=iter.pToNode && p->next!=node)
	{
		p=p->next;
	}
	if(p->next==node)
		return;
	pListNode q=p;
	p=p->next;
	q->next=p->next;
	delete p;
	p=NULL;
	iter.pToNode=q->next;
}


#endif //LIST

自己写个list玩玩

标签:

原文地址:http://blog.csdn.net/walker19900515/article/details/46505169

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!