-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.java
More file actions
141 lines (135 loc) · 2.35 KB
/
stack.java
File metadata and controls
141 lines (135 loc) · 2.35 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
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
import java.util.*;
class Node{
int data;
Node top;
public Node(){}
public Node(int a)
{
this.data=a;
}
}
class stck extends Node{
Node head;
void push(int d){
Node temp=null;
if(head==null)
{
head=new Node(d);
}
else{
temp=new Node(d);
temp.top=head;
head=temp;
}
}
void pop(){
int value=0;
if(head==null){
System.out.println("empty stack");
return;
}
else{
value = head.data;
head=head.top;
}
System.out.println(value+" poped successfully!");
}
void display(){
Node t=head;
if(t==null)
{
System.out.println("Empty stack");
return;
}
do{
System.out.println("| "+t.data+" |");
System.out.println("+----+");
t=t.top;
}while(t!=null);
}
boolean isEmpty()
{
if(head==null)
return true;
else
return false;
}
int size()
{
int count=0;
Node t = head;
if(t==null)
return count;
do{
count+=1;
t=t.top;
}while(t!=null);
return count;
}
int peek()
{
int value=0;
if(head==null)
System.out.println("Empty Stack");
else
{
value=head.data;
}
return value;
}
}
public class stack{
public static void main(String[] args){
stck s =new stck();
Scanner c = new Scanner(System.in);
while(true)
{
System.out.println("\n-------------------------------------------------------------------------------");
System.out.println("Stack: \n== 1.push == 2.pop == 3.display == 4.isEmpty == 5.peek == 6.size == 7.exit ==");
System.out.println("-------------------------------------------------------------------------------");
int choice = c.nextInt();
switch(choice)
{
case 1:
{
System.out.println("enter the value to be pushed");
s.push(c.nextInt());
break;
}
case 2:
{
s.pop();
break;
}
case 3:
{
s.display();
break;
}
case 4:
{
System.out.println("The stack is empty : "+s.isEmpty());
break;
}
case 5:
{
System.out.println("The peek element is : "+s.peek());
break;
}
case 6:
{
System.out.println("\nThe size of the Stack is : "+s.size());
break;
}
case 7:
{
return;
}
default:
{
System.out.println("Warning: Enter the correct choice !");
}
}
}
}
}