Skip to content

Instantly share code, notes, and snippets.

@AnneCosta
Last active March 17, 2018 02:31
Show Gist options
  • Save AnneCosta/874676058d0908d04a025a0f18b031fe to your computer and use it in GitHub Desktop.
Save AnneCosta/874676058d0908d04a025a0f18b031fe to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
typedef struct no{
int info;
struct no* prox;
}Fila;
typedef struct{
Fila* inicio;
Fila* fim;
}fila;
fila f;
int menu(){
int r;
printf(" Menu Fila\n");
printf("--------------------------------------------\n");
printf("1 - INSERIR NO FINAL\n");
printf("2 - REMOVER DO INÍCIO\n");
printf("3 - SAIR\n");
printf("---------------------------------------------\n\n");
printf("Digite a opção\n");
printf("> ");
scanf("%d", &r);
return r;
}
void inicia(fila *f){
f->inicio = NULL;
f->fim = NULL;
}
void inserir(fila *f, int dado){
Fila *aux;
aux = malloc(sizeof(Fila));
aux->info = dado;
aux->prox = NULL;
if(f->inicio == NULL){
f->inicio = aux;
}
else{
f->fim->prox = aux;
}
f->fim = aux;
}
void remover(fila *f){
Fila *aux;
if(f->inicio == NULL){
printf("Fila vazia.\n");
}
else{
aux = f->inicio;
f->inicio = aux->prox;
}
free(aux);
printf("Informação removida.\n");
}
main(){
setlocale(LC_ALL,"");
int r, i=0, dado;
inicia(&f);
r=menu();
while(r!=3){
system("cls");
if(r==1){
printf("Informe o %d° valor: ", i+1);
scanf("%d", &dado);
inserir(&f, dado);
i++;
}
else if(r==2){
remover(&f);
}
r=menu();
}
}
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
typedef struct no{
float info;
struct no* anterior;
}No;
typedef struct{
No* topo;
}Pilha;
Pilha *p;
int menu(){
int r;
printf(" Menu Pilha\n");
printf("---------------------------------------------\n");
printf(" 1 - EMPILHAR\n");
printf(" 2 - DESEMPILHAR\n");
printf(" 3 - EXIBIR\n");
printf(" 4 - SAIR\n");
printf("----------------------------------------------\n\n");
printf("Digite sua opção\n");
printf("> ");
scanf("%d", &r);
return r;
}
Pilha *criar(Pilha *p){
p = (Pilha*) malloc(sizeof(Pilha));
p->topo = NULL;
return p;
}
No *empilha(Pilha *p, float info){
No* aux;
aux = (No*) malloc(sizeof(No));
aux->info = info;
aux->anterior = p->topo;
p->topo = aux;
return aux;
}
void desempilha(Pilha *p){
float i;
No *aux;
if((p->topo == NULL) || (p->topo->anterior == NULL)){
printf("Pilha vazia.\n");
}
else{
i = p->topo->info;
aux = p->topo;
p->topo = aux->anterior;
free(aux);
printf("Valor removido: %.2f\n", i);
}
}
void exibir(Pilha *p){
No *i = p->topo;
if(i == NULL){
printf("Pilha vazia.\n");
}
else{
while(i != NULL){
printf("%.1f\n", i->info);
i = i->anterior;
}
}
}
main(){
setlocale(LC_ALL,"");
int r, i=0;
float info;
criar(&p);
r=menu();
while(r!=4){
system("cls");
if(r==1){
printf("Informe o %d° valor: ", i+1);
scanf("%f", &info);
empilha(&p, info);
i++;
}
else if(r==2){
desempilha(&p);
}
else if(r==3){
exibir(&p);
}
r=menu();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment