-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack implementation 3.cpp
More file actions
98 lines (77 loc) · 1.49 KB
/
stack implementation 3.cpp
File metadata and controls
98 lines (77 loc) · 1.49 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
#include<iostream>
using namespace std;
struct node{
int num;
node *next;
};
node *head;
void push(int num){
node *newNode = new node();
if(!newNode)
cout << "stack overflow\n";
else{
if(head == NULL){
head = new node();
head->num = num;
head->next = NULL;
}
else{
newNode->num = num;
newNode->next = head;
head = newNode;
}
}
}
void peek(){
if(head == NULL)
cout << "stack underflow\n";
else
cout << head->num <<"\n";
}
void pop(){
if(head == NULL)
cout << "stack underflow\n";
else{
node *temp = head;
head = head->next;
delete(temp);
}
}
bool isEmpty(){
if(head == NULL)
return true;
else
return false;
}
void print(){
node *currentNode = head;
if(head == NULL)
cout << "stack empty\n";
else{
while(currentNode != NULL){
cout << currentNode->num << " ";
currentNode = currentNode->next;
}
cout << "\n";
}
}
int main(){
peek();
pop();
if(isEmpty())
cout << "stack empty\n";
else
cout << "stack not empty\n";
for(int i=1; i<=10; i++){
push(i);
}
print();
peek();
pop();
peek();
if(isEmpty())
cout << "stack empty\n";
else
cout << "stack not empty\n";
// while()
}