ESTRUCTURAS DE DATOS 2
description
Transcript of ESTRUCTURAS DE DATOS 2
![Page 1: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/1.jpg)
ESTRUCTURAS DE DATOS 2
![Page 2: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/2.jpg)
Agenda
– Resumen clase anterior– Tamaño de una estructura.– Arreglo de Estructuras.– Apuntadores a estructuras– Estructuras Anidadas
![Page 3: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/3.jpg)
RESUMEN – INTROD.
• Se puede utilizar una estructura para almacenar diferentes tipos de información sobre una persona, tal como nombre, estado civil, edad, fecha de nacimiento,…
RESUMEN
Ejemplo: Estructura ‘estudiante de Informática 1’
MIEMBROSNombreCedulaCorreo ElectrónicoNota TeóriaNota LaboratorioFaltas
TIPOS DE DATOSCadena de Caracteres max 50Cadena de Caracteres max 15Cadena de Caracteres max 50Numerico flotanteNumerico flotanteNumerico entero.
![Page 4: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/4.jpg)
Declaración de Estructura “Estudiante Informatica1”
REPASO
struct nombreDeLaEstructura{
tipoDeDatoMiembro1 nombreMiembro1; tipoDeDatoMiembro2 nombreMiembro2; ... tipoDeDatoMiembroN nombreMiembroN;} ;
struct estudianteInform1{ char nombre[50]; char cedula[15]; char email[50]; float notaTeorica; float notaLab; int faltas;} ;
![Page 5: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/5.jpg)
Inicialización de Variables tipo estructura
Variables tipo Estructura
struct cdsMusica{ char titulo[40]; char artista[40]; char genero[15]; int numCanciones; int lanzamiento; int precio;};
struct cdsMusica cd1= { “The Best Of 1980-1990", “U2", “Rock” , 15, 1998, 30000};
Cuando se declara una variable, se reserva espacio en memoria.
struct cdsMusica cd1;
…
…
…
…
…
…
cd1 titulo
artista
genero
NumCanc.
añoLanza.
precio
![Page 6: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/6.jpg)
ACCESO A ESTRUCTURASVariables tipo Estructura
Una Variable tipo estructura, así tenga internamente otros tipos de datos(No importa cuantos ni cuales), se sigue considerando como UNA SOLA VARIABLE.
Se puede copiar todo el contenido de variable tipo estructura a otra que tenga el mismo tipo de estructura
cd1=cd2; //Todo el contenido de cd2 se copia en cd1
![Page 7: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/7.jpg)
ACCESO A ESTRUCTURASVariables tipo Estructura
cd1=cd2;
…
…
…
…
…
…
cd1 titulo
artista
genero
NumCanc.
añoLanza.
precio
…
…
…
…
…
…
cd2 titulo
artista
genero
NumCanc.
añoLanza.
precio
struct cdsMusica cd2,cd1= { “The Best Of 1980-1990", “U2", “Rock” , 15, 1998, 50000};
![Page 8: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/8.jpg)
ACCESO A ESTRUCTURASVariables tipo Estructura
cd1=cd2; //Todo el contenido de cd2 se copia en cd1
Un elemento especifico dentro de una variable estructura se puede accesar por medio del operador “.” ó “->” (Con apuntadores).
nombreVarEstructura.nombreMiembro = dato;
Cada miembro de una variable estructura se maneja como si fuera otra variable común y corriente.
![Page 9: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/9.jpg)
ACCESO A ESTRUCTURASVariables tipo Estructura
Como copia una cadena de caracteres a un miembro de un estructura que es tipo cadena de caracteres? Ejemplo a titulo
…
…
…
…
…
…
cd1struct cdsMusica{ char titulo[40]; char artista[40]; char genero[15]; int numCanciones; int lanzamiento; int precio;};
int main(void){ struct cdsMusica cd1; ….. cd1.numCanciones=10; cd1.precio=30000; printf(“%d”, cd1.precio);
……. strcpy(cd1.titulo, "The Best");
}
titulo
artista
genero
NumCanc.
añoLanza.
precio
10
30000
![Page 10: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/10.jpg)
RECUERDE ……
Un arreglo es …..
La dirección de un arreglo se obtiene ….
Una estructura es…Una colección de varios datos bajo un nombre general. Una estructura es una VARIABLE.
La dirección de una estructura se obtiene….
RESUMEN
![Page 11: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/11.jpg)
TAMAÑO DE UNA ESTRUCTURATAMAÑO
struct cdsMusica cd1; tam=sizeof(cd1);printf ("El tamaño en Bytes de cd1 es: %d \n",tam) ;
El operador sizeof se puede aplicar para determinar el tamaño en bytes que ocupa en memoria una estructura, u otra variable
Tipo de Datos Tamaño Bytes
char 1
booleano 2
int 4
float 4
![Page 12: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/12.jpg)
ARREGLOS DE ESTRUCTURAS
Se define un arreglo de variables del tipo struct cdsMus:struct cdsMus cds[10];
Se Accede a la variable estructura dentro del arreglo:cds[0]=cds[1]; //Se copia toda la información
Se puede acceder a cualquier miembro de cualquiera de las variables estructura del arreglo cds[0].numCanc=10;
cds[3].titulo="Los cañonazos"; printf("El titulo del CD3 es: %s\n", cds[3].titulo);
struct cdsMus{ char titulo[20]; char autor[20]; int numCanc; int lanz; };
ERROR!!!
![Page 13: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/13.jpg)
#include <stdio.h>
struct cdsMus{ char titulo[20]; char autor[20]; int numCanc; int lanz; char flag; };int main(void){ struct cdsMus cds[10]; int opc; printf("\n PROGRAMA PARA MANEJAR COLECCION DE CDS (Max10) \n"); while(1){ printf("\n1. para modificar 2.para consultar\n"); scanf("%d",&opc); if(opc==1){ printf("\nIngrese el # del CD a modificar:\n"); scanf("%d",&opc); printf("-----------------------------\n"); printf("Ingrese el titulo del CD%d: ",opc); scanf("%s",cds[opc].titulo); printf("Ingrese el autor del CD%d: ",opc); scanf("%s",cds[opc].autor); printf("Ingrese el NumCanciones del CD%d: ",opc); scanf("%d",&cds[opc].numCanc); printf("Ingrese el agno de lanzamiento del CD%d: ",opc); scanf("%d",&cds[opc].lanz); printf("-----------------------------\n"); cds[opc+1]=cds[opc]; //Se copia el cd[i] a cd[i+1] } else if(opc==2){ printf("\nIngrese el # del CD a visualizar\n"); scanf("%d",&opc); printf("-----------------------------\n"); printf("El titulo del CD%d es: %s\n",opc,cds[opc].titulo); printf("El autor del CD%d es: %s\n",opc,cds[opc].autor); printf("El #canciones del CD%d es: %d\n",opc,cds[opc].numCanc); printf("El lanzamiento del CD%d fue %d\n",opc,cds[opc].lanz); printf("-----------------------------\n"); } else{} }return 0;}
![Page 14: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/14.jpg)
APUNTADORES A ESTRUCTURAS
Se define una variable estud1 del tipo struct estudiante:struct estudiante estud1={"Jose" , 4 , 2009 , 4.5};
Se define una variable puntero para apuntar a variables del tipo struct estudiante:
struct estudiante *ptrEstruct;
Apunte con ptrEstruct a la variable estud1:
ptrEstruct=&estud1;
El operador puntero, ->, sirve para acceder a los datos de la estructura a partir de un apuntador.
printf("La nota de estud1 es %d", ptrEstruct->nota);//Es lo mismo que printf("La nota de estud1 es %d", estud1.nota);
struct estudiante{char Nombre[40];int numEstudiante;int agnoMatricula;float nota;};
![Page 15: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/15.jpg)
#include <stdio.h>
struct estudiante{ char Nombre[40]; int numEstudiante; int agnoMatricula; float nota;};
int main(void){ struct estudiante estud1={"Jose" , 4 , 2009 , 4.5}; struct estudiante *ptrEstruct; ptrEstruct=&estud1; printf("La nota de estud1 es %.2f (Con Apuntador)\n", ptrEstruct->nota); //Metodo1 printf("La nota de estud1 es %.2f (Sin Apuntador)\n", estud1.nota); //Metodo2 getchar(); return 0;}
APUNTADORES A ESTRUCTURASEjemplo:
![Page 16: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/16.jpg)
Estructuras anidadas
Cliente
Dirección
Cll Num CiuNombre Saldo
Estudiante
Notas
N1 N2 N3Nombre Código
Los campos de las estructuras puede ser la combinación de los siguientes datos:
• Variables: char, int, double,….• Apuntadores• Arreglos de variable• OTRA ESTRUCTURA
struct cliente{ char nombre[20]; struct direccion direc; int saldo;};
struct estudiante{ char nombre[20]; int codigo; struct notas notaEst;};
![Page 17: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/17.jpg)
Estructuras anidadas(2)Cliente
Dirección
Cll Num CiuNombre Saldo
struct cliente{ char nombre[20]; struct direccion direc; int saldo;};
struct cliente{ char nombre[20]; struct direccion{ char cll[5];
char num[5];char ciud[15];
} direc; int saldo;};
struct direccion{ char cll[5];
char num[5];char ciu[15];
} ;struct cliente{ char nombre[20]; struct direccion direc; int saldo;};
![Page 18: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/18.jpg)
Bibliografía
• Joyanes Aguilar, Luis. PROGRAMACIÓN EN C. Metodología, algoritmos y estructura de datos Capitulo10.
![Page 19: ESTRUCTURAS DE DATOS 2](https://reader033.fdocumento.com/reader033/viewer/2022061605/56814f68550346895dbd1dca/html5/thumbnails/19.jpg)
GRACIAS