网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

链表模板、队列模板、顺序表模板、栈模板、-创新互联

//利用容器适配器实现栈和队列


#pragma once
#include
#include
#include
using namespace std;


template
struct Node
{
public:
	Node(const T& d)
		:_next(NULL)
		, _prev(NULL)
	    ,_data(d){}
	T _data;
	Node *_next;
	Node *_prev;
};

template
class LinkList
{
public:
	LinkList()
		:_head(NULL)
		, _tail(NULL){}
	LinkList(const LinkList& list);
	LinkList& operator=(LinkList list);
	~LinkList();
	void PushBack(const T& d);
	void PushFront(const T& d);
	void PopBack();
	void PopFront();
	void Insert(Node *addr, const T& d);  //在当前结点后面插入元素
	Node* Search(const T& d);
	void Remove(const T& d);
	void RemoveAll(const T& d);
	void Sort();
	void Display();
	size_t Size();
	T& GetFront()
	{
		assert(_head);
		return _head->_data;
	}
	T& GetBack()
	{
		assert(_tail);
		return _tail->_data;
	}
private:
	Node *_head;
	Node *_tail;
};




template
size_t LinkList::Size()
{
	Node *cur = _head;
	size_t count = 0;
	while (cur)
	{
		count++;
		cur = cur->_next;
	}
	return count;
}


template
LinkList::LinkList(const LinkList& list)
			:_head(NULL)
			, _tail(NULL)
{
	Node *cur = list._head;
	while (cur)
	{
		PushBack(cur->_data);
		cur = cur->_next;
	}
}

template
LinkList& LinkList::operator=(LinkList list)
{

	std::swap(_head,list._head);
	std::swap(_tail,list._tail);
	return *this;
}

template
LinkList::~LinkList()
{
	Node *cur = _head;
	while (cur)
	{
		_head = _head->_next;
		delete cur;
		cur = _head;
	}
	_tail = NULL;
}

template
void LinkList::PushBack(const T& d)
{
	Node *NewHead = new Node(d);
	if (NULL == _head)
	{
		_head = NewHead;
		_tail = NewHead;
	}
	else
	{
		_tail->_next = NewHead;
		NewHead->_prev = _tail;
		_tail = _tail->_next;
	}
}

template
void LinkList::PushFront(const T& d)
{
	Node *NewHead = new Node(d);
	if (NULL == _head)
	{
		_head = NewHead;
		_tail = NewHead;
	}
	else
	{
		NewHead->_next = _head;
		_head->_prev = NewHead;
		_head = NewHead;
	}
}

template
void LinkList::PopBack()
{
	if (NULL == _head)
	{
		return;
	}
	else if (NULL==_head->_next)
	{
		delete _tail;
		_head = NULL;
		_tail = NULL;
	}
	else
	{
		Node *cur = _tail;
		_tail = _tail->_prev;
		_tail->_next = NULL;
		delete cur;
	}

}

template
void LinkList::PopFront()
{
	if (NULL == _head)
	{
		return;
	}
	else if (NULL == _head->_next)
	{
		delete _tail;
		_head = NULL;
		_tail = NULL;
	}
	else
	{
		Node *cur = _head;
		_head = _head->_next;
		_head->_prev = NULL;
		delete cur;
	}
}

template
void LinkList::Insert(Node *addr, const T& d)   //在当前结点后面插入元素
{
	Node *NewNode = new Node(d);
	if (_head == addr)
	{
		NewNode->_next = _head;
		_head->_prev = NewNode;
		_head = NewNode;
	}
	else if (_tail == addr)
	{
		PushBack(d);
	}
	else
	{
		NewNode->_next = addr->_next;
		addr->_next->_prev = NewNode;
		addr->_next = NewNode;
		NewNode->_prev = addr;
	}
}

template
Node* LinkList::Search(const T& d)
{
	Node *cur = _head;
	while (cur)
	{
		if (cur->_data == d)
		{
			return cur;
		}
		cur = cur->_next;
	}
	return NULL;
}

template
void LinkList::Remove(const T& d)
{
	Node *cur =Search(d);
	if (cur == _head)
	{
		PopFront();
	}
	else if (cur == _tail)
	{
		PopBack();
	}
	else
	{
		cur->_prev->_next = cur->_next;
		cur->_next->_prev = cur->_prev;
		delete cur;
	}
}

template
void LinkList::RemoveAll(const T& d)
{
	while (Search(d) != NULL)
	{
		Remove(d);
	}
}

template
void LinkList::Sort()
{
	Node *end = _tail;
	while (_head != end)
	{
		Node *cur = _head;
		int flag = 1;
		while (cur!=end)
		{
			if (cur->_data > cur->_next->_data)
			{
				T tmp = cur->_data;
				cur->_data = cur->_next->_data;
				cur->_next->_data=tmp;
				flag = 0;
			}
			cur = cur->_next;
		}
		if (flag)
		{
			return;
		}
		end = end->_prev;
	}
}

template
void LinkList::Display()
{
	Node *cur = _head;
	while (cur)
	{
		cout << cur->_data << " ";
		cur = cur->_next;
	}
	cout << endl;
}

站在用户的角度思考问题,与客户深入沟通,找到万安网站设计与万安网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:做网站、成都网站制作、企业官网、英文网站、手机端网站、网站推广、域名注册、网页空间、企业邮箱。业务覆盖万安地区。
#include"LinkList.h"

template class Container>
class Queue
{
public:
	void Push(const T& d);
	void Pop();
	T& Front();
	T& Back();
	size_t Size();
	bool Empty();
	void Display()
	{
		while (!Empty())
		{
			cout << Front() << " ";
			Pop();
		}
		cout << endl;
	}
private:
	Container _con;
};

template class Container>
void Queue::Push(const T& d)
{
	_con.PushBack(d);
}

template class Container>
void Queue::Pop()
{
	_con.PopFront();
}

template class Container>
T& Queue::Front()
{
	return _con.GetFront();
}

template class Container>
T& Queue::Back()
{
	return _con.GetBack();
}

template class Container>
size_t Queue::Size()
{
	return _con.Size();
}

template class Container>
bool Queue::Empty()
{
	return _con.Size()== 0;
}

#pragma once
#include
#include
#include
#include
using namespace std;

template
class Seqlist
{
public:
	Seqlist();
	Seqlist(const Seqlist& seq);
	Seqlist & operator=(Seqlist seq);
	~Seqlist();
	void PushBack(const T& d);
	void PushFront(const T& d);
	void PopBack();
	void PopFront();
	void Insert(int index,const T& d);
	int Search(const T& d);
	void Remove(const T& d);
	void RemoveAll(const T& d);
	void Sort();
	void Reserve(int n);
	void Display();
	int GetSize();
	T& operator[](int index);
private:
	void CheckCapacity(int n=0);
private:
	T *_pdata;
	int _sz;
	int _capacity;
};

template
T& Seqlist::operator[](int index)
{
	assert(index >= 0);
	assert(index < _sz);
	return _pdata[index];
}

template
int Seqlist::GetSize()
{
	return _sz;
}

template
Seqlist::Seqlist()
             :_sz(0)
              , _capacity(0)
			  , _pdata(NULL){}

template
Seqlist::Seqlist(const Seqlist& seq)
{

	_pdata = new T[seq._capacity];
	for (int i = 0; i < seq._sz; i++)
	{
		_pdata[i] = seq._pdata[i];
	}
	_sz = seq._sz;
	_capacity = seq._capacity;
}


template
Seqlist& Seqlist::operator=(Seqlist seq)
{
	swap(_pdata,seq._pdata);
	_sz = seq._sz;
	_capacity = seq._capacity;
	return *this;
}

template
Seqlist::~Seqlist()
{
    if (_pdata != NULL)
	{
		delete[] _pdata;
		_pdata = NULL;
		_sz = 0;
		_capacity = 0;
	}
}

