实现菜单功能选择堆栈程序或者队列程序,代码比较乱,不喜勿喷.
/*
*Copyright: Copyright (c) 2018
*Created on 2018-9-20
*Author: Kwok Cheung Yuk
*Version 1.0
*Title: 链式堆栈、循环队列菜单
*/
#include<iostream>
#include <stdlib.h>
using namespace std;
//堆栈的操作
struct Node{
char date;
Node *next;
Node(){
next = NULL;
}
Node(char item, Node *add_on = NULL){
this->date = item;
this->next = add_on;
}
};
class LinkedStack{
protected:
Node *top;
int count;
public:
LinkedStack(){
this->top = NULL;
this->count = 0;
}
~LinkedStack(){
this->top = NULL;
}
//判断是否为空
bool is_empty(){
if(top == NULL){
return 1;
}
else{
return 0;
}
}
//获取顶部元素
void getTop()const{
if(top != NULL){
cout << "栈顶元素是:" << top->date << endl;
}
else{
cout << "这个链式堆栈是空的!" << endl;
}
}
//实现push操作
void push(const char &item){
Node *newTop = new Node(item, top);
if(newTop == NULL){
cout << "该链式堆栈已满,不能进行入栈操作!" << endl;
}
else{
this->top = newTop;
cout <<"元素:" << top->date << " 已经被放入" << endl;
this->count ++;
}
}
//实现pop操作
void pop(){
if(this->top == NULL){
cout << "该链式堆栈为空栈,不能进行出栈操作!" << endl;
}
else{
Node *oldTop = this->top;
this->top = oldTop->next;
cout <<"元素:" << oldTop->date << " 已经被取出." << endl;
delete oldTop;
this->count --;
}
}
//打印栈堆的情况
void printStack()const{
if(top == NULL){
cout << "这个链式堆栈是空的!" << endl;
}
else{
cout << "链式堆栈目前有 " << count << "个元素." << endl;
cout << "从栈顶到栈底分别是:" << endl;
LinkedStack temp = *this;
for(int i = 0; i < temp.count; i ++){
cout << temp.top->date << endl;
temp.top = temp.top->next;
}
}
}
};
int Stack(){
LinkedStack stack;
char c;
//cin >> c;
do{
cout << endl << endl;
cout << "堆栈程序菜单:" << endl;
cout << "1.进行入栈操作" << endl;
cout << "2.进行出栈操作" << endl;
cout << "3.获取栈顶元素" << endl;
cout << "4.判定当前堆栈是否为空" << endl;
cout << "5.输出当前堆栈的情况" << endl;
cout << "0.返回上层菜单" << endl;
cout << "请输入对应编号:";
cin >> c;
switch(c){
case '0':
// cout << "感谢使用!" <<endl;
return 0;
break;
case '1':
cout << endl << endl;
char date;
cout << "请输入入栈元素:" << endl;
cin >> date;
stack.push(date);
break;
case '2':
cout << endl << endl;
stack.pop();
break;
case '3':
cout << endl << endl;
stack.getTop();
break;
case '4':
cout << endl << endl;
if(stack.is_empty() == 1){
cout << "这个链式堆栈是空的!" << endl;
}
else{
cout << "这个链式堆栈不是空的!" << endl;
}
break;
case '5':
cout << endl << endl;
stack.printStack();
break;
default:
cout << endl << endl;
cout << "输入码无效,请重新输入!"<<endl;
break;
}
}while( c != '\n');
}
//队列的操作
class CircularQueue{
protected:
char array[10];
int front;
int rear;
int count;
public:
CircularQueue(){
this->front = 0;
this->rear = 0;
this->count = 0;
}
~CircularQueue(){
delete []array;
}
void serve(){
if(this->count == 0){
cout << "该循环队列为空队列,不能进行出队操作!" << endl;
}
else{
cout << "出队的元素是:" << this->array[this->front % 10] << endl;
this->front ++;
count --;
}
}
bool is_empty()const{
if(this->count == 0){
return 1;
}
else{
return 0;
}
}
void append(const char &item){
if(this->count >= 10){
cout << "该队列已满,不能进行入队操作!" << endl;
}
else{
cout <<"元素:" << item << " 已入队" << endl;
this->array[this->rear % 10] = item;
this->count ++;
this->rear ++;
}
}
void getFront()const{
if(this->count == 0){
cout << "该循环队列为空队列,不能进行获取队首元素的操作!" << endl;
}
else{
cout << "队首元素是:" << this->array[this->front % 10] << endl;
}
}
void printQueue()const{
if(this->count == 0){
cout << "这个队列是空的!" << endl;
}
else{
cout << "队列目前有 " << this->count << "个元素." << endl;
cout << "从队首到队尾分别是:" << endl;
for(int i = 0; i < this->count; i ++){
cout << array[(this->front + i ) % 10] << endl;
}
}
}
};
int Queue(){
CircularQueue queue;
char c;
//cin >> c;
do{
cout << endl << endl;
cout << "队列程序菜单:" << endl;
cout << "1.进行入队操作" << endl;
cout << "2.进行出队操作" << endl;
cout << "3.获取队首元素" << endl;
cout << "4.判定当前队列是否为空" << endl;
cout << "5.输出当前队列的情况" << endl;
cout << "0.返回上层菜单" << endl;
cout << "请输入对应编号:";
cin >> c;
switch(c){
case '0':
// cout << "感谢使用!" <<endl;
return 0;
break;
case '1':
cout << endl << endl;
char date;
cout << "请输入入队元素:" << endl;
cin >> date;
queue.append(date);
break;
case '2':
cout << endl << endl;
queue.serve();
break;
case '3':
cout << endl << endl;
queue.getFront();
break;
case '4':
cout << endl << endl;
if(queue.is_empty() == 1){
cout << "这个队列是空的!" << endl;
}
else{
cout << "这个队列不是空的!" << endl;
}
break;
case '5':
cout << endl << endl;
queue.printQueue();
break;
default:
cout << endl << endl;
cout << "输入码无效,请重新输入!"<<endl;
break;
}
}while( c != '\n');
}
int main(){
char a;
do{
cout << endl << endl;
cout << "主程序菜单:" << endl;
cout << "1.执行堆栈程序" << endl;
cout << "2.执行队列程序" << endl;
cout << "0.退出程序" << endl;
cout << "请输入对应编号:";
cin >> a;
switch(a){
case '0':
cout << "感谢使用!" <<endl;
return 0;
break;
case '1':
cout << endl << "堆栈程序正在启动......" << endl;
Stack();
break;
case '2':
cout << endl << "队列程序正在启动......" << endl;
Queue();
break;
break;
default:
cout << endl << endl;
cout << "输入码无效,请重新输入!"<<endl;
break;
}
}while( a != '\n');
}