-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathlinked2stack.cpp
More file actions
62 lines (54 loc) · 1.07 KB
/
linked2stack.cpp
File metadata and controls
62 lines (54 loc) · 1.07 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
#include<iostream>
using namespace std;
struct Node{
int data;
Node *link;
};
int size = 0;
Node *top = NULL;
bool isempty(){
if(top == NULL) return true;
else return false;
}
void push(int value){
// Initializing a new node
Node *ptr = new Node();
// Giving it a value
ptr->data = value;
// We then are making it point to the first element of the stack currently
ptr->link = top;
// Then we are making the current node the top
top = ptr;
size = size + 1;
}
void pop(){
if (isempty()) cout<<"Stack is empty"<<endl;
else{
Node *ptr = top;
top = top->link;
delete(ptr);
size = size - 1;
}
}
void showTop(){
if (isempty()) cout<<"Stack is empty"<<endl;
else cout<<"Element at the top is "<<top->data<<endl;
}
int main(){
cout<<size<<endl;
push(5);
showTop();
cout<<size<<endl;
push(10);
push(5);
cout<<size<<endl;
push(10);
showTop();
pop();
showTop();
cout<<size<<endl;
pop();
showTop();
cout<<size<<endl;
return 0;
}