-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplementeEnlazada.java
More file actions
133 lines (112 loc) · 2.46 KB
/
SimplementeEnlazada.java
File metadata and controls
133 lines (112 loc) · 2.46 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
/**
* Universidad del Valle de Guatemala
* Algoritmos y Estructura de Datos
* Sección: 10
* 20/08/2015
* Hoja de Trabajo 4
*
*/
/**
* La calse SimplementeEnlazada es una lista que, como su nombre
* lo indica es de enlace simple. Solo se puede apuntar al siguiente
* nodo y no se puede apuntar al anterior ni al ultimo. Hay un atritubo
* que se llama head que apunta al primer nodo de la lista y ayudará a
* encontrar valores siguientes o apuntar a otros nodos.
*
* @author Rudy Garrido
* @author Yosemite Meléndez
*
* @param <E>
*/
public class SimplementeEnlazada<E> extends Lista<E>{
private Nodo<E> head=null;
/**
* Este es el constructor de objetos. Crea un count que lo inicializa
* en cero y head que al inicio es null porque la lista está vacía
*/
public SimplementeEnlazada() {
cont = 0;
head = null;
// TODO Auto-generated constructor stub
}
public E removeLast() {
Nodo <E> finger = head;
Nodo <E> previous = null;
while (finger.getNext() != null){ //fin de la lista
previous = finger;
finger = finger.getNext();
}
//finger = null o fin de la lista
if (previous == null){
head = null; //un elemento
}
else{
previous.setNext(null);
}
cont--;
return finger.getValor();
}
public void addLast(E value) {
Nodo <E> temp = new Nodo <E> (value);
if (head!=null){
Nodo<E> finger = head;
while (finger.getNext() != null){
finger = finger.getNext();
}
finger.setNext(temp);
}
else head = temp;
cont ++;
}
public E getLast() {
Nodo <E> finger = head;
Nodo <E> previous = null;
while (finger.getNext() != null){ //fin de la lista
previous = finger;
finger = finger.getNext();
}
return previous.getValor();
}
public boolean contains(E value) {
Nodo <E> finger = head;
while (finger != null && !finger.getValor().equals(value)){
finger = finger.getNext();
}
return finger != null;
}
@Override
public void empty() {
cont=0;
head=null;
}
@Override
public boolean isEmpty() {
return cont==0;
}
@Override
public void push(E x) {
Nodo<E> temporal = head;
head = new Nodo<E>(x) ;
head.setNext(temporal);
cont++;
}
@Override
public E pop() throws Exception {
Nodo <E> temp = head;
if(head!=null && head.getNext()!=null){
head = head.getNext();
}else{
head=null;
}
cont--;
return temp.getValor();
}
@Override
public E peek() throws Exception {
return head.getValor();
}
@Override
public int size() {
return cont;
}
}