Ir ao conteúdo
  • Cadastre-se

C 10 números numa pilha. Crie fila com os números da pilha. Crie pilha com pares.


jerrym

Posts recomendados

Boa noite. Alguém poderia me ajudar com essa avaliação? Tenho até 23h59 para enviar e não estou conseguindo. Agradeço desde já, pessoal.

 

Leia 10 números e armazene numa pilha

a) Crie uma fila com os números da pilha

b) Crie outra pilha com os pares da fila criada

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <locale.h>
#include "pilha_seq.h"
#include "F_SEQ.H"


void p_pilha(void), exibirpilha(void);
void p_fila(void), exibirfila(void);
void p_pares(void), exibirpares(void);


tpilha p, aux, fila;
tpilha criarp2;
tfila info;


int main()
{
setlocale(LC_ALL, "Portuguese");


    int op;


    do
    {
      system("cls");
      printf("\n\n\n\n");
      printf("\t\t\t1 - EMPILHAR (ATÉ 10 NÚMEROS)\n");
      printf("\t\t\t2 - EXIBIR PILHA\n");
      printf("\t\t\t3 - CRIAR FILA COM OS NÚMEROS DA PILHA\n");
      printf("\t\t\t5 - CRIAR OUTRA PILHA (COM OS PARES)\n");
      printf("\t\t\t0 - SAIR\n\n\n");
      printf("\t\t\tOpção: ");
      scanf("%d", &op);


    switch (op){
      default: printf("\t\t\t\n\nOpção inválida!");
      case 0: break;
      case 1: p_pilha(); break;
      case 2: exibirpilha(); break;
      case 3: p_fila(); break;
      case 4: exibirfila(); break;
      case 5: p_pares(); break;
      case 6: exibirpares(); break;
      getch(); break;
    }
    }while(op != 0);
    
system("cls");
}


void p_pilha(void){

telem elem;

  criarp(&p);
    printf("\t\t\tDigite o valor a ser empilhado: ");
    scanf("%d",&elem);
      if (push(&p,elem))
        printf("\t\t\tElemento %d empilhado com sucesso!\n\n",elem);
      else
        printf("\t\t\tErro: Pilha cheia.\n\n");

getch();
system("cls");
}


void exibirpilha(void){

telem elem;

  criarp(&aux);
    printf("\n\t\t\tElementos da pilha: ");
    printf("\n\t\t\t");
      while (pop(&p,&elem))
      {
        printf("%d - ",elem);
        push(&aux,elem);
      }
      while (pop(&aux,&elem))
        push(&p,elem);
        printf("\n\n");
        
getch();
}


void p_fila(void){

telem elem;

  criarp(&aux);
    printf("Elementos da fila: ");
      while (pop(&p,&elem))
      {
        printf("%d - ",aux);
        push(&aux,elem);
      }
      while (pop(&aux,&elem))
        push(&p,elem);
printf("\n\n");

getch();
system("cls");
}


void exibirfila(void) {

  int i, lim;

  if (vaziaf(info)) {
    printf("\n\nFila vazia !!\n\n");
    return;
  }

  lim = info.inicio + info.tam;
  if (lim > MAX)
    lim = MAX;

  printf("\n\n\n");
  for(i=info.inicio; i<lim; i++)
    printf("%d - ", info.v);

  if (info.inicio > info.final)
    for(i=0; i<=info.final; i++)
      printf("%d - ", info.v);

  return;

getch();
}

 

void p_pares(void){

  int i, lim;
  int r;

  if (vaziaf(info)) {
    printf("\n\nFila vazia !!\n\n");
    return;
  }

  criarp(&aux);

  lim = info.inicio + info.tam;
  if (lim > MAX)
    lim = MAX;

  for(i=info.inicio; i<lim; i++)
    if (info.v % 2 == 0)
      push(&aux,info.v);
      printf("%d", info.v);

  return;

getch();
}

 

void exibirpares(void) {
} 

 

==========================================================================

==========================================================================

==========================================================================

 

BIBLIOTECA DE FILAS:

#define MAX 10

 

typedef int telem;

typedef struct {
  telem v[MAX];
  int inicio;
  int final;
  int tam;
} tfila;


void criarf (tfila *F) {

  F->inicio = 0;
  F->final  = -1;
  F->tam    = 0;

}

 

int vaziaf (tfila F) {

  return (F.tam == 0);

}

 

int primeirof (tfila F, telem *dado) {

  if (vaziaf(F))
    return (0);

  *dado = F.v[F.inicio];
  return (1);

}

 

int inserirf (tfila *F, telem valor) {

  if (F->tam == MAX)
    return (0);

  (F->tam)++;
  F->final = (F->final + 1) % MAX;
  F->v[F->final] = valor;

  return (1);

}

 

int removerf (tfila *F, telem *valor) {

  if (vaziaf(*F))
    return (0);

  primeirof(*F, valor);

  (F->tam)--;
  F->inicio = (F->inicio + 1) % MAX;

  return (1);
}

  

==========================================================================

==========================================================================

==========================================================================

 

BIBLIOTECA DE PILHAS:

typedef int telem;

#define MAX 10

typedef struct
{
    telem v[MAX];
    int topo;
} tpilha;

 

void criarp (tpilha *P)
{
    P->topo = -1;
}

 

int vaziap (tpilha P)
{
    return P.topo == -1;
}

 

int elemtopo (tpilha P, telem *valor)
{
    if (vaziap(P))
        return 0;
    *valor = P.v[P.topo];
    return 1;
}

 

int push(tpilha *P, telem valor)
{
    if (P->topo == MAX-1)
        return 0;
    (P->topo)++;
    P->v[P->topo] = valor;
    return 1;
} 

 

int pop(tpilha *P, telem *valor)
{
    if (vaziap(*P))
        return 0;
    *valor = P->v[P->topo];
    (P->topo)--;
    return 1;
}

 

Link para o comentário
Compartilhar em outros sites

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

Ebook grátis: Aprenda a ler resistores e capacitores!

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!