实现简单的队和栈结构,附代码,图

</pre><span style="font-size:18px;">数据结构相信大家都不陌生<img alt="大笑" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif" />,经过了多操作多类别的链表,下来是比较简单的栈和队列,栈(FILO),队列(FIFO). </span><p></p><p></p><p><span style="font-size:18px;"><strong>1.栈结构.</strong></span></p><p><span style="font-size:18px;">栈呢,类似于顺序表,可以将你的数据压栈,相当于压在你所创的顺序表的[0]下标处,_top指向你最后一个元素的下一位置。说白了就是对一个动态数组进行操作,_top和定义的_capacity,还有增容函数比较重要,掌握这些就可以编写简单的栈啦。</span></p><p><span style="font-size: 18px;">数据入栈,你的_top就得向上走一位啦,相对的pop一个数据时,_top就得相应的减1。</span></p><p><span style="font-size:18px;">如下图:</span></p><p><img src="https://img-blog.csdn.net/20160407131937184?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></p><p><span style="font-size:18px;">来到代码段</span>:</p><p><pre name="code" class="cpp"><span style="font-size:18px;"><span style="font-size:18px;"></span></span><pre name="code" class="cpp">#include<iostream>
#include<assert.h>
using namespace std;

 
<span style="font-size:18px;">template <class T>
class stack
{
public:
	stack():_top(0),_capacity(0),a(NULL)
	{}//将对象成员初始化
	~stack()
	{
		delete[] a;
		_top = 0;
		_capacity = 0;
	}
	void checkCapacity()
	{
		if(_top==_capacity)//判断容量是否够用,不够则按照下面规则进行增容
		{
			T *temp = new T[_capacity*2+1];
			_capacity=_capacity*2+1;
			memcpy(temp,a,sizeof(T)*_top);//将原来的复制到新增容空间
			delete[] a;
			a = temp;//使原来的指针指向增容后的空间
		}
		else
			return;
	}
	void push(T x)
	{
		checkCapacity();//进行是否需要增容的判断
		if(_top<=_capacity)//容量够用进行插入
		{
			a[_top++]=x;
		}
	}
	void pop()
	{
		assert(_top>0);//断言_top>0
		_top--;
	}
	void display()
	{
		while(_top)
		{
			cout<<a[_top-1]<<"->"; //_top 指向最后一个数字的后一位
			_top--;
		}
		cout<<"Nul"<<endl;
	}
private:
	T *a;
	int _top;
	size_t _capacity;
};
int main()
{
	stack<int> sc;
	sc.push(1);
	sc.push(2);
	sc.push(3);
	sc.push(4);
	sc.push(5);
	sc.push(6);
	sc.push(6);
	sc.pop();
	sc.display();
	return 0;
}
</span>
2.队列

队列的理解呢,就好比你去食堂打饭,有个先来后到的顺序,你先来那你必定先买到饭,后来的肯定后买到喽。通过概念的理解,这种结构用一个单链表就可以很好的解决问题了,实现先进先出。(注意_head和_tail的操作)

图解:


话不多说上代码:

#include<iostream>
#include<assert.h>
using namespace std;
template<class T>
struct Node
{
	Node(const T &x):_data(x),_next(NULL)
	{}
	T _data;
	Node<T>* _next;
};//定义一个节点的结构体
template<class T>
class deque
{
public:
	deque():_head(NULL),_tail(NULL)
	{}//将头尾初始化
	void push(const T &x)
	{
		if(_head==NULL)//判断头是否为空
		{
			_head = new Node<T>(x);
			_tail = _head;
		}
		else
		{
			Node<T> *tmp;
			tmp = new Node<T>(x); 
			_tail->_next= tmp;
			_tail = tmp;//进行尾插
		}
	}
	void pop()
	{
		assert(_head!=NULL);//头不能为空
		if(_head == _tail)//只有一个节点
		{
			delete _head;
			_head = _tail = NULL;
		}
		Node<T> *tmp = _head;//定义一个临时变量保存当前头节点
		_head = _head->_next;
		delete tmp;
	}
	T& GetTail()
	{
		return _tail->_data;
	}
	void display()
	{
		assert(_head!=NULL);
		while(_head)
		{
			cout<<_head->_data<<"->";
			_head=_head->_next;
		}
		cout<<"Nul"<<endl;
	}
private:
	Node<T> *_head;
	Node<T> *_tail;
};
int main()
{
	deque<int> d1;
	d1.push(1);
	d1.push(2);
	d1.push(3);
	d1.push(4);
	d1.push(5);
	d1.push(6);
	d1.pop();
	d1.display();
	int t = d1.GetTail();
	cout<<t<<endl;
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值