-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path6_StackUsingLinkedList.cpp
More file actions
91 lines (76 loc) · 1.67 KB
/
6_StackUsingLinkedList.cpp
File metadata and controls
91 lines (76 loc) · 1.67 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
#include <bits/stdc++.h>
using namespace std;
class Stack{
private:
class Node{
public:
int data;
Node *next;
};
Node *head;
public:
Stack(int data)
{
Node *node = new Node();
node->data = data;
node->next = NULL;
this->head = node;
}
void push(int x)
{
Node *node = new Node();
node->data = x;
node->next = this->head;
this->head = node;
}
int top()
{
if(this->head == NULL){
cout << "Stack Empty" << endl;
return INT_MIN;
}
return (this->head)->data;
}
void pop()
{
if(this->head == NULL){
cout << "Error! Stack Already Empty" << endl;
return;
}
Node *temp = (this->head);
this->head = temp->next;
delete temp;
}
void show()
{
if(this->head == NULL)
{
cout << "Stack Empty!!!" << endl;
return;
}
Node *trav = this->head;
while(trav != NULL){
cout << trav->data << " ";
trav = trav -> next;
}
cout << endl;
}
bool isEmpty()
{
return head==NULL;
}
};
int main()
{
Stack s = Stack(4);
s.show();
s.push(7);
cout << s.top() << endl;
s.show();
s.pop();
s.show();
s.pop();
s.show();
cout << s.isEmpty() << endl;
return 0;
}