Descrption
Requirement:
In this assignment, you are required to finish the Stack with Template. Please modify class Stack’s declaration and definition so as to finish the validation from main.cpp
Attention: please use template you have learned in the class to finish this assignment and DON NOT add and modify any memeber function and member variable.
Warning:
Do not use Stack in STL.
Do not use array of Node to finish the definition of some function.
Extra:
The Stack’s declaration with element type “int” is below:
class Stack {
public:
Stack() ;
Stack(const Stack &stack);
~Stack();
bool empty();
size_t size() const;
int top() const;
void push(int element);
void pop();
void swap(Stack& stack);
void reverse(); /*reverse the elements' order in the stack*/
void clear();
private:
struct Node {
int element;
Node* next;
Node(int ele, Node *n = NULL) {
element = ele;
next = n;
}
};
Node *top_node;
size_t node_num;
};
出题人:黎洋
Provided Codes
main.cpp
#include <iostream>
#include "Stack.h"
#include <string>
#include <sstream>
#include <exception>
using namespace std;
class STLForbidden : public exception {
virtual const char *what() const throw() {
return "Please do not use std::sort or std::list or std::vector .....";
}
};
class Job {
public:
explicit Job(int pri = 0) {
id = number++;
priority = pri;
}
string toString() {
stringstream ss;
ss << "[" << id << ":" << priority << "]";
return ss.str();
}
private:
static int number;
int id;
int priority;
};
int Job::number = 0;
template<typename T>
void print(Stack<T> stack) {
while (!stack.empty()) {
cout << stack.top() << " ";
stack.pop();
}
cout << endl;
}
int main() {
// ignore it
#if defined(_GLIBCXX_ALGORITHM) || defined(_GLIBCXX_LIST) || \
defined(_GLIBCXX_VECTOR) || defined(_GLIBCXX_DEQUE) || \
defined(_GLIBCXX_STACK)
// throw AlgorithmnForbidden();
throw STLForbidden();
#endif
// testing -> integer..
Stack<int> stk;
int m, n;
cin >> m >> n;
for (int i = 0; i < m; i++) stk.push(i + 0.01);
for (int i = 0; i < n; i++) stk.pop();
Stack<int> stk0(stk);
if (!stk.empty()) cout << stk.top() << endl;
cout << "The size is: " << stk.size() << endl;
if (stk.empty()) cout << "The stack is empty!" << endl;
else cout << "The stack is NOT empty!" << endl;
// testing -> Stack(const &stack);
if (!stk0.empty()) cout << "The top of Stack is: " << stk0.top() << endl;
cout << "The size is: " << stk0.size() << endl;
print(stk0);
if (!stk0.empty()) cout << "The top of Stack is: " << stk0.top() << endl;
cout << "The size is: " << stk0.size() << endl;
// testing -> reverse()
stk0.reverse();
cout << "Result of reversing is: ";
print(stk0);
// testing -> double..
Stack<double> stk1;
cin >> m >> n;
for (int i = 0; i < m; i++) stk1.push(i + 0.01);
for (int i = 0; i < n; i++) stk1.pop();
if (!stk1.empty()) cout << stk1.top() << endl;
cout << "The size is: " << stk1.size() << endl;
stk1.clear();
cout << "The size is: " << stk1.size() << endl;
if (stk1.empty()) cout << "The stack is empty!" << endl;
else cout << "The stack is NOT empty!" << endl;
// testing -> user defined class..
cin >> m >> n;
Stack<Job> stk2;
for (int i = 0; i < m; i++) stk2.push(Job(i));
for (int i = 0; i < n; i++) stk2.pop();
if (!stk2.empty()) cout << stk2.top().toString() << endl;
cout << "The size is: " << stk2.size() << endl;
if (stk2.empty()) cout << "The stack is empty!" << endl;
else cout << "The stack is NOT empty!" << endl;
// testing -> swap function..
Stack<int> stk3, stk4;
for (int i = 0; i < m; i++) stk3.push(i);
for (int i = 0; i < m; i++) stk4.push(m - i);
cout << "Before swap...." << endl;
print(stk3);
print(stk4);
stk3.swap(stk4);
cout << "After swap...." << endl;
print(stk3);
print(stk4);
}
Submission
Stack.h
#ifndef STACK_H
#define STACK_H
#include <cstddef>
#include <cstdlib>
template <typename T>
class Stack {
public:
Stack();
Stack(const Stack &stack);
~Stack();
bool empty();
size_t size() const;
T top() const;
void push(T element);
void pop();
void swap(Stack& stack);
void reverse(); /*reverse the elements' order in the stack*/
void clear();
private:
struct Node {
T element;
Node* next;
Node(T ele, Node *n = NULL) {
element = ele;
next = n;
}
};
Node *top_node;
size_t node_num;
};
template <typename T>
Stack<T>::Stack(){
top_node=NULL;
node_num=0;
}
template <typename T>
Stack<T>::Stack(const Stack<T> &stack){
top_node=NULL;
node_num=0;
Node *tem=stack.top_node;
Node *last=top_node;
while(tem!=NULL){
if(top_node==NULL)
last=top_node=new Node(tem->element);
else
last=last->next=new Node(tem->element);
++node_num;
tem=tem->next;
}
}
template <typename T>
Stack<T>::~Stack(){
clear();
}
template <typename T>
bool Stack<T>::empty(){
if(node_num==0)
return true;
return false;
}
template <typename T>
size_t Stack<T>::size() const{
return node_num;
}
template <typename T>
T Stack<T>::top() const{
if(top_node!=NULL)
return top_node->element;
}
template <typename T>
void Stack<T>::push(T element){
Node* tem=new Node(element);
tem->next=top_node;
top_node=tem;
++node_num;
}
template <typename T>
void Stack<T>::pop(){
if(!empty()){
Node *tem=top_node;
top_node=top_node->next;
delete tem;
--node_num;
}
}
template <typename T>
void Stack<T>::swap(Stack<T>& stack){
Node *tem=top_node;
int t=node_num;
top_node=stack.top_node;
node_num=stack.node_num;
stack.top_node=tem;
stack.node_num=t;
}
template <typename T>
void Stack<T>::reverse(){
Stack<T> stack;
while(!empty()){
stack.push(top());
pop();
}
Node *tem=stack.top_node;
Node *last=top_node;
while(tem!=NULL){
if(top_node==NULL)
last=top_node=new Node(tem->element);
else
last=last->next=new Node(tem->element);
++node_num;
Node *t=tem;
tem=tem->next;
}
}
template <typename T>
void Stack<T>::clear(){
while(!empty())
pop();
}
#endif