-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinkedlist_imple_of_stack.cpp
64 lines (63 loc) · 1.12 KB
/
linkedlist_imple_of_stack.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
#include<iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
Node* top=NULL;
void Push(int x){
Node* temp=new Node();
temp->data=x;
temp->next=top;
top=temp;
}
void Pop(){
Node* temp;
temp=top;
top=top->next;
delete(temp);
}
int Top(){
Node* temp=top;
while(temp->next!=NULL)
{
temp=temp->next;
}
std::cout<<"Currently the top most element of stack is"<<"\n";
std::cout<<temp->data<<"\n";
}
bool isEmpty()
{ Node* temp=top;
if(temp->next==NULL)
return true;
else
return false;
}
void print( ){
Node* temp=top;
std::cout<<"Currently stack is as follows"<<"\n";
while(temp!=NULL)
{
std::cout<<temp->data<<" ";
temp=temp->next;
}
std::cout<<"\n";
}
int main(){
int top;
bool isempty;
Push(2);
print();
Push(5);
print();
Push(10);
print();
Pop();
print();
Top();
isempty=isEmpty();
if(isempty==0)
std::cout<<"The stack is not empty"<<"\n";
else
std::cout<<"The stack is empty"<<"\n";
}