STL::List部分函数实现



  前面一篇文章讲了STL::List用法详解,简单介绍list的一些函数,在这分享一下双向链表的部分简单函数实现

      因为链表会出现频繁的增加、删除,所以使用迭代器较为简单


先定义节点类,

struct ListNode
{
	ListNode* _prev;
	ListNode* _next;
	T _data;
	ListNode(const T&x)
		:_prev(NULL)
		,_next(NULL)
		,_data(x)
	{}
	    
};
然后需要定义迭代器类
在迭代器这块,需要实现一些函数以供调用,例如operator++、operator++(int)、operator-、operator*、operator==和operator!=等一些函数。
template 
struct ListIterator
{
	typedef ListNodeNode;
	typedef ListIterator Self;
	ListIterator(Node* node)
		:_node(node)
	{}
	ListIterator()
	{}
	Ref operator*()//operator->
	{
		return _node->_data;
	}
	bool operator==(const Self&s)const
	{
		return _node==s._node;
	}
	bool operator!=(const Self&s)const
	{
		return _node!=s._node;
	}
	Self& operator++()//前置加加
	{
		_node=_node->_next;
		return *this;
	}
	Self operator++(int)//后置加加
	{
		Self tmp(_node);
		_node=_node->_next;
		return tmp;
	}
	Self& operator--()
	{
		_node=_node->prev;
        return*this;
	}
	Self operator--(int)
	{
		Slef tmp(_node);
		_node=_node->_prev;
		return tmp;
	}
	
	Node *_node;
};

这就需要定义链表类了,此涉及节点类,迭代器类的调用,要提前声明,涉及构造、析构、头插、头删、尾插、尾删、指定位置插入

指定位置删除、指定位置插入指定元素、去重、查找、清空等函数

当然插入和删除节点顺序尤为重要,在此就不做详细介绍,记得以前有过此类问题详解

template 
class List
{
	typedef ListNode Node;
public:
	typedef ListIterator Iterator;
	typedef ListIterator ConstIterator;
	Node* BuyNode(const T&x)
	{
		Node *node=new Node(x);
		return node;
	}
	List()
	{
		_head=BuyNode(T());
		_head->_next=_head;
		_head->_prev=_head;
	}
	void PushBack(const T&x)
	{
		Node *tail=_head->_prev;
		Node *tmp=BuyNode(x);

		tail->_next=tmp;
		tmp->_prev=tail;

		tmp->_next=_head;
		_head->_prev=tmp;
		//Tnsert(End(),x);
	}
	void PushFront(const T&x)
	{
		Node *tmp=BuyNode(x);
		Node *cur=_head->_next;

		_head->_next=tmp;
		tmp->_prev=_head;

		tmp->_next=cur;
		cur->_prev=tmp;
		//Tnsert(Begin(),x);
	}
	void Popback()
	{
		Node* _del=_head->_prev;
		Node* cur=_del->_prev;

		_head->_prev=cur;
		cur->_next=_head;

		Node* _tail=cur;
		delete _del;
		//Erase(--End());

	}
	void PopFront()
	{
		Node* del=_head->_next;
		Node* cur=del->_next;

		_head->_next=cur;
		cur->_prev=_head;
		delete del;
		//Erase(Begin());
	}
	//在指定位置前插入
	void Insert(Iterator pos,const T&x)
	{
		Node *cur=pos._node;
		Node *tmp=BuyNode(x);
		Node *prev=cur->_prev;
		
		tmp->_next=cur;
		cur->_prev=tmp;

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

	}
	void Erase(Iterator& pos)
	{
		assert(pos != End());
		Node* cur=pos._node;
		Node* prev=cur->_prev;
		Node* next=cur->_next;

		prev->_next=next;
		next->_prev=prev;
		delete cur;
	}
	Iterator Begin()
	{
		return Iterator(_head->_next);
	}
	Iterator End()
	{
		return Iterator(_head);
	}
private:
	Node*_head;
};

调试函数

