-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_LL.cpp
More file actions
101 lines (86 loc) · 1.56 KB
/
stack_LL.cpp
File metadata and controls
101 lines (86 loc) · 1.56 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<iostream>
using namespace std;
template<typename T>
class Node{
public:
T data;
Node<T>*nextNode;
Node<T>*prevNode;
Node(T data){
this->data=data;
nextNode=NULL;
prevNode==NULL;
}
Node(){
nextNode=NULL;
prevNode==NULL;
}
};
template<typename T>
class Stack{
private:
Node<T>*head;
int count;
public:
Stack(){
head=NULL;
count=0;
}
void push(T element){
Node<T>*newNode=new Node<T>(element);
newNode->prevNode=head;
head=newNode;
count++;
}
int size(){
return count;
}
bool isEmpty(){
return count==0;
}
T pop(){
if(count==0){
cout<<"Empty stack"<<endl;
return 0;
}
else{
Node<T>*temp=head;
T data=temp->data;
head=head->prevNode;
delete temp;
count--;
return data;
}
}
T top(){
if(count==0){
cout<<"Empty stack"<<endl;
return 0;
}
return head->data;
}
};
int main(){
Stack <int>s1;
s1.push(2);
s1.push(4);
s1.push(6);
s1.push(8);
cout<<s1.top()<<endl;
cout<<s1.pop()<<endl;
cout<<s1.size()<<endl;
cout<<boolalpha<<s1.isEmpty()<<endl;
cout<<s1.pop()<<endl;
cout<<s1.pop()<<endl;
cout<<s1.pop()<<endl;
cout<<s1.pop()<<endl;
s1.push(8);
s1.push(9);
s1.push(10);
s1.push(11);
s1.push(12);
s1.push(13);
s1.push(14);
cout<<s1.top()<<endl;
cout<<s1.size()<<endl;
}