Ir para conteúdo

Função Empilha e Desempilha


Tiago_gad

Posts Recomendados

Boa noite, alguém pode corrigir meu código e me ajudar?
Estou fazendo um código que empilha e depois desempilha reempilhando na ordem contrária, alguém me ajuda aí no que falta, segue p código em questão:

//Bibliotecas
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
//Definindo uma constante com o tamanho exato que comporta o número do meu RA

#define tamanho 8

//Definindo uma estrutura
typedef struct {
    int dados[tamanho];
    int ini;
    int Topo;
} TPilha;

void TPilha_Inicia(TPilha *p) { 
    p->Topo = -1;
    
}
int TPilha_Vazia (TPilha *p) {
    if(p->Topo == -1) {
        return 1;
    }
    else 
    {
        return 0;
    }
}

int TPilha_Cheia (TPilha *p) {
    if(p->Topo == tamanho -1) {
        return 1;
    }
    else 
    {
        return 0;
    }
}

void TPilha_Insere (TPilha *p, int x) {
    if(TPilha_Cheia(p) ==1){
        printf("\n Erro: Pilha cheia");
    }
    else
    {
        p->Topo++;
        p->dados[p->Topo] = x;
    }
}

void imprimir(TPilha *p) {
    int i = 0;
    printf("\n\nImprimir:");
    for(i = 0; i < tamanho; i++) {
        printf("%d", p->dados);
    }
    
}

int TPilha_Retira (TPilha *p) {
    int aux;
    if(TPilha_Vazia(p) == 1){
        printf("\n Erro: A Pilha está vazia");
    }
    else
    {
        aux = p->dados[p->Topo];
        p->Topo--;
        return aux;
    }
}

 

 

int main(int argc, char *argv[]) {
    setlocale(LC_ALL, "portuguese");
    TPilha *p = (TPilha*)malloc(sizeof(TPilha));
    TPilha_Inicia(p);
    
    TPilha_Insere (p, 1);
    printf("\n Empilhou o 1");
    TPilha_Insere (p, 9);
    printf("\n Empilhou o 9");
    TPilha_Insere (p, 8);
    printf("\n Empilhou o 8");
    TPilha_Insere (p, 1);
    printf("\n Empilhou o 1");
    TPilha_Insere (p, 8);
    printf("\n Empilhou o 8");
    TPilha_Insere (p, 4);
    printf("\n Empilhou o 4");
    TPilha_Insere (p, 4);
    printf("\n Empilhou o 4");
    TPilha_Insere (p, 5);
    printf("\n Empilhou o 5");
    
    void imprimir(TPilha *p) ;
    
    system("\n pause");
    
    int aux;
    
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    
    system(" pause");
    
    return 0;
}

Link para o comentário
Compartilhar em outros sites

//Bibliotecas
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
//Definindo uma constante com o tamanho exato que comporta o número do meu RA

#define tamanho 8

//Definindo uma estrutura
typedef struct {
    int dados[tamanho];
    int ini;
    int Topo;
} TPilha;

void TPilha_Inicia(TPilha *p) { 
    p->Topo = -1;
    
}
int TPilha_Vazia (TPilha *p) {
    if(p->Topo == -1) {
        return 1;
    }
    else 
    {
        return 0;
    }
}

int TPilha_Cheia (TPilha *p) {
    if(p->Topo == tamanho -1) {
        return 1;
    }
    else 
    {
        return 0;
    }
}

void TPilha_Insere (TPilha *p, int x) {
    if(TPilha_Cheia(p) ==1){
        printf("\n Erro: Pilha cheia");
    }
    else
    {
        p->Topo++;
        p->dados[p->Topo] = x;
    }
}

void imprimir(TPilha *p) {
    int i = 0;
    printf("\n\nImprimir:");
    for(i = 0; i < tamanho; i++) {
        printf("%d", p->dados);
    }
    
}

int TPilha_Retira (TPilha *p) {
    int aux;
    if(TPilha_Vazia(p) == 1){
        printf("\n Erro: A Pilha está vazia");
    }
    else
    {
        aux = p->dados[p->Topo];
        p->Topo--;
        return aux;
    }
}

 

 

int main(int argc, char *argv[]) {
    setlocale(LC_ALL, "portuguese");
    TPilha *p = (TPilha*)malloc(sizeof(TPilha));
    TPilha_Inicia(p);

    int num[] = {1, 9, 8, 1, 8, 4, 4, 5};
    int count;

    for(count = 0; count < sizeof(num)/sizeof(int); count++){
	TPilha_Insere(p, num[count]);
	printf("%d ", p->dados[count]);
    }

    printf("\n\n");

    int aux;

    for(count = 0; count < 8; count++){
	aux = TPilha_Retira(p);
	printf("%d ", aux);
    }

    putchar('\n');
    system(" pause");

    return 0;
}

Era isso que vc queria fazer?

Link para o comentário
Compartilhar em outros sites

6 horas atrás, oito8bits disse:

//Bibliotecas
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
//Definindo uma constante com o tamanho exato que comporta o número do meu RA

#define tamanho 8

//Definindo uma estrutura
typedef struct {
    int dados[tamanho];
    int ini;
    int Topo;
} TPilha;

void TPilha_Inicia(TPilha *p) { 
    p->Topo = -1;
    
}
int TPilha_Vazia (TPilha *p) {
    if(p->Topo == -1) {
        return 1;
    }
    else 
    {
        return 0;
    }
}

int TPilha_Cheia (TPilha *p) {
    if(p->Topo == tamanho -1) {
        return 1;
    }
    else 
    {
        return 0;
    }
}

void TPilha_Insere (TPilha *p, int x) {
    if(TPilha_Cheia(p) ==1){
        printf("\n Erro: Pilha cheia");
    }
    else
    {
        p->Topo++;
        p->dados[p->Topo] = x;
    }
}

void imprimir(TPilha *p) {
    int i = 0;
    printf("\n\nImprimir:");
    for(i = 0; i < tamanho; i++) {
        printf("%d", p->dados);
    }
    
}

int TPilha_Retira (TPilha *p) {
    int aux;
    if(TPilha_Vazia(p) == 1){
        printf("\n Erro: A Pilha está vazia");
    }
    else
    {
        aux = p->dados[p->Topo];
        p->Topo--;
        return aux;
    }
}

 

 

int main(int argc, char *argv[]) {
    setlocale(LC_ALL, "portuguese");
    TPilha *p = (TPilha*)malloc(sizeof(TPilha));
    TPilha_Inicia(p);

    int num[] = {1, 9, 8, 1, 8, 4, 4, 5};
    int count;

    for(count = 0; count < sizeof(num)/sizeof(int); count++){
	TPilha_Insere(p, num[count]);
	printf("%d ", p->dados[count]);
    }

    printf("\n\n");

    int aux;

    for(count = 0; count < 8; count++){
	aux = TPilha_Retira(p);
	printf("%d ", aux);
    }

    putchar('\n');
    system(" pause");

    return 0;
}

Era isso que vc queria fazer?

Sim, era isso, muito obrigado, valeu!!!

Link para o comentário
Compartilhar em outros sites

Em 07/05/2020 em 18:00, oito8bits disse:

//Bibliotecas
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
//Definindo uma constante com o tamanho exato que comporta o número do meu RA

#define tamanho 8

//Definindo uma estrutura
typedef struct {
    int dados[tamanho];
    int ini;
    int Topo;
} TPilha;

void TPilha_Inicia(TPilha *p) { 
    p->Topo = -1;
    
}
int TPilha_Vazia (TPilha *p) {
    if(p->Topo == -1) {
        return 1;
    }
    else 
    {
        return 0;
    }
}

int TPilha_Cheia (TPilha *p) {
    if(p->Topo == tamanho -1) {
        return 1;
    }
    else 
    {
        return 0;
    }
}

void TPilha_Insere (TPilha *p, int x) {
    if(TPilha_Cheia(p) ==1){
        printf("\n Erro: Pilha cheia");
    }
    else
    {
        p->Topo++;
        p->dados[p->Topo] = x;
    }
}

void imprimir(TPilha *p) {
    int i = 0;
    printf("\n\nImprimir:");
    for(i = 0; i < tamanho; i++) {
        printf("%d", p->dados);
    }
    
}

int TPilha_Retira (TPilha *p) {
    int aux;
    if(TPilha_Vazia(p) == 1){
        printf("\n Erro: A Pilha está vazia");
    }
    else
    {
        aux = p->dados[p->Topo];
        p->Topo--;
        return aux;
    }
}

 

 

