-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_with_likedList.cpp
110 lines (79 loc) · 1.39 KB
/
stack_with_likedList.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
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
109
110
#include <iostream>
using namespace std;
class node {
public:
int data ;
node* next ;
node() {
data = 0;
next = NULL;
}
node(int d) {
data = d ;
next = NULL ;
}
};
class stack {
public:
node* head ;
stack() {
head = NULL ;
cout << "stack created successfully" << endl;
}
stack(node* n) {
head = n ;
cout << "stack created successfully" << endl;
}
void push(node* n);
void pop();
bool isEmpty();
void display();
int peek();
int count();
};
bool stack:: isEmpty() {
if (head == NULL) return true ;
return false ;
}
void stack:: push(node* n) {
n->next = head ;
head = n ;
cout << "push successful" << endl;
}
void stack:: pop() {
if (isEmpty()) cout << "stack is Empty" << endl;
else {
head = head->next ;
cout << "pop successful" << endl;
}
}
void stack::display() {
node * ptr = head ;
while (ptr != NULL) {
cout << ptr->data << " ";
ptr = ptr->next ;
}
cout << endl;
}
int stack:: peek() {
return head->data ;
}
int stack:: count() {
int c = 0 ;
node* ptr = head ;
while (head != NULL) {c++; head = head->next;}
return c ;
}
int main() {
node n1(1);
node n2(2) , n3(3) , n4(4) ;
stack s(&n1);
s.push(&n4);
s.push(&n2);
s.push(&n3);
s.display();
s.pop();
s.display();
cout << s.peek() << endl << s.count();
return 0;
}