-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathStacksusinglinkedlist.cpp
80 lines (78 loc) · 1.35 KB
/
Stacksusinglinkedlist.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
#include<bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int d){
this->data=d;
this->next=NULL;
}
};
class Stack{
Node* head;
int capacity;
int currSize;
public:
Stack(int c){
this->capacity=c;
this->currSize=0;
head=NULL;
}
int isEmpty(){
return this->head==NULL;
}
int isFull(){
return this->currSize==this->capacity-1;
}
void push(int d){
if(this->currSize==this->capacity){
cout<<"Overflow"<<endl;
return;
}
Node* newNode=new Node(d);
newNode->next=this->head;
this->head=newNode;
this->currSize++;
}
void pop(){
if(this->head==NULL){
cout<<"Underflow"<<endl;
return;
}
head=head->next;
}
void getTop(){
if(this->head==NULL){
cout<<"Empty"<<endl;
return;
}
cout<<this->head->data<<endl;
}
void display(){
Node* temp=this->head;
if(head!=NULL){
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
}
};
int main(){
Stack st(5);
st.push(5);
st.push(4);
st.push(3);
st.push(2);
st.getTop();
st.push(6);
st.push(5);
st.getTop();
st.display();
st.pop();
st.display();
st.getTop();
return 0;
}