-
Notifications
You must be signed in to change notification settings - Fork 2
/
82.cpp
75 lines (64 loc) · 1.34 KB
/
82.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<iostream>
using namespace std;
const int SIZE = 10;
//creating a template class
//demonstrating the stack and its function with the help of templates.
template<class Type> //creating a class named STack
class Stack
{
Type stck[SIZE];
int index;
public:
//constructor of the class Stack()
Stack()
{
index=0;
}
//function declaration to push an item into the stack
void push(Type item);
//function declaration to pop an item out of the stack
Type pop();
};
//pushing an object
template<class Type>
void Stack<Type>::push(Type item)
{
if(index==SIZE)
{
cout<<"Stack is full"<<endl;
return;
}
stck[index]=item;
index++;
}
//popping an object
template<class Type>
Type Stack<Type>::pop()
{
if(index==0)
{
cout<<"Stack is empty"<<endl;
return 0;
}
index--;
return stck[index];
}
int main()
{
//creating object of stack class
Stack<char> s1;
s1.push('S');
s1.push('K');
s1.push('I');
s1.push('I');
s1.push('P');
cout<<"Top element of the stack s1 : "<<s1.pop()<<endl;
Stack<double> s2;
s2.push(1.23);
s2.push(2.45);
s2.push(6.54);
s2.push(1234.44);
s2.push(78.9);
cout<<"Top element of the stack s2 : "<<s2.pop()<<endl;
return 0;
}