Created
October 29, 2012 00:51
-
-
Save gtcarlos/3970756 to your computer and use it in GitHub Desktop.
Transformando Pilha em Fila na linguagem C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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;