forked from gaurav03kr/hello-hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacks.java
112 lines (82 loc) · 1.54 KB
/
stacks.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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com;
/**
*
* @author zenalarifin
*/
import java.util.Scanner;
public class Program{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
Stack s1 = new Stack();
int opt = 0;
do{
System.out.println("Choose an option:" + "\n" +
"1) Add item \n" +
"2) Del item \n" +
"3) Show Items \n" +
"4) Stack lenght \n" +
"5) Exit \n");
opt = in.nextInt();
switch(opt){
case(1):
System.out.println("[++] Put the number to add:");
int val = in.nextInt();
s1.add_Node(val);
break;
case(2):
s1.del_Node();
break;
case(3):
s1.show_Nodes();
break;
case(4):
s1.stack_Lenght();
break;
}
}while(opt != 5);
System.exit(0);
}
}
class Stack{
public Node last_node;
int lenght;
public Stack(){
last_node = null;
lenght = 0;
}
public void add_Node(int value){
Node new_node = new Node(value);
new_node.previous = last_node;
last_node = new_node;
lenght++;
}
public void del_Node(){
int temp = last_node.value;
last_node = last_node.previous;
System.out.println("> Item: " + temp + " deleted!");
}
public void show_Nodes(){
Node temp = last_node;
String list = "";
while(temp!=null){
list += temp.value + "\n";
temp = temp.previous;
}
System.out.println(list);
}
public int stack_Lenght(){
return lenght;
}
}
class Node{
public Node previous;
int value;
public Node(int value){
this.value = value;
previous = null;
}
}