Sunteți pe pagina 1din 25

Lista

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

a b c d

©® Dr. Alfredo Gutiérrez Hdez. 1


Lista
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

a b c d

©® Dr. Alfredo Gutiérrez Hdez. 2


Lista
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

a b c d e

©® Dr. Alfredo Gutiérrez Hdez. 3


Lista
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

a c d e

©® Dr. Alfredo Gutiérrez Hdez. 4


Lista
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

a b c d e
datos

ultimo
4

tipo_lista
#define TAMMAX 500
typedef _______ tipo_dato;

typedef struct {
tipo_dato datos[TAMMAX];
int ultimo;
} tipo_lista;

typedef int tipo_pos;


©® Dr. Alfredo Gutiérrez Hdez. 5
typedef tipo_alumno tipo_dato;

typedef struct {
tipo_dato datos[TAMMAX];
int ultimo;
} tipo_lista; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

typedef int tipo_pos;

int main () {
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n
tipo_lista altas;
tipo_lista bajas;
tipo_lista examen;
.
. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

.
altas.ultimo = -1; datos

scanf (“%s”, grupo.datos[25].nombre.apm);


}

©® Dr. Alfredo Gutiérrez Hdez. 6


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

Tipo de dato: Operación Regresa

• Conjunto de valores o elementos Inicializa - nada


Vacía - booleano
• Conjunto de operaciones Llena * - booleano
Inserta - nada
Elimina - nada
Primero - posición
Ultimo - posición
Anterior - posición
Siguiente - posición
Localiza - posición
Recupera - elemento
Ordena - nada
Imprime - nada
Anula - nada

©® Dr. Alfredo Gutiérrez Hdez. 7


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

datos

ultimo

función: inicializa
recibe: lista(datos[], último)
regresa: nada
último = –1

©® Dr. Alfredo Gutiérrez Hdez. 8


typedef _______ tipo_dato;

typedef struct{
tipo_dato datos[TAMMAX];
int ultimo;
} tipo_lista ;
función: inicializa
recibe: lista(datos[], último) typedef int tipo_pos;
regresa: nada
último = –1
void inicializa (tipo_lista *L) {
L->ultimo = -1;
}

©® Dr. Alfredo Gutiérrez Hdez. 9


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

datos

ultimo

función: vacía
recibe: lista(datos[], último)
regresa: booleano
¿último = –1?
Sí: regresar: verdadero
No: regresar: falso

©® Dr. Alfredo Gutiérrez Hdez. 10


#define FALSE 0
#define TRUE 1
·
·
·
int vacia (tipo_lista *L) {
if (L->ultimo = = -1)
return TRUE;
función: vacía else
recibe: lista(datos[], último) return FALSE;
regresa: booleano }
¿último = –1?
Sí: regresar: verdadero
No: regresar: falso

int vacia (tipo_lista *L) {


return L->ultimo = = -1;
}

©® Dr. Alfredo Gutiérrez Hdez. 11


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

datos

ultimo

función: llena
recibe: lista(datos[], último)
regresa: booleano
¿último = tamaño_del_arreglo – 1?
Sí: regresar: verdadero
No: regresar: falso

©® Dr. Alfredo Gutiérrez Hdez. 12


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

datos

ultimo
función: inserta
recibe: elem, pos, lista(datos[], último)
regresa: nada
¿llena(lista) ó pos < 0 ó pos > último + 1?
Sí: desplegar mensaje de error
terminar
i = último + 1
mientras i > pos
datos[i] = datos[i – 1]
i=i–1
fin mientras
datos[p] = elem
último = último + 1

©® Dr. Alfredo Gutiérrez Hdez. 13


función: inserta
recibe: elem, pos, lista(datos[], último)
regresa: nada
¿llena(lista) ó pos < 0 ó pos > último + 1?
Sí: desplegar mensaje de error
terminar
i = último + 1
mientras i > pos
datos[i] = datos[i – 1]
void inserta (tipo_dato e, tipo_pos pos, tipo_lista *L) {
i=i–1
fin mientras int i;
datos[p] = elem if (llena (L) || pos < 0 || pos > L->ultimo + 1)
último = último + 1 return;
for (i = L->ultimo + 1; i > pos; i--)
L->datos[i] = L->datos[i - 1];
L->datos[pos] = e;
L->ultimo++;
}

©® Dr. Alfredo Gutiérrez Hdez. 14


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

datos

ultimo
función: elimina
recibe: pos, lista(datos[], último)
regresa: nada
¿vacía(lista) ó pos < 0 ó pos > último?
Sí: desplegar mensaje de error
terminar
i = pos
mientras i < último
datos[i] = datos[i + 1]
i=i+1
fin mientras
último = último – 1

©® Dr. Alfredo Gutiérrez Hdez. 15


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

datos

ultimo

función: primero
recibe: lista(datos[], último)
regresa: posición
¿vacía(lista)?
Sí: regresar: –1
No: regresar: 0

©® Dr. Alfredo Gutiérrez Hdez. 16


función: primero
recibe: lista(datos[], último)
regresa: posición
¿vacía(lista)?
Sí: regresar: –1
No: regresar: 0
tipo_pos primero (tipo_lista *L) {
if (vacia (L))
return -1;
else
return 0;
}

©® Dr. Alfredo Gutiérrez Hdez. 17


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

datos

ultimo

función: último
recibe: lista(datos[], último)
regresa: posición
regresar: último

©® Dr. Alfredo Gutiérrez Hdez. 18


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

datos

ultimo

función: anterior
recibe: pos, lista(datos[], último)
regresa: posición
¿vacía(lista) ó pos < 1 ó pos > ultimo?
Sí: regresar: –1
No: regresar: pos - 1

©® Dr. Alfredo Gutiérrez Hdez. 19


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

datos

ultimo

función: siguiente
recibe: pos, lista(datos[], último)
regresa: posición
¿vacía(lista) ó pos < 0 ó pos > último –1?
Sí: regresar: –1
No: regresar: pos + 1

©® Dr. Alfredo Gutiérrez Hdez. 20


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

datos

ultimo

función: localiza
recibe: elem, lista(datos[], último)
regresa: posición
regresar: llamar: búsqueda lineal(elem, datos, último)

©® Dr. Alfredo Gutiérrez Hdez. 21


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

datos

ultimo

función: recupera
recibe pos, lista(datos[], último)
regresa: elemento
¿vacía(lista) ó pos < 0 ó pos > último?


Sí: ¡Error de excepción!
Insuficiencia de datos
Terminar
No: regresar: datos[pos]

©® Dr. Alfredo Gutiérrez Hdez. 22


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

datos

ultimo

función: ordena
recibe: lista(datos[], último)
regresa: nada
llamar: quicksort(datos, 0, último)

©® Dr. Alfredo Gutiérrez Hdez. 23


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

datos

ultimo

función: imprime
recibe: lista(datos[], último)
regresa: nada
i=0
mientras i ≤ último
imprimir: datos[i]

i=i+1
fin mientras

©® Dr. Alfredo Gutiérrez Hdez. 24


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n-2 n-1 n

datos

ultimo

función: anula
recibe: lista(datos[], último)
regresa: nada
último = –1

©® Dr. Alfredo Gutiérrez Hdez. 25

S-ar putea să vă placă și