#include
#include "List.h"
#include
using namespace std;
void TestList()
{
 List l;
 
 l.PushBack(1);
 l.PushBack(2);
 l.PushBack(3);
 l.PushBack(4);
 l.PushBack(5);
 l.PushBack(6);
 l.PushBack(7);
 l.PushBack(8);
 //第二个位置处插入0
 l.Insert(++l.Begin(),0);
 //删除第一个位置的节点
 l.Erase(++l.End());
 List::Iterator it = l.Begin();
 while(it != l.End())
 {
  cout<<*it<<" ";
  ++it;
 }
 cout<  //1 2 3 4 5 6 7 8
 //l.Popback();
 //l.Popback();
 //l.PopFront();
 //l.PopFront();
 //while(it != l.End())
 //{
 // cout<<*it<<" ";
 // ++it;
 //}
 //cout<  3 4 5 6 
 //l.Insert(++l.Begin(),0);
 ///*l.Erase(l.End()++);*/
 //while(it != l.End())
 //{
 // cout<<*it<<" ";
 // ++it;
 //}
 //cout<


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

源代码:

List.h

#pragma once
#include
template 
struct ListNode
{
	ListNode* _prev;
	ListNode* _next;
	T _data;
	ListNode(const T&x)
		:_prev(NULL)
		,_next(NULL)
		,_data(x)
	{}
	    
};
template 
struct ListIterator
{
	typedef ListNodeNode;
	typedef ListIterator Self;
	ListIterator(Node* node)
		:_node(node)
	{}
	ListIterator()
	{}
	Ref operator*()//operator->
	{
		return _node->_data;
	}
	bool operator==(const Self&s)const
	{
		return _node==s._node;
	}
	bool operator!=(const Self&s)const
	{
		return _node!=s._node;
	}
	Self& operator++()//前置加加
	{
		_node=_node->_next;
		return *this;
	}
	Self operator++(int)//后置加加
	{
		Self tmp(_node);
		_node=_node->_next;
		return tmp;
	}
	Self& operator--()
	{
		_node=_node->prev;
        return*this;
	}
	Self operator--(int)
	{
		Slef tmp(_node);
		_node=_node->_prev;
		return tmp;
	}
	
	Node *_node;
};
template 
class List
{
	typedef ListNode Node;
public:
	typedef ListIterator Iterator;
	typedef ListIterator ConstIterator;
	Node* BuyNode(const T&x)
	{
		Node *node=new Node(x);
		return node;
	}
	List()
	{
		_head=BuyNode(T());
		_head->_next=_head;
		_head->_prev=_head;
	}
	void PushBack(const T&x)
	{
		Node *tail=_head->_prev;
		Node *tmp=BuyNode(x);

		tail->_next=tmp;
		tmp->_prev=tail;

		tmp->_next=_head;
		_head->_prev=tmp;
		//Tnsert(End(),x);
	}
	void PushFront(const T&x)
	{
		Node *tmp=BuyNode(x);
		Node *cur=_head->_next;

		_head->_next=tmp;
		tmp->_prev=_head;

		tmp->_next=cur;
		cur->_prev=tmp;
		//Tnsert(Begin(),x);
	}
	void Popback()
	{
		Node* _del=_head->_prev;
		Node* cur=_del->_prev;

		_head->_prev=cur;
		cur->_next=_head;

		Node* _tail=cur;
		delete _del;
		//Erase(--End());

	}
	void PopFront()
	{
		Node* del=_head->_next;
		Node* cur=del->_next;

		_head->_next=cur;
		cur->_prev=_head;
		delete del;
		//Erase(Begin());
	}
	//在指定位置前插入
	void Insert(Iterator pos,const T&x)
	{
		Node *cur=pos._node;
		Node *tmp=BuyNode(x);
		Node *prev=cur->_prev;
		
		tmp->_next=cur;
		cur->_prev=tmp;

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

	}
	void Erase(Iterator& pos)
	{
		assert(pos != End());
		Node* cur=pos._node;
		Node* prev=cur->_prev;
		Node* next=cur->_next;

		prev->_next=next;
		next->_prev=prev;
		delete cur;
	}
	Iterator Begin()
	{
		return Iterator(_head->_next);
	}
	Iterator End()
	{
		return Iterator(_head);
	}
private:
	Node*_head;
};

test.cpp

#include
#include "List.h"
#include
using namespace std;
void TestList()
{
 List l;
 
 l.PushBack(1);
 l.PushBack(2);
 l.PushBack(3);
 l.PushBack(4);
 l.PushBack(5);
 l.PushBack(6);
 l.PushBack(7);
 l.PushBack(8);
 //第二个位置处插入0
 l.Insert(++l.Begin(),0);
 //删除第一个位置的节点
 l.Erase(++l.End());
 List::Iterator it = l.Begin();
 while(it != l.End())
 {
  cout<<*it<<" ";
  ++it;
 }
 cout<  //1 2 3 4 5 6 7 8
 //l.Popback();
 //l.Popback();
 //l.PopFront();
 //l.PopFront();
 //while(it != l.End())
 //{
 // cout<<*it<<" ";
 // ++it;
 //}
 //cout<  3 4 5 6 
 //l.Insert(++l.Begin(),0);
 ///*l.Erase(l.End()++);*/
 //while(it != l.End())
 //{
 // cout<<*it<<" ";
 // ++it;
 //}
 //cout<

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


 


你可能感兴趣的:(c/c++,stl,链表,双向链表部分函数实现)