-
-
Save gtcarlos/3970756 to your computer and use it in GitHub Desktop.
#include <stdio.h> | |
#include <stdlib.h> | |
struct pilha | |
{ | |
int info; | |
struct pilha *prox; | |
}; | |
int inserir(struct pilha **fila,struct pilha **fim,int n) | |
{ | |
if(*fila==NULL) | |
{ | |
(*fila)=(*fim)=(struct pilha*)malloc(sizeof(struct pilha)); | |
if (*fila==NULL) | |
{ | |
return -1; | |
} | |
(*fila)->info=n; | |
(*fila)->prox=NULL; | |
return (0); | |
} | |
struct pilha *aux; | |
aux=(struct pilha*)malloc(sizeof(struct pilha)); | |
if(aux==NULL) | |
{ | |
return (-1); | |
} | |
aux->info=n; | |
aux->prox=NULL; | |
(*fim)->prox=aux; | |
(*fim)=aux; | |
return (0); | |
} | |
int transformar(struct pilha **fila,struct pilha **pilha) | |
{ | |
if(*pilha==NULL) | |
{ | |
(*pilha)=(struct pilha*)malloc(sizeof(struct pilha)); | |
if (*pilha==NULL) | |
{ | |
return -1; | |
} | |
(*pilha)->info=(*fila)->info; | |
(*pilha)->prox=NULL; | |
(*fila)=(*fila)->prox; | |
struct pilha *aux; | |
while((*fila)!=NULL) | |
{ | |
aux=(struct pilha*)malloc(sizeof(struct pilha)); | |
if(aux==NULL) | |
{ | |
return (-1); | |
} | |
aux->info=(*fila)->info; | |
aux->prox=(*pilha); | |
(*pilha)=aux; | |
(*fila)=(*fila)->prox; | |
} | |
} | |
return (0); | |
} | |
main() | |
{ | |
int n,x,y,t=0;; | |
struct pilha *fila,*fim,*pilha; | |
fila=fim=NULL; | |
pilha=NULL; | |
while(t!=4) | |
{ | |
printf("Digite um numero para inserir na fila: "); | |
scanf("%d",&n); | |
x=inserir(&fila,&fim,n); | |
t++; | |
} | |
printf("\nTRANSFORMANDO FILA EM PILHA ...\n\n"); | |
y=transformar(&fila,&pilha); | |
printf("Pilha transformada:\n\n"); | |
while(pilha!=NULL) | |
{ | |
printf("%d ",pilha->info); | |
pilha=pilha->prox; | |
} | |
free(fila); | |
printf("\n"); | |
} |
arquivo #include "collection.h"(tem que salvar, como: Collection.h, arquivo separado porem na mesma pasta").
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////// Alunos: Murilo e Ramom /////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
typedef struct Collection_ {
int Tamanho;
int elements[MAX][MAX];
} Collection;
typedef struct Pilha {
int Tamanho;
int elements[MAX];
} Pilha;
resolvido
/********************************************************************************************************************
*********************************************************************************************************************/
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////// Alunos: Murilo e Ramom /////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#define MAX 20
#include "collection.h"
#include <stdio.h>
#include <strings.h>
#include<stdlib.h>
#define TRUE 0
#define FALSE 1
Collection criar_fila();
int insere(Collection *fila, char *textoAInserir);
int retira(Collection *fila, char *textoRemovido);
int inversor(char *textoOriginal, char *textoInvertido);
//----- Esta parte do código não pode ser alterada ------
int main() {
char textos[20][20] = { "Abertura", "Continuo", "Contador", "Marcado", "Automovel", "Gentileza", "Verdade", "Fermento", "Teclado", "Barbeiro", "Treinamento", "Dinamismo", "Homenagem", "Levantar", "Integral"};
char textosInvertidos[20][20] = { "arutrebA", "ounitnoC", "rodatnoC", "odacraM", "levomotuA", "azelitneG", "edadreV", "otnemreF", "odalceT", "oriebraB", "otnemanierT", "omsimaniD", "meganemoH", "ratnaveL", "largetnI"};
system ("pause");
return 0;
}
//----------- Final da parte do código que não pode ser alterada ---------
Collection criar_fila() {
Collection texto;
texto.Tamanho = 0;
return texto;
}
int insere(Collection *fila, char *textoAInserir) {
if(fila->Tamanho == MAX) return FALSE;
strcpy(fila->elements[fila->Tamanho], textoAInserir);
fila->Tamanho++;
return TRUE;
}
int retira(Collection *fila, char *textoRemovido) {
if(fila->Tamanho == 0) return FALSE;
strcpy(textoRemovido, fila->elements[0]);
int i;
for(i = 0; i < fila->Tamanho-1; i++)
strcpy(fila->elements[i], fila->elements[i+1]);
fila->Tamanho--;
return TRUE;
}
// criando a pilha
Pilha criar_pilha() {
Pilha inicio;
inicio.Tamanho = 0;
return inicio;
}
/*
Rotina que faz a inserção de elementos na pilha
A função não retorna valor algum
*/
void push(Pilha *pilha, char texto) {
pilha->elements[pilha->Tamanho] = texto;
pilha->Tamanho++;
}
/*
Rotina que retira elementos da pilha
O elemento retirado sempre é o que está no topo
*/
char pop(Pilha *pilha) {
return pilha->elements[--pilha->Tamanho];
}
//Rotina inversor
int inversor(char *textoOriginal, char *textoInvertido) {
Pilha pilha = criar_pilha();
int i = 0;
while(textoOriginal[i] != '\0') {
push(&pilha, textoOriginal[i]);
i++;
}
int j;
for(j = 0; i > 0; i--, j++) {
textoInvertido[j] = pop(&pilha);
}
textoInvertido[j] = '\0';
return TRUE;
}