代码拉取完成,页面将自动刷新
#ifndef _STACK_HPP_
#define _STACK_HPP_
#include <iostream>
#include <string>
using namespace std;
template<class ElementType>
class Stack {
typedef struct Node{
ElementType data;
Node *next;
} Node;
Node *top;
int num;
public:
Stack<ElementType>();
~Stack();
void push( ElementType );
ElementType pop( void );
ElementType topElement();
void show();
bool empty();
};
template<class ElementType>
Stack<ElementType>::Stack(): num(0) {
top = new Node;
}
template<class ElementType>
Stack<ElementType>::~Stack(){
}
template<class ElementType>
void Stack<ElementType>::push( ElementType str ){
if( num == 0 ){
top->data = str;
}else{
// cout << "push" << str << endl;
Node *oldTop = new Node;
oldTop->data = top->data;
oldTop->next = top->next;
top ->next = oldTop;
top -> data = str;
}
num ++;
}
template<class ElementType>
ElementType Stack<ElementType>::pop( void ){
if( num == 1 ){ num = 0; return top->data; }
if( !num ) cerr << "stack empty" << endl;
ElementType ans = top->data;
Node *t = top;
top = top->next;
delete t;
num --;
// cout << "pop" << ans << endl;
return ans;
}
template<class ElementType>
ElementType Stack<ElementType>::topElement(){
if(empty()) return "null";
return top->data;
}
template<class ElementType>
void Stack<ElementType>::show(){
cout << "stack:";
Node *iterator = top;
for( int i = 0; i < num; ++i ){
cout << iterator->data << " -> ";
iterator = iterator->next;
}
cout << endl;
}
template<class ElementType>
bool Stack<ElementType>::empty(){
if(num) return false;
return true;
}
#endif
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。