实验 2
学院:数据科学与计算机学院
【题目要求】
1、实现堆栈在链接存储结构上的入栈、出栈运算;
2、实现队列在循环顺序存储结构上的入队、出队运算;
3、编写主函数,以字符选单形式,使得可以建立堆栈、入栈、出栈以及建立队列,入队和出队选择。
【数据结构与算法】
一、堆栈:
1、将其封装成一个类, 类的方法有push()和pop(), 类的成员有一个指向头结点的指针head;
2、每个节点为一个结构体,包括节点数据和指向下一个节点的指针。
3、出栈时如果为空,返回错误信息,否则,弹出栈顶元素。
二、队列
1、将其封装成一个类, 类的方法有push()和pop(), 类的成员有一个计数器count,头和尾的标记head和tail。
2、入栈时判断队列是否已满(计数器为最大值),出站时判断队列是否为空(计数器为0)
【测试数据、结果及分析】
一、数据:以字符‘C’‘O’‘M’‘P’‘U’‘T’‘E’‘R’为数据调试
代码:
main函数
#include"Stack.hpp"
#include"Queue.hpp"
#include<iostream>
#include<cstdlib>
using std::cout;
using std::cin;
using std::endl;
int main() {
while(1) {
/**********************main menu*********************/
cout << endl;
cout << "-----------------------welcome----------------------" << endl;
cout << "please enter the operation: " << endl;
cout << "s/S -- creat a char stack" << endl;
cout << "q/Q -- creat a char queue" << endl;
cout << "e/E -- exit" << endl;
cout << "$ ";
char op;
cin >> op;
if (op == 's' || op == 'S') {
Stack s;
while(1) {
/**********************stack menu*********************/
cout << endl;
cout << "------------------Stack------------------" << endl;
cout << "please enter the operation: " << endl;
cout << "1 -- push a char element to the Stack" << endl;
cout << "2 -- pop a char element from the Stack" << endl;
cout << "0 -- exit" << endl;
cout << "Stack: $ ";
/**********************stack operation*********************/
char o;
cin >> o;
if (o == '1') { // push
cout << endl << "please input the element : ";
char c;
cin >> c;
s.push(c);
cout <<endl << "push successfully";
} else if (o == '2') {
if (s.pop()) { // pop
cout << endl << "pop successfully";
} else {
cout << endl << "pop failed : empty stack";
}
} else if (o == '0') { // exit
break;
} else { // wrong input
cout << endl << "wrong input! ";
continue;
}
}
} else if (op == 'q' || op == 'Q') {
Queue q;
while(1) {
/**********************queue menu*********************/
cout << endl;
cout << "------------------Queue------------------" << endl;
cout << "please enter the operation: " << endl;
cout << "1 -- push a char element to the Queue" << endl;
cout << "2 -- pop a char element from the Queue" << endl;
cout << "0 -- exit" << endl;
cout << "Queue: $ ";
/**********************queue operation*********************/
char o;
cin >> o;
if (o == '1') { // push
cout << endl << "please input the element : ";
char c;
cin >> c;
if (q.push(c)) {
cout << endl << "push successfully";
} else {
cout << endl << "push failed : full queue";
}
} else if (o == '2') { // pop
if (q.pop()) {
cout << endl << "pop successfully";
} else {
cout << endl << "pop failed : empty queue";
}
} else if (o == '0') { // exit
break;
} else { // wrong input
cout << endl << "wrong input! ";
continue;
}
}
} else if (op == 'e' || op == 'E') {
/**********************exit operation*********************/
cout << endl << "good bye~" << endl;
break;
} else { // wrong input
cout << endl << "wrong input! ";
continue;
}
}
system("pause");
return 0;
}
Stack.hpp
#ifndef __STACK_H__
#define __STACK_H__
#define error_sign '*'
#include<iostream>
struct node {
char data;
node* next;
node(char c, node* n = NULL) {
data = c;
next = n;
}
};
class Stack {
public:
Stack() { top_node = NULL; }
~Stack() {
while (top_node != NULL) pop();
}
void push(char c) {
node* add = new node(c, top_node);
top_node = add;
}
bool pop() {
if (top_node == NULL) return false;
node* temp = top_node;
top_node = top_node->next;
delete temp;
return true;
}
private:
node* top_node;
};
#endif
Queue.hpp
#ifndef __QUEUE_H__
#define __QUEUE_H__
#define max_size 10000
class Queue {
public:
Queue() {
head = tail = count = 0;
}
~Queue() {}
bool push(char c) {
if (count == max_size) return false;
q[tail] = c;
tail = (tail + 1) % max_size;
count++;
return true;
}
bool pop() {
if (count == 0) return false;
head = (head + 1) % max_size;
count--;
return true;
}
private:
char q[max_size];
int count;
int head;
int tail;
};
#endif