队列与栈的一些基本问题

来源:互联网 发布:生产线显示屏 网络 编辑:程序博客网 时间:2024/06/14 13:30

实现栈

#pragma once#include<assert.h>template<class T>class stack{public:stack():_size(0), _capacity(0), _arr(NULL){}~stack(){if (_size != 0){delete[] _arr;_arr = NULL;}_size = 0;_capacity = 0;}stack(const stack& s):_capacity(new T(s._capacity-1)){swap(_arr, s._arr);_size = s._size;_capacity = s._capacity;}void CheckCapacity(){if (_capacity==_size){size_t Newcapacity = _capacity * 2 + 3;T* tmp = new T[Newcapacity];assert(tmp);size_t i = 0;while (i<_size){tmp[i] = _arr[i];i++;}delete[] _arr;_arr = tmp;_capacity = Newcapacity;}}void Push(const T& x){if (_size == _capacity){CheckCapacity();}_arr[_size] = x;_size++;}T& Min(){return Top();}void Pop(){if (_size > 0){_size--;}}T& Top(){return _arr[_size-1];}bool Empty(){return _size == 0;}size_t Size(){return _size;}void Dispaly(){if (_size == 0){cout << "STACK NULL"<<endl;}else{size_t i = 0;while (i < _size){cout << _arr[i]<<" ";i++;}cout << endl;}}protected:size_t _size;size_t _capacity;T* _arr;};

实现队列

#pragma once#include<assert.h>template<class T>struct QueueNode{int _data;QueueNode<T>* _next;};template<class T>class queue{public:typedef QueueNode<T> Node;queue():_head(NULL), _tail(NULL){}void  Push(const T& x){if (NULL == _head){_head = new Node;_head->_data = x;_head->_next = NULL;_tail = _head;}else{Node* tmp = new Node;tmp->_data = x;tmp->_next = NULL;_tail->_next = tmp;_tail = _tail->_next;}}void Pop(){assert(_head);Node* cur = _head;_head = _head->_next;delete cur;cur = NULL;}size_t Size(){size_t count = 0;if (_head == _tail){if (_head == NULL){return 0;}return 1;}else{Node* cur = _head;while (cur){++count;cur = cur->_next;}return count;}}T& Front(){return _head->_data;}T& Back(){return _tail->_data;}bool Empty(){if (_head == NULL){return true;}else{return false;}}protected:Node* _head;Node* _tail;};

#include"stack.h"#include"queue.h"#include<iostream>using namespace std;#include<assert.h>//检查合法出栈入栈//bool CheckLegal(int* stack_in,int* stack_out,size_t len_in,size_t len_out)//{//assert(stack_in&&stack_out);//if (len_in != len_out)//{//return false;//}//int i = 0;//int j = 0;//stack<int> s;//queue<int> q;//for (i = 0; i < len_in; i++)//{//s.Push(stack_in[i]);//}//int x = 2;//while (x--)//{//q.Push(s.Top());//s.Pop();//}//int a = 2;//while (a--)//{//s.Push(q.Front());//q.Pop();//}//while(s.Size() > 0 && s.Top() == stack_out[j])//{////s.Pop();//j++;////}//return (s.Size() > 0) ? false:true;//}//两个队列实现一个栈//template<class T>//class stack//{////public://void Push(const T& x)//{//q1.Push(x);//}//void Pop()//{//if (q1.Size() == 0)//{//cout << "STACK  NULL" << endl;//}//else if(q1.Size()==1)//{//q1.Pop();//}//else//{ //while (q1.Size()>1)//{//q2.Push(q1.Front());//q1.Pop();////}//q1.Pop();//while (!q2.Empty())//{//q1.Push(q2.Front());//q2.Pop();////}//}////}//T& Top()//{//return q1.Back();//}//size_t Size()//{//return q1.Size();//}//protected://queue<T> q1;//queue<T> q2;//////};//两个栈实现一个队列//////template<class T>//class queue//{//T top = 0;//public://void Push(const T& x)//{//s1.Push(x);//top = s1.Top();//}//void Pop()//{//while (!s1.Empty())//{//s2.Push(s1.Top());//s1.Pop();//}//s2.Pop();//}//T& Front()//{//while (!s1.Empty())//{////s2.Push(s1.Top());//s1.Pop();//}//return s2.Top();//}//T& Back()//{//return top;//}//size_t Size()//{//return s2.Size();//}//protected://stack<T> s1;//stack<T> s2;////};//O(1) 时间复杂度 的 Min函数//template<class T>//class Minstack//{//public://void Push(const T& x)//{//s.Push(x);//if (smin.Empty() || x <= smin.Top())//{//smin.Push(x);//}//}//void Pop()//{//if (s.Top() = smin.Top())//{//s.Pop();//smin.Pop();//}////}//T& Min()//{//return smin.Top();////}//////protected://stack<T> s;//stack<T> smin;//};//int main()//{//int stack_in[] = { 1,2,3,4,5 };//int stack_out[] = { 4,5,3,2,1 };//int len_in = sizeof(stack_in) / sizeof(stack_in[0]);//int len_out = sizeof(stack_out) / sizeof(stack_out[0]);//bool ret = CheckLegal(stack_in, stack_out, len_in, len_out);//if (ret)//cout << "出栈顺序合法" << endl;//else//cout << "出栈顺序不合法" << endl;//queue<int> q;//q.Push(1);//q.Push(2);//q.Push(3);//q.Push(4);//q.Pop();//q.Push(6);//q.Push(7);//q.Push(8);//q.Pop();//cout << q.Front() << endl;//cout << q.Back() << endl;//cout << q.Size()<< endl;////Minstack<int> s;//s.Push(2);//s.Push(2);//s.Push(3);//s.Push(4);//s.Push(1);//s.Pop();//cout<<s.Min()<<endl;/*stack<int> s;s.Pop();s.Push(1);s.Pop();cout << s.Top() << endl;s.Push(2);cout << s.Size() << endl;s.Pop();cout << s.Top() << endl;s.Push(3);s.Push(4);cout << s.Top() << endl;s.Pop();cout << s.Top() << endl;*///s.Dispaly();//cout << s.Min()<<endl;return 0;}