forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack_using_LinkedList.cpp
133 lines (121 loc) · 3.02 KB
/
Stack_using_LinkedList.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
Implement the basic functionality of stack using Linked List Data structure
@author: Rishikeshrajrxl
@created: 17/08/2020
*/
#include <iostream>
using namespace std;
struct node
{ //declaration of node
int data;
struct node*next;
}*head=NULL,*temp,*ptr;
void Push(); //Function declaration
void Peek();
void Pop();
void Is_Empty();
void Display();
void count();
int ele;
int main() //Main() starts
{
int ch;
cout<< "\n\n\t Implement the basic functionality of stack"<<endl; //Displaying the Features in a tabular manner
cout<<"\t **************************************************"<<endl;
do{
cout<<"\t 1) Push\t \t 2) Pop "<<endl;
cout<<"\t 3) Peek\t \t 4) Is_Empty"<<endl;
cout<<"\t 5) Display\t \t 6) count"<<endl;
cout<<"\t 7) Exit"<<endl;
cout<<"\t ***************************************************\nENTER YOUR CHOICE : ";
cin >>ch;
switch(ch)
{
case 1:
Push();
break;
case 2:
Pop();
break;
case 3:
Peek();
break;
case 4:
Is_Empty();
break;
case 5:
Display();
break;
case 6:
count();
break;
case 7:
exit(0);
break;
default:
cout<<"Invalid Input. Please enter the Number between 1 to 7 \n"<<endl;
}
}while(ch!=0);
return 0;
} // Main() Ends
//--------------------------------------------------------------------------
void Push() //Insertion at the top
{
ptr=(struct node*)malloc(sizeof(struct node));
cout<<"ENTER THE ELEMENT : ";
cin>>ele;
ptr->data=ele;
ptr->next=head;
head=ptr;
cout<<"Pushed Successfully"<<endl;
}
//---------------------------------------------------------------------------
void Pop() //deletion from the top
{
if(head==NULL){
cout<<"Stack is Empty"<<endl;
}
else{
temp=head;
head=head->next;
cout<<"Poped Successfully"<<endl;
free(temp);
}
}
//---------------------------------------------------------------------------
void Peek() //returns the value of the top
{
cout<<"Peek Value is : "<< head->data<<endl;
}
//-----------------------------------------------------------------------------
void Is_Empty() //returns Yes if Empty, else No
{
if(head==NULL)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
//------------------------------------------------------------------------------
void Display() //returns all the value inside the Stack
{
if(head==NULL){
cout<<"Stack is Empty "<<endl;
} else{
temp=head;
cout<<"Data In Stack : ";
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}cout<<""<<endl;
}
}
//-------------------------------------------------------------------------------
void count() //returns total no of nodes in stack
{
int count=0;
temp=head;
while(temp!=NULL){
count+=1;
temp=temp->next;
} cout<<"Total No is : "<<count<<endl;
}