int main(int argc, char *argv[]) {
    setlocale(LC_ALL, "portuguese");
    TPilha *p = (TPilha*)malloc(sizeof(TPilha));
    TPilha_Inicia(p);

    int num[] = {1, 9, 8, 1, 8, 4, 4, 5};
    int count;

    for(count = 0; count < sizeof(num)/sizeof(int); count++){
	TPilha_Insere(p, num[count]);
	printf("%d ", p->dados[count]);
    }

    printf("\n\n");

    int aux;

    for(count = 0; count < 8; count++){
	aux = TPilha_Retira(p);
	printf("%d ", aux);
    }

    putchar('\n');
    system(" pause");

    return 0;
}

Era isso que vc queria fazer?

 

Em 08/05/2020 em 00:08, Tiago_gad disse:

Sim, era isso, muito obrigado, valeu!!!

pode me ajudar novamente????
Esse mesmo código, só que antes reimprimir ao contrário no aux igual vc fez, jogar em uma segunda pilha e aí reimprimir na ordem contrária...

Link para o comentário
Compartilhar em outros sites

  • 2 semanas depois...
Em 06/05/2020 em 23:45, Tiago_gad disse:

Boa noite, alguém pode corrigir meu código e me ajudar?
Estou fazendo um código que empilha e depois desempilha reempilhando na ordem contrária, alguém me ajuda aí no que falta, segue p código em questão:

//Bibliotecas
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
//Definindo uma constante com o tamanho exato que comporta o número do meu RA

#define tamanho 8

//Definindo uma estrutura
typedef struct {
    int dados[tamanho];
    int ini;
    int Topo;
} TPilha;

void TPilha_Inicia(TPilha *p) { 
    p->Topo = -1;
    
}
int TPilha_Vazia (TPilha *p) {
    if(p->Topo == -1) {
        return 1;
    }
    else 
    {
        return 0;
    }
}

int TPilha_Cheia (TPilha *p) {
    if(p->Topo == tamanho -1) {
        return 1;
    }
    else 
    {
        return 0;
    }
}

void TPilha_Insere (TPilha *p, int x) {
    if(TPilha_Cheia(p) ==1){
        printf("\n Erro: Pilha cheia");
    }
    else
    {
        p->Topo++;
        p->dados[p->Topo] = x;
    }
}

void imprimir(TPilha *p) {
    int i = 0;
    printf("\n\nImprimir:");
    for(i = 0; i < tamanho; i++) {
        printf("%d", p->dados);
    }
    
}

int TPilha_Retira (TPilha *p) {
    int aux;
    if(TPilha_Vazia(p) == 1){
        printf("\n Erro: A Pilha está vazia");
    }
    else
    {
        aux = p->dados[p->Topo];
        p->Topo--;
        return aux;
    }
}

 

 

int main(int argc, char *argv[]) {
    setlocale(LC_ALL, "portuguese");
    TPilha *p = (TPilha*)malloc(sizeof(TPilha));
    TPilha_Inicia(p);
    
    TPilha_Insere (p, 1);
    printf("\n Empilhou o 1");
    TPilha_Insere (p, 9);
    printf("\n Empilhou o 9");
    TPilha_Insere (p, 8);
    printf("\n Empilhou o 8");
    TPilha_Insere (p, 1);
    printf("\n Empilhou o 1");
    TPilha_Insere (p, 8);
    printf("\n Empilhou o 8");
    TPilha_Insere (p, 4);
    printf("\n Empilhou o 4");
    TPilha_Insere (p, 4);
    printf("\n Empilhou o 4");
    TPilha_Insere (p, 5);
    printf("\n Empilhou o 5");
    
    void imprimir(TPilha *p) ;
    
    system("\n pause");
    
    int aux;
    
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    aux = TPilha_Retira(p);
    printf("\n Desenpilhou: %d", aux);
    
    system(" pause");
    
    return 0;
}

Olá thiago_gad, você conseguiu fazer ??poderia me ajudar ? é o mesmo mapa... falta 3 dias ?

Link para o comentário
Compartilhar em outros sites

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

  • Quem Está Navegando   0 membros estão online

    • Nenhum usuário registrado visualizando esta página.
×
×
  • Criar Novo...