环形队列
#include<iostream>
using namespace std;
#include<stack>
#include<string>
#include<vector>
class queue//push/pop/front/back/empty/size
{
public:
queue(int size = 10)
:cap_(size)
, front_(0)
, rear_(0)
, size_(0)
{
pQue_ = new int[cap_];
};
public:
//入队
void push(int val)
{
if (front_ == (rear_+1)% cap_)
{
expand(2 * cap_);
}
pQue_[rear_] = val;
rear_ = (rear_ + 1) % cap_;
size_++;
}
//出队
void pop()
{
if (front_ == rear_)
throw"queue is empty";
front_ = (front_ + 1)% cap_;
size_--;
}
//队头
int front() const
{
if (front_ == rear_)
throw"queue is empty";
return pQue_[front_];
}
//队尾
int back() const
{
if (front_ == rear_)
throw"queue is empty";
return pQue_[(rear_+ cap_-1)% cap_];
}
//判空
bool empty() const
{
return front_ == rear_;
}
//队列元素个数
int size() const
{
return size_;
//遍历统计
/*int size = 0;
for (int i = front_; i != rear_; i = (i + 1) % cap_)
{
size++;
}
return size;*/
}
~queue()
{
delete[]pQue_;
}
private:
void expand(int size)
{
int* p = new int[size];
int i = front_, j = 0;
for (; i != rear_; i = (i + 1) % cap_)
{
p[j++] = pQue_[i];
}
delete []pQue_;
pQue_ = p;
cap_ = size;
front_ = 0;
rear_ = j;
}
private:
int* pQue_;//队名
int cap_;//空间容量
int front_;//队头
int rear_;//队尾
int size_;//队列元素个数
};
int main()
{
int arr[1] = { 0 };
queue que;
for (int v : arr)
{
que.push(v);
}
que.pop();
cout << que.size() << endl;
cout << endl;
cout << que.empty() << endl;
}
链式队列
#include<iostream>
using namespace std;
#include<stack>
#include<string>
#include<vector>
class linkQueue//双向循环链表 push/pop/front/back/empty/size
{
public:
linkQueue()
{
head_ = new Node();
head_->next_ = head_;
head_->pre_ = head_;
size_ = 0;
}
public:
//入队 尾部
void push(int val)
{
Node* node = new Node(val);
Node* p = head_->pre_;
node->next_ = head_;
head_->pre_ = node;
p->next_ = node;
size_++;
}
//出队 头部
void pop()
{
Node* p = head_->next_;
head_->next_ = p->next_;
p->next_->pre_ = head_;
delete p;
size_--;
}
//队头
int front() const
{
if (head_->next_ = head_)
throw"queue is empty";
return head_->next_->data_;
}
//队尾
int back() const
{
if(head_->next_=head_)
throw"queue is empty";
return head_->pre_->data_;
}
//判空
bool empty() const
{
return size_ == 0;
//return head_->next_ == head_;
}
//队列元素个数
int size() const
{
return size_;
}
~linkQueue()
{
Node* p = head_->next_;
while (p != head_)
{
head_->next_ = p->next_;
p->next_->pre_ = head_;
delete p;
p = head_->next_;
}
delete head_;
head_ = nullptr;
}
private:
struct Node
{
Node(int data = 0)
: data_(data)
, next_(nullptr)
, pre_(nullptr)
{};
int data_;
Node* next_;
Node* pre_;
};
Node* head_;//指向头结点
int size_;
};
int main()
{
int arr[8] = { 12,5,35,652,7622,47,15,44 };
linkQueue que;
for (int v : arr)
{
que.push(v);
}
cout << que.size() << endl;
que.push(10);
que.pop();
que.push(10);
que.pop();
cout << que.size() << endl;
}
两个栈实现一个队列
#include<iostream>
using namespace std;
#include<stack>
class myQueue
{
public:
myQueue()
{
}
public:
void push(int val)//入
{
s1.push(val);
}
int pop()//出
{
if (s2.empty())
{
while (!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
int val = s2.top();
s2.pop();
return val;
}
int peek() //查看第一个元素
{
if (s2.empty())
{
while (!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
return s2.top();
}
bool empty() const
{
return s1.empty() && s2.empty();
}
private:
stack<int> s1;
stack<int> s2;
};
int main()
{
myQueue mq;
mq.push(1);
mq.push(2);
mq.push(3);
cout << mq.peek() << endl;
mq.push(4);
cout << mq.peek() << endl;
mq.pop();
cout << mq.peek() << endl;
return 0;
}
两个队列实现一个栈
#include<iostream>
using namespace std;
#include<queue>
class myStack
{
public:
myStack()
{
q1 = new queue<int>;
q2 = new queue<int>;
}
~myStack()
{
delete q1;
delete q2;
q1 = nullptr;
q2 = nullptr;
}
public:
void push(int val)//入
{
q1->push(val);
while (!q2->empty())
{
q2->push(q1->front());
q1->pop();
}
queue<int> *q = q1;
q1 = q2;
q2 = q;
}
int pop()//出
{
int val = q2->front();
q2->pop();
return val;
}
int top() //查看第一个元素
{
return q2->front();
}
bool empty() const
{
return q2->empty();
}
private:
queue<int> *q1;
queue<int> *q2;
};
int main()
{
return 0;
}