利用栈实现队列的操作
来源:互联网 发布:php curl 下载pdf文件 编辑:程序博客网 时间:2024/06/05 04:00
#include<iostream>
#include<stdexcept>
using namespace std;
class Stack
{
public:
// 初始化为一个空堆栈
Stack() :m_top(NULL){}
~Stack()
{
for (Node* next; m_top; m_top = next)
{
next = m_top->m_next;
delete m_top;
}
}
void push(int data)
{
/*Node* node = new Node;
node->m_data = data;
node->m_next = m_top;
m_top = node;*/
m_top = new Node(data, m_top);
}
int pop(void)
{
if (empty())
throw UnderFlow();
int data = m_top->m_data;
Node* next = m_top->m_next;
delete m_top;
m_top = next;
return data;
}
bool empty() const
{
return !m_top;
}
private: //下溢异常
class UnderFlow :public exception
{
const char* what(void) const throw()
{
return "堆栈下溢";
}
};
//结点
class Node
{
public:
Node(int data = 0, Node* next = NULL) :m_data(data),
m_next(next){}
int m_data;//数据
Node* m_next;//后指针
};
Node* m_top; //栈顶指针
};
//基于堆栈的队列
class Queue
{
public:
//成员变量支持无参构造
//压入成员函数
void push(int data)
{
m_i.push(data);
}
//弹出
int pop(void)
{
if (m_o.empty())
{
if (m_i.empty())
throw underflow_error("队列下溢");
while (!m_i.empty())
m_o.push(m_i.pop());
}
return m_o.pop();
}
//判空
bool empty(void) const
{
return m_i.empty() && m_o.empty();
}
private:
Stack m_i;//输入栈
Stack m_o;//输出栈
};
int main()
{
try{
Queue queue;
for (int i = 0; i < 10; ++i)
queue.push(i);
cout << queue.pop() << endl;//0
cout << queue.pop() << endl;//1
cout << queue.pop() << endl;//2
cout << queue.pop() << endl;//3
queue.push(10);
queue.push(11);
while (!queue.empty())
cout << queue.pop() << endl;
}
catch (exception& ex)
{
cout << ex.what() << endl;
return -1;
}
}
#include<stdexcept>
using namespace std;
class Stack
{
public:
// 初始化为一个空堆栈
Stack() :m_top(NULL){}
~Stack()
{
for (Node* next; m_top; m_top = next)
{
next = m_top->m_next;
delete m_top;
}
}
void push(int data)
{
/*Node* node = new Node;
node->m_data = data;
node->m_next = m_top;
m_top = node;*/
m_top = new Node(data, m_top);
}
int pop(void)
{
if (empty())
throw UnderFlow();
int data = m_top->m_data;
Node* next = m_top->m_next;
delete m_top;
m_top = next;
return data;
}
bool empty() const
{
return !m_top;
}
private: //下溢异常
class UnderFlow :public exception
{
const char* what(void) const throw()
{
return "堆栈下溢";
}
};
//结点
class Node
{
public:
Node(int data = 0, Node* next = NULL) :m_data(data),
m_next(next){}
int m_data;//数据
Node* m_next;//后指针
};
Node* m_top; //栈顶指针
};
//基于堆栈的队列
class Queue
{
public:
//成员变量支持无参构造
//压入成员函数
void push(int data)
{
m_i.push(data);
}
//弹出
int pop(void)
{
if (m_o.empty())
{
if (m_i.empty())
throw underflow_error("队列下溢");
while (!m_i.empty())
m_o.push(m_i.pop());
}
return m_o.pop();
}
//判空
bool empty(void) const
{
return m_i.empty() && m_o.empty();
}
private:
Stack m_i;//输入栈
Stack m_o;//输出栈
};
int main()
{
try{
Queue queue;
for (int i = 0; i < 10; ++i)
queue.push(i);
cout << queue.pop() << endl;//0
cout << queue.pop() << endl;//1
cout << queue.pop() << endl;//2
cout << queue.pop() << endl;//3
queue.push(10);
queue.push(11);
while (!queue.empty())
cout << queue.pop() << endl;
}
catch (exception& ex)
{
cout << ex.what() << endl;
return -1;
}
}
0 0
- 利用栈实现队列的操作
- C++:利用两个队列实现栈的操作
- 利用两个栈实现队列的--->基本操作
- C++:利用两个栈实现队列操作
- 利用数组实现队列操作
- 用栈操作实现队列的操作
- 利用两个栈实现的队列
- 利用两个栈实现队列的功能
- 《算法》第一章——利用两个栈实现一个队列的push和pop操作
- 栈和队列的基本操作实现
- (java)两个队列实现栈的操作
- 用两个栈实现队列的操作
- 用两个栈实现队列的操作
- 用栈实现队列的进队出队操作
- 用队列实现一个栈的操作
- 利用操作队列和信号量来实现操作同步
- 【十四】队列的特别实现--利用双栈
- 利用两个栈实现队列的入队出队
- EM-Tree + Paragraph2vector 实现大规模文档聚类
- 把获取的维基百科主页词条及链接存入mysql数据库
- C基础之CONST
- EasyUI之选项卡Tabs
- Metasploit中配置beef
- 利用栈实现队列的操作
- java获取项目的绝对路径
- Search a 2D Matrix II
- 数据库事务隔离级别
- LeetCode 01
- python学习笔记
- Caffe入门(三) faster_rcnn matlab版demo
- 通过交互模式添加SSDB ip 白名单
- 读取mysql数据库数据