-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStack.h
108 lines (93 loc) · 1.31 KB
/
Stack.h
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
102
103
104
105
106
107
108
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Node
{
private:
string data;
Node* next;
public:
Node(string x = "", Node* ptr = NULL);
string get_data();
Node* get_Next();
void set_data(string y);
void set_next(Node* ptr);
};
class Stack
{
public:
Node* top;
public:
Stack();
~Stack();
Node* get_top();
void push(string x);
string pop();
bool empty();
string Top();
};
Node::Node(string x, Node* ptr)
{
data = x;
next = ptr;
}
string Node::get_data()
{
return data;
}
Node* Node::get_Next()
{
return next;
}
void Node::set_data(string x)
{
data = x;
}
void Node::set_next(Node* ptr)
{
next = ptr;
}
Stack::Stack()
{
top = NULL;
}
Node* Stack::get_top()
{
return top;
}
void Stack::push(string x)
{
Node* topptr = new Node(x, top);
top = topptr;
//cout << x << " pushed!\n";
}
string Stack::pop()
{
if (empty())
{
cout << "Stack is Empty!!" << endl;
}
else
{
Node* temp = get_top();
top = top->get_Next();
string z = temp->get_data();
//cout << z << " is popped!\n";
delete temp;
return z;
}
return "";
}
bool Stack::empty()
{
return (get_top() == NULL);
}
string Stack::Top()
{
return top->get_data();
}
Stack::~Stack()
{
delete top;
}