// stack.cpp : Defines the entry point for the console application.
//
#include"iostream"
#include"conio.h"
#include"stdafx.h"
using namespace std;
template<class T> class Stack
{
private: int size; //Kích thước Stack
private: T *elements; //Mảng các phần tử kiểu T
private: int top; //Chỉ số của phần tử ngoài cùng
public: Stack(int n) //Hàm khởi tạo
{
size = n;
elements = new T[size];
top = -1;
}
public: ~Stack() //Hàm hủy
{
delete elements;
}
public: void Push(T x) //Đẩy một phần từ x vào Stack
{
if(top
== size - 1)cout << "\nStack da day"; else elements[++top] = x;
}
public: T Pop() //Đẩy phần tử top của Stack ra ngoài
{
if(top
== -1)cout << "\nStack rong"; else return elements[top--];
}
public: T Top() //Lấy giá trị của phần tử top
{
if(top
== -1)cout << "\nStack rong"; else return elements[top];
}
public: bool Full() //Kiểm tra Stack đầy
{
return top == size - 1;
}
public: bool Emty() //Kiểm tra Stack rỗng
{
return top == -1;
}
};
int main()
{
cout << "Stack kieu so nguyen:\n"; Stack<int> S(100);
S.Push(1);
S.Push(5);
S.Push(7);
S.Push(8);
while(!S.
Emty()) cout << S.
Pop() << endl
;
cout << "\n\nStack kieu ky tu:\n"; Stack<char> S2(50);
S2.Push('c');
S2.Push('A');
S2.Push('Z');
while(!S2.
Emty()) cout << S2.
Pop() << endl
;
getch();
}