-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.java
125 lines (109 loc) · 3.29 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
import java.util.Scanner;
class Stack {
public static Node firstNode;
public static void main(String[] args) {
int op;
Scanner scan = new Scanner(System.in);
do {
menu();
System.out.print(" Introduce opcion: ");
op = scan.nextInt();
switch (op) {
case 1: firstNode = createNode(); break;
case 2: printStack(); break;
case 3: delSingleNode(); break;
case 4: delMultipleNodes(); break;
}
} while (op!=5);
}
public static String introducir() {
Scanner scan = new Scanner(System.in);
return scan.nextLine();
}
public static Node createNode() {
System.out.print("\n Info para el nodo: ");
String info = introducir();
if (firstNode == null) {
Node node = new Node(info);
return node;
} else {
Node node = new Node(info, firstNode);
return node;
}
}
public static void menu() {
System.out.println("\n\t\033[4mMenu\033[0m\n");
System.out.println(" 1. Crear nodo");
System.out.println(" 2. Mostrar stack");
System.out.println(" 3. Borrar nodo");
System.out.println(" 4. Borrar nodo(s)");
System.out.println(" 5. Salir\n");
}
public static void printStack() {
Node node = firstNode;
System.out.format("\n %-16s %-10s %s\n", "Self:", "Info:", "Next:");
while (node!=null) {
node.info();
node = node.next;
}
System.out.println();
}
public static void delete(Node d) {
Node node = firstNode;
while (node!=null) {
if (firstNode == d) {
firstNode = d.next;
} else if (node.next == d) {
node.next = d.next;
}
node = node.next;
}
}
public static void delSingleNode() {
Node node = firstNode;
System.out.print("\n Info del nodo a borrar: ");
String s = introducir();
while (node!=null) {
if (node.info.equals(s)) {
node.info();
System.out.print(" ==> Eliminar este nodo? (y/n): ");
if (introducir().equals("y")) {
delete(node);
}
node = null;
} else {
node = node.next;
}
}
}
public static void delMultipleNodes() {
Node node = firstNode;
System.out.print("\n Info del nodo(s) a borrar: ");
String s = introducir();
while(node!=null) {
if (node.info.equals(s)) {
node.info();
System.out.print(" ==> Eliminar este nodo? (y/n): ");
if (introducir().equals("y")) {
delete(node);
}
}
node = node.next;
}
}
}
class Node {
String info;
Node next;
public Node(String info) {
this.info = info;
}
public Node(String info, Node next) {
this.info = info;
this.next = next;
}
public void info() {
System.out.println();
System.out.format(" %-16s %-10s %s", this, info, next);
}
}