-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstack_and_queue_2.cpp
More file actions
93 lines (77 loc) · 1.47 KB
/
stack_and_queue_2.cpp
File metadata and controls
93 lines (77 loc) · 1.47 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
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
Node(int data){
this->data=data;
next=NULL;
}
};
//stack using linked list
class Stack
{
Node* head;
int Size; //for calculating size of stack
public:
//constructor
Stack()
{
head=NULL;
Size=0;
}
//getSize function
int getSize()
{
return Size;
}
bool isEmpty()
{
if(head==NULL)
return true;
else
return false;
}
void push(int element)
{
Node* newNode = new Node(element);
newNode->next = head;
head=newNode;
Size++;
}
int pop()
{
if(isEmpty())
return 0;
int ans = head->data;
Node* temp = head;
head=head->next;
delete temp;
Size--;
return ans;
}
int top()
{
if(isEmpty())
return 0;
return head->data;
}
};
int main()
{
cout<<"Program started.."<<endl;
Stack s;
s.push(12);
s.push(13);
s.push(14);
s.push(15);
s.push(16);
cout<<"top of stack is: "<<s.top()<<endl;
cout<<"is stack empty: "<<s.isEmpty()<<endl;
cout<<"element popped: "<<s.pop()<<endl;
cout<<"size of stack is: "<<s.getSize()<<endl;
cout<<"top of stack is: "<<s.top()<<endl;
return 0;
}