forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.java
149 lines (147 loc) · 3.89 KB
/
Stack.java
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.util.*;
public class Stack
{
static Node top;
class Node
{
public int data;
public Node next;
Node(int data)
{
this.data=data;
}
}
void push(int data)
{
Node ptr = new Node(data);
if(top==null)
{
top=ptr;
ptr.next=null;
System.out.println(data+" is successfully added to the stack");
}
else
{
Node ctr=top;
ptr.next=ctr;
top = ptr;
System.out.println(data+" is successfully added to the stack");
}
}
boolean isEmpty()
{
if(top==null)
return true;
return false;
}
void peek()
{ if(!isEmpty())
System.out.println(top.data);
else
System.out.println("Stack is empty");
}
void count()
{
int count = 0;
Node ptr = top;
while(ptr!=null)
{
count++;
ptr = ptr.next;
}
System.out.println("The stack has "+count+" elements");
}
void pop()
{
Node ctr=top;int data;
if(ctr==null)
{
System.out.println("Stack is empty");
return ;
}
else if(ctr!=null && ctr.next==null)
{ data = top.data;
top = null;
}
else
{ data = top.data;
top = top.next;
}
System.out.println(data+" is successfully removed from the stack");
}
void display()
{ if (isEmpty())
{
System.out.println("The stack is empty");
}
else
{ System.out.println("The stack is ");
Node ctr=top;
while(ctr!=null)
{
System.out.print(ctr.data+" ");
ctr=ctr.next;
}
System.out.println();
}
}
public static void main(String[] args)
{
Stack st=new Stack();
Scanner sc=new Scanner(System.in);
System.out.println("Stack");
int choice; boolean f=true;
while(f)
{
System.out.println("Enter 1 to input data, 2 to delete data from stack, 3 to peek data from stack,4 to count the number of elements in the stack, 5 to display all elements in the stack and 6 to exit");
choice=sc.nextInt();
switch(choice)
{
case 1: System.out.println("Enter data to enter into stack");
int data = sc.nextInt();
st.push(data);
break;
case 2: st.pop();
break;
case 3: st.peek();
break;
case 4: st.count();
break;
case 5: st.display();
break;
case 6: f=false;
break;
default: System.out.println("Enter Valid Input");
break;
}
}
}
}
/*
Stack
Enter 1 to input data, 2 to delete data from stack, 3 to peek data from stack,4 to count the number of elements in the stack, 5 to display all elements in the stack and 6 to exit
1
Enter data to enter into stack
2
2 is successfully added to the stack
Enter 1 to input data, 2 to delete data from stack, 3 to peek data from stack,4 to count the number of elements in the stack, 5 to display all elements in the stack and 6 to exit
2
2 is successfully removed from the stack
Enter 1 to input data, 2 to delete data from stack, 3 to peek data from stack,4 to count the number of elements in the stack, 5 to display all elements in the stack and 6 to exit
1
Enter data to enter into stack
3
3 is successfully added to the stack
Enter 1 to input data, 2 to delete data from stack, 3 to peek data from stack,4 to count the number of elements in the stack, 5 to display all elements in the stack and 6 to exit
3
3
Enter 1 to input data, 2 to delete data from stack, 3 to peek data from stack,4 to count the number of elements in the stack, 5 to display all elements in the stack and 6 to exit
4
The stack has 1 elements
Enter 1 to input data, 2 to delete data from stack, 3 to peek data from stack,4 to count the number of elements in the stack, 5 to display all elements in the stack and 6 to exit
5
The stack is
3
Enter 1 to input data, 2 to delete data from stack, 3 to peek data from stack,4 to count the number of elements in the stack, 5 to display all elements in the stack and 6 to exit
6
*/