-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack implementation.cpp
More file actions
108 lines (88 loc) · 1.8 KB
/
stack implementation.cpp
File metadata and controls
108 lines (88 loc) · 1.8 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
99
100
101
102
103
104
105
106
107
108
#include<iostream>
using namespace std;
int stack_size = 30;
int count = 1;
struct node{
int num;
node *next, *prev;
};
node *root, *tail;
void push(int num){
if(count <= stack_size){
if(root == NULL){
root = new node();
root->num = num;
root->next = NULL;
tail = root;
count++;
}
else{
node *newNode = new node();
newNode->num = num; // store value in new node
newNode->next = NULL;
newNode->prev = tail; //point to the previous node
tail->next = newNode; //point to the next node
tail = newNode; // point to the previous node
count++;
}
}
else
cout << "stack full\n";
}
void peek(){
if(root == NULL)
cout << "stack underflow\n";
else
cout << tail->num <<"\n";
}
void pop(){
if(root == NULL)
cout << "stack underflow\n";
else{
node *temp = tail;
tail = tail->prev;
tail->next = NULL;
delete(temp);
count--;
}
}
bool isFull(){
if(count > 30)
return true;
else
return false;
}
bool isEmpty(){
if(root == NULL)
return true;
else
return false;
}
int main(){
if(isEmpty())
cout << "stack empty\n";
else
cout << "stack not empty\n";
if(isFull())
cout << "stack full\n";
else
cout << "stack not full\n";
for(int i=1; i<=10; i++){
push(i);
}
if(isEmpty())
cout << "empty\n";
else
cout << "not empty\n";
peek();
for(int i=11; i<=30; i++){
push(i);
}
if(isFull())
cout << "full\n";
else
cout << "not full\n";
peek();
pop();
peek();
}