template
void Seqlist::CheckCapacity(int n=0)
{
	if (_sz == _capacity||n>_capacity)
	{
		int NewCapacity =2*_capacity+1;
		if (n > _capacity)
		{
			NewCapacity = n;
		}
		T* tmp = new T[NewCapacity];
		for (int i = 0; i < _sz; i++)
		{
			tmp[i] =_pdata[i];
		}
		delete[] _pdata;
		_pdata = tmp;
		_capacity = NewCapacity;
	}

}

template
void Seqlist::PushBack(const T& d)
{
	CheckCapacity();
	_pdata[_sz++] = d;
}

template
void Seqlist::PushFront(const T& d)
{
	CheckCapacity();
	//memmove(_pdata + 1,_pdata,sizeof(T)*_sz);
	for (int i =_sz;i>0; i--)
	{
		_pdata[i] = _pdata[i-1];
	}
	_pdata[0] = d;
	_sz++;
}

template
void Seqlist::PopBack()
{
	_sz--;
}

template
void Seqlist::PopFront()
{
	//memmove(_pdata,_pdata+1,sizeof(T)*(--_sz));
	for (int i = 0; i<_sz-1; i++)
	{
		_pdata[i] = _pdata[i+1];
	}
	_sz--;
}

template
void Seqlist::Insert(int index, const T& d)
{
	assert(index >= 0);
	assert(index<_sz);
	CheckCapacity();
	//memmove(_pdata+index+1,_pdata+index,sizeof(T)*(_sz-index));
	for (int i = _sz; i>index; i--)
	{
		_pdata[i] = _pdata[i - 1];
	}
	_sz++;
	_pdata[index] = d;
}

template
int Seqlist::Search(const T& d)
{
	int i = 0;
	for (i = 0; i < _sz; i++)
	{
		if (_pdata[i] == d)
		{
			return i;
		}
	}
	return -1;              //没找到返回-1
}

template
void Seqlist::Remove(const T& d)
{
	int index = Search(d);
	if (index == -1)
	{
		return;
	}
	else
	{
		//memmove(_pdata+index,_pdata+index+1,sizeof(T)*(_sz-index-1));
		for (int i =index; i<_sz-1; i++)
		{
			_pdata[i] = _pdata[i+1];
		}
		_sz--;
	}
}

template
void Seqlist::RemoveAll(const T& d)
{
	while (Search(d) != -1)
	{
		Remove(d);
	}
}

template
void Seqlist::Reserve(int n)
{
	CheckCapacity(n);
}

template
void Seqlist::Sort()
{
	int end = _sz - 1;
	for (int i = 0; i < _sz - 1; i++)
	{
		int flag = 1;
		int k = end;
		for (int j = 0; j _pdata[j+1])
			{
				T tmp = _pdata[j];
				_pdata[j] = _pdata[j + 1];
				_pdata[j + 1] = tmp;
				flag = 0;
				k= j;
			}
		}
		if (flag == 1)
		{
			return;
		}
		end = k;
	}
}


template
void Seqlist::Display()
{
	for (int i = 0; i < _sz; i++)
	{
		cout << _pdata[i] << " ";
	}
	cout << endl;
}

#include"Seqlist.h"

template class Container>
class Stack
{
public:
	bool Empty();
	void Push(const T& d);
	void Pop();
	T& Top();
	int Size();
	void Display()
	{
		while (!Empty())
		{
			cout << Top() << " ";
			Pop();
		}
		cout << endl;
	}
private:
	Container _con;
};

template class Container>
bool Stack::Empty()
{
	return _con.GetSize()==0;
}

template class Container>
void Stack::Pop()
{
	_con.PopBack();
}

template class Container>
void Stack::Push(const T& d)
{
	_con.PushBack(d);
}

template class Container>
int Stack::Size()
{
	return _con.GetSize();
}

template class Container>
T& Stack::Top()
{
	int sz=_con.GetSize()-1;
	return _con[sz];
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


标题名称:链表模板、队列模板、顺序表模板、栈模板、-创新互联
分享地址:http://bjjierui.cn/article/dppddo.html

其他资讯