Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Considere la siguiente estructura para el tipo de dato Ramo:
- typedef struct{
- char Nombre[100];
- char Codigo[12];
- char prereq1[12];
- char prereq2[12];
- } Ramo;
- Escriba la función Ramo* creaRamo(char line[]), la cual a partir de una linea de texto (line) con el siguiente formato:
- "Codigo","Nombre","Pre-requisito1","Pre-requisito2",...
- crea un dato de tipo Ramo y retorna un puntero a éste.
- Se recomienda usar la función strtok.
- _______________________________________________________________________________________________________________________________________
- Ramo* creaRamo(char line[]){
- Ramo* linea =(Ramo*) malloc(sizeof(Ramo));
- const char s[2] = ",";
- char *token;
- token=strtok(line, s);
- token[strlen(token)-1]='\0';
- strcpy(linea->Codigo,token+1);
- token=strtok(NULL, s);
- token[strlen(token)-1]='\0';
- strcpy(linea->Nombre,token+1);
- token=strtok(NULL, s);
- token[strlen(token)-1]='\0';
- strcpy(linea->prereq1,token+1);
- strcpy(linea->prereq2,token+strlen(linea->prereq1));
- return linea;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Considere la siguiente estructura para el tipo de dato Ramo:
- typedef struct{
- char Nombre[30];
- char Codigo[10];
- char prereq1[10];
- char prereq2[10];
- } Ramo;
- Escriba un código que genere el arreglo Ramo r[] con los ramos del archivo "mallainf.csv".
- Puede usar (¡no la implemente!) la función Ramo* creaRamo(char[]) de la pregunta anterior.
- _______________________________________________________________________________________________________________________________________
- Ramo *r = (Ramo*) malloc (sizeof(Ramo));
- char cadena[10000];
- int i = 1;
- Ramo *ptr = (Ramo*) malloc (sizeof(Ramo));
- FILE *entrada = fopen("mallainf.csv","r");
- if(entrada == NULL){
- printf("Archivo de entrada no se pudo abrir\n");
- exit(0);
- }
- fgets(cadena,10000,entrada);//NO recibo los datos, porque es una cadena de ejemplo
- while(fgets(cadena,10000,entrada) != NULL){
- ptr = creaRamo(cadena);
- r[i-1] = *ptr;
- i++;
- r = (Ramo*) realloc (r,sizeof(Ramo)*i);
- }
- fclose(entrada);
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Considere las estructuras:
- typedef struct stack{
- void** data; //arreglo de datos
- int size; // tamaño maximo del arreglo
- int top; // posicion actual de el top de la pila
- }stack;
- typedef struct queue{
- void** data; // arreglo de datos
- int size; //tamaño maximo del arreglo
- int top; //posicion del ultimo dato ingresado en la cola
- int bottom; //posicion anterior al primer dato ingresado en la cola
- }queue;
- Implemente las funciones stack* createStack(int size) y queue* createQueue(int size) que crean la pila y la cola con un arreglo de tamaño size, inicializan sus valores y retornan las variables. Considere que al crear la cola y la pila, sus posiciones iniciales son -1.
- _______________________________________________________________________________________________________________________________________
- stack* createStack(int size) {
- stack *ptr = (stack*) malloc(sizeof(stack));
- ptr -> data = (void*) malloc(size * sizeof(void*));
- ptr -> size = size;
- ptr -> top = -1;
- return ptr;
- }
- queue* createQueue(int size) {
- queue* ptr = (queue*) malloc(sizeof(queue));
- ptr -> data = (void*) malloc(size * sizeof(void*));
- ptr -> size = size;
- ptr -> top = -1;
- ptr -> bottom = ptr ->top;
- return ptr;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Considere las siguientes estructuras:
- typedef struct stack{
- void** data; //arreglo de datos
- int size; // tamaño del arreglo
- int top; // posicion actual de el top de la pila
- }stack;
- typedef struct queue{
- void** data; // arreglo de datos
- int size; //tamaño del arreglo
- int top; //posicion del ultimo dato ingresado en la cola
- int bottom; //posicion anterior al primer dato ingresado en la cola
- }queue;
- Escriba el código para la función void PushStack(stack* P, void* data) y void PushQueue(queue* Q, void* data). Estas funciones agregan el dato a las estructuras según su definición. Puede hacer uso de la funcion realloc en el caso que la pila o la cola ya estén llenas.
- _______________________________________________________________________________________________________________________________________
- void PushStack (stack* P,void* data) {
- if(P -> size == P -> top + 1 ) {
- P -> data = (void*) realloc (P->data, 2*P->size*sizeof(void));
- P -> size = 2*P->size;
- P -> top++;
- P -> data[P->top] = data;
- }
- else {
- P -> top++;
- P -> data[P->top] = data;
- }
- }
- void PushQueue(queue*Q,void* data) {
- if(Q -> size == Q -> top+1) {
- Q -> data = (void*) realloc (Q->data, 2*Q->size*sizeof(void));
- Q -> size= 2*Q->size;
- Q -> top++;
- Q -> data[Q->top] = data;
- }
- else {
- Q -> top++;
- Q -> data[Q->top] = data;
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Implemente las funciones void* top(stack* S) y void* front(queue* Q). Que retornan el dato del top de la pila y el front de la cola respectivamente. Recuerde que, si las estructuras están vacías, las funciones deben retornar NULL.
- typedef struct stack{
- void** data; //arreglo de datos
- int size; // tamaño maximo del arreglo
- int top; // posicion actual de el top de la pila
- }stack;
- typedef struct queue{
- void** data; // arreglo de datos
- int size; //tamaño maximo del arreglo
- int top; //posicion del ultimo dato ingresado en la cola
- int bottom; //posicion anterior al primer dato ingresado en la cola
- }queue;
- Nota: considere una pila vacía con top=-1, y una cola vacía con top=bottom
- _______________________________________________________________________________________________________________________________________
- void* top(stack* S) {
- if (S->top==-1) {
- return NULL;
- }
- else {
- return S->data[S->top];
- }
- }
- void* front(queue* S) {
- if (S->bottom == S->top) {
- return NULL;
- }
- else {
- return S->data[S->bottom+1];
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Escriba el código para las funciones void PopStack(stack* S) y void PopQueue(queue* Q). Estas funciónes eliminan un elemento de las estructuras según su definición.
- ArrayStack:
- typedef struct{
- void** data; //arreglo de punteros (void*)
- int size; //tamaño del arreglo
- int top; //tamaño actual del arreglo
- }stack;
- ArrayQueue:
- typedef struct{
- void** data; //arreglo de punteros (void*)
- int size; //tamaño total del arreglo
- int top; //indice de la cima de la cola
- int bottom; //indice del fondo de la cola
- }queue;
- _______________________________________________________________________________________________________________________________________
- void PopStack(stack* S) {
- S-> top--;
- }
- void PopQueue(queue* Q) {
- Q-> bottom++ ;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment