Dev - C++ - Ayuda C++ Listas Enlazadas Insertar Ordenado

 
Vista:
sin imagen de perfil

Ayuda C++ Listas Enlazadas Insertar Ordenado

Publicado por Alejandro (1 intervención) el 03/10/2017 01:20:34
Tengo el siguiente codigo de c++ de una lista enlazada, debo implementar un metodo que inserte ordenadamente, yo lo he implementado hasta donde pude pero aun no consigo que funcione agradeceria una colaboración

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// para compilar:
// g++ -g -o simple listaSimple.cpp
 
// g++ -std=c++11 -o simple listaSimple.cpp
 
#include <iostream>
#include <string>
using namespace std;
 
class Nodo {
  private:
   int valor;
   Nodo *sig;
 
  friend class Lista;
 
  public:
    Nodo(int valor, Nodo *sig=NULL){
      this->valor = valor;
      this->sig = sig;
    }
};
 
typedef Nodo *apNodo;
 
class Lista{
  private:
    apNodo cabeza;
    apNodo index;
  public:
   Lista(){
     this->cabeza = NULL;
     this->index = NULL;
   }
   ~Lista(){};
   void addFiFo(int valor);
   void addLiFo(int valor);
   void addOrdenado(int valor);
   bool estaVacia();
   string verLista();
   string verLista2();
   string verLista3();
   void borrar(int valor);
  void borrar2(int valor);
   bool estoyUltimo();
};
 
bool Lista::estoyUltimo(){
  return this->index->sig==NULL;
}
 
bool Lista::estaVacia(){
  return this->cabeza==NULL;
}
 
//declarar los metodos
void Lista::addLiFo(int valor){
  //la lista esta vacia
  if (this->estaVacia()){
    this->cabeza = new Nodo(valor);
  }
  //la lista tiene elementos
  else {
    apNodo item = new Nodo(valor, this->cabeza);
    this->cabeza = item;
  }
}
 
void Lista::addFiFo(int valor){
  //la lista esta vacia
  if (this->estaVacia()){
    this->cabeza = new Nodo(valor);
  }
  //la lista tiene elementos
  else {
    this->index = this->cabeza;
    while (this->index->sig){
      this->index = this->index->sig;
    }
    this->index->sig = new Nodo(valor);
  }
}
 
string Lista::verLista(){
  string texto = "";
 
  this->index = this->cabeza;
  if (this->index){
    do{
      texto = texto +  to_string(this->index->valor);
      texto = texto + " : ";
      this->index = this->index->sig;
    } while(this->index);
 
    texto = texto + "fin.";
  }
  return texto;
}
 
 
string Lista::verLista2(){
  string texto = "";
 
  this->index = this->cabeza;
  if (this->index){
    while(this->index){
      texto = texto +  to_string(this->index->valor);
      texto = texto + " : ";
      this->index = this->index->sig;
    }
    texto = texto + "fin.";
  }
  return texto;
}
 
 
 
 
string Lista::verLista3(){
  string texto = "";
 
  this->index = this->cabeza;
  if (this->index){
    while(!this->estoyUltimo()){
      texto = texto +  to_string(this->index->valor);
      texto = texto + " : ";
      this->index = this->index->sig;
    }
    if (this->index){
      texto = texto +  to_string(this->index->valor);
      texto = texto + " : ";
    }
    texto = texto + "fin.";
  }
  return texto;
}
 
void Lista::borrar(int valor){
  if (estaVacia()){
    return;
  } else {
    this->index = this->cabeza;
    if (this->cabeza->valor == valor){
      this->cabeza = this->cabeza->sig;
      delete this->index;
    } else {
      apNodo bas = index;
      //bas = index;
      index = index->sig;
      while(this->index){
        if (this->index->valor == valor){
          this->index = bas;
	  apNodo xx = this->index->sig;
	  this->index->sig = this->index->sig->sig;
	  delete xx;
	  return;
	}
	bas = index;
	this->index = this->index->sig;
      }
    }
  }
}
 
void Lista::borrar2(int valor){
   //.... pendiente por desarrollar
  if (this->estaVacia()){
    //  :( la lista esta vacia y no hago nada :(
    return;
  }else {
    this->index = this->cabeza;
    if (this->index->valor == valor){
      //el elemento a borrar esta en la cabeza
      this->cabeza = this->index->sig;
      delete this->index;
    } else {
      //el elemento a borrar puedo estar en el cuerpo
      while(!estoyUltimo()){
	if (this->index->sig->valor == valor){
          apNodo tmp = this->index->sig;
	  this->index->sig = this->index->sig->sig;
	  delete tmp;
	  return;
	}
	this->index = this->index->sig;
      }
    }
  }
}
 
void Lista::addOrdenado(int valor){
  //la lista esta vacia
  if(this->estaVacia()){
    this->cabeza = new Nodo(valor);
  }
  //La lista tiene elementos
  else {
    //El valor ingresado es mayor a la index
  this->index = this->cabeza;
  if(valor<this->index->valor){
    apNodo tmp = this->index;
    this->cabeza = new Nodo(valor);
    this->cabeza->sig= tmp;
 
  } else {
    while(!this->estoyUltimo()){
      if(this->index->valor>valor){
        this->index->sig = new Nodo(valor);
      } else {
   this->index = this->index->sig;}
  } else {
    apNodo tmp2 = this->index;
    this->index = new Nodo(valor);
    this->index->sig = tmp2;
  }
 }
}
}
 
 
//===[ EJEMPLO ]=========================
 
int main(){
 
 
  Lista listaLiFo;
  //realizar varias inserciones FiFo
  listaLiFo.addOrdenado(30);
  listaLiFo.addOrdenado(10);
  listaLiFo.addOrdenado(20);
  listaLiFo.addOrdenado(50);
  listaLiFo.addOrdenado(40);
  cout << "Lista LiFo de (10-20-30-40-50): ";
  cout << listaLiFo.verLista() << endl;
 
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder