Skip to content

Instantly share code, notes, and snippets.

@gtcarlos
Created October 29, 2012 00:51
Show Gist options
  • Save gtcarlos/3970756 to your computer and use it in GitHub Desktop.
Save gtcarlos/3970756 to your computer and use it in GitHub Desktop.
Transformando Pilha em Fila na linguagem C
#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");
}
@MuriloRibeiroFloripa
Copy link

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment