//栈实现队列功能
#include<iostream>
#include<cstring>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
//结点
class MyData
{
public:
MyData() :data(0), next(NULL){}//默认构造函数
MyData(int value) :data(value), next(NULL){}//带参数构造函数
int data;
MyData *next;
};
//栈结构
class Mystack
{
public:
Mystack() :top(NULL){}//默认构造函数
void push(MyData data);
void pop(MyData *pdata);
bool isEmpty();
MyData *top;
};
//进栈
void Mystack::push(MyData data)
{
MyData *pData = NULL;
pData = new MyData(data.data);//每次获取新的节点地址
pData->next = top;//与原来栈顶结点相连
top = pData;//将新节点地址定为栈顶
}
//出栈
void Mystack::pop(MyData *data)
{
if (isEmpty())
{
printf("This stack is empty!");
return;
}
data->data = top->data;//给出栈结点赋值
MyData *p = top;
top = top->next;//栈顶结点指向下一个结点
delete p;
}
//判断是否为空栈
bool Mystack::isEmpty()
{
return(top == NULL);
}
//以栈替队
class MyQueue
{
//延伸(方法)
public:
void enQueue(MyData data);
void deQueue(MyData &dada);
bool isEmpty();
//内部固有(类似属性)
private:
Mystack stackA;
Mystack stackB;
};
//重载入栈(对属性进行操作)入栈对A
void MyQueue::enQueue(MyData data)
{
stackA.push(data);
/*Mystack() stacka;
cout << "请输入入队数值(输入0退出):"<<endl;
MyData *pData = NULL;
cin >> pData->data;
if (0 == pData->data)return;
top->next = pData;*/
}
bool MyQueue::isEmpty()
{
if (stackA.isEmpty() && stackB.isEmpty())
return true;
}
void MyQueue::deQueue(MyData &data)
{
if (!MyQueue::isEmpty())
{
MyData temp(0);
if (stackB.isEmpty())
{
while (!stackA.isEmpty())
{
stackA.pop(&temp);//弹出的元素弹入temp地址
stackB.push(temp);
}
}
stackB.pop(&data);
cout << data.data << endl;
}
else cout<<"木有数值在队里了"<<endl;
}
int main(int argc, int *argv[])
{
int x;
MyData data(0);//初始化一个结点
MyQueue stQueue;//定义一个队列
int com = 1;//是否退出程序
int choice;//选择
while (com)
{
cout << "please enter the number of command" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "请输入入队数值" << endl;
cin >> x;
stQueue.enQueue(MyData(x));//入队(压入一个结点)
break;
case 2:
//先定义需要插入的序号和值
cout <<"以下数值已出队"<< endl;
stQueue.deQueue(data);//出队(推出是一个数值)
break;
}
cout << "isEmpty:" << stQueue.isEmpty() << endl;
cout << "If you want to exit,enter '0'" << endl;
cin >> com;
}
return 0;
}
#include<iostream>
#include<cstring>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
//结点
class MyData
{
public:
MyData() :data(0), next(NULL){}//默认构造函数
MyData(int value) :data(value), next(NULL){}//带参数构造函数
int data;
MyData *next;
};
//栈结构
class Mystack
{
public:
Mystack() :top(NULL){}//默认构造函数
void push(MyData data);
void pop(MyData *pdata);
bool isEmpty();
MyData *top;
};
//进栈
void Mystack::push(MyData data)
{
MyData *pData = NULL;
pData = new MyData(data.data);//每次获取新的节点地址
pData->next = top;//与原来栈顶结点相连
top = pData;//将新节点地址定为栈顶
}
//出栈
void Mystack::pop(MyData *data)
{
if (isEmpty())
{
printf("This stack is empty!");
return;
}
data->data = top->data;//给出栈结点赋值
MyData *p = top;
top = top->next;//栈顶结点指向下一个结点
delete p;
}
//判断是否为空栈
bool Mystack::isEmpty()
{
return(top == NULL);
}
//以栈替队
class MyQueue
{
//延伸(方法)
public:
void enQueue(MyData data);
void deQueue(MyData &dada);
bool isEmpty();
//内部固有(类似属性)
private:
Mystack stackA;
Mystack stackB;
};
//重载入栈(对属性进行操作)入栈对A
void MyQueue::enQueue(MyData data)
{
stackA.push(data);
/*Mystack() stacka;
cout << "请输入入队数值(输入0退出):"<<endl;
MyData *pData = NULL;
cin >> pData->data;
if (0 == pData->data)return;
top->next = pData;*/
}
bool MyQueue::isEmpty()
{
if (stackA.isEmpty() && stackB.isEmpty())
return true;
}
void MyQueue::deQueue(MyData &data)
{
if (!MyQueue::isEmpty())
{
MyData temp(0);
if (stackB.isEmpty())
{
while (!stackA.isEmpty())
{
stackA.pop(&temp);//弹出的元素弹入temp地址
stackB.push(temp);
}
}
stackB.pop(&data);
cout << data.data << endl;
}
else cout<<"木有数值在队里了"<<endl;
}
int main(int argc, int *argv[])
{
int x;
MyData data(0);//初始化一个结点
MyQueue stQueue;//定义一个队列
int com = 1;//是否退出程序
int choice;//选择
while (com)
{
cout << "please enter the number of command" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "请输入入队数值" << endl;
cin >> x;
stQueue.enQueue(MyData(x));//入队(压入一个结点)
break;
case 2:
//先定义需要插入的序号和值
cout <<"以下数值已出队"<< endl;
stQueue.deQueue(data);//出队(推出是一个数值)
break;
}
cout << "isEmpty:" << stQueue.isEmpty() << endl;
cout << "If you want to exit,enter '0'" << endl;
cin >> com;
}
return 0;
}