How to Insert a string into the data stack

Asked

Viewed 1,647 times

0

I need to make a stack that does a push, pop and print. So I created the functions and this works. But I wanted to add a String to my stack. Possibly a number and time or just a string. But I can’t get either. Could someone help me, please?

#include <stdio.h>
#define MAXSIZE 5  

struct pilha
{
    int pilhaa[MAXSIZE];
    int top;

};

typedef struct pilha PILHA;
PILHA p;

void push(void);
int  pop(void);
void display(void);

void main ()
{
    int escolha;
    int opcao = 1;
    p.top = -1;

    printf ("Operacoes da Pilha\n");
    while (opcao)
    {
        printf ("------------------------------------------\n");
        printf ("      1    -->    PUSH               \n");
        printf ("      2    -->    POP               \n");
        printf ("      3    -->    DISPLAY               \n");
        printf ("      4    -->    EXIT                   \n");
        printf ("------------------------------------------\n");

        printf ("Escolha uma opcao!\n");
        scanf    ("%d", &escolha);
        switch (escolha)
        {
        case 1:
            push();
            break;
        case 2:
            pop();
            break;
        case 3:
            display();
            break;
        case 4:
            return;
        }
        fflush (stdin);
        printf ("(Digite 0 para 'SAIR' ou 1 para 'CONTINUAR')?\n");
        scanf    ("%d", &opcao);
    }
}
/* Função para adicionar um elemento à pilha */
void push()
{
    char value[20];

    if (p.top == (MAXSIZE - 1))
    {
        printf ("Pilha Cheia!\n");
        return;
    }
    else
    {
        printf ("Digite o numero no formato INTEIRO \n");
        scanf ("%s", value);


        p.top = p.top + 1;
        p.pilhaa[p.top] = value;
    }
    return;
}
/* Função para excluir um elemento da pilha */
int pop()
{
    int num;
    if (p.top == - 1)
    {
        printf ("Pilha Vazia!\n");
        return (p.top);
    }
    else
    {
        num = p.pilhaa[p.top];
        printf ("Elemento a ser retirado eh: = %d\n", p.pilhaa[p.top]);
        p.top = p.top - 1;
    }
    return(num);
}
/* Função para exibir o status da pilha */
void display()
{
    int i;
    if (p.top == -1)
    {
        printf ("Pilha Vazia\n");
        return;
    }
    else
    {
        printf ("\n Os status da pilha é: \n");
        for (i = p.top; i >= 0; i--)
        {
            printf ("%s", p.pilhaa[i]);
        }
    }
    printf ("\n");
}
  • But is the stack supposed to have strings or numbers? It seems that in some situations it is trying to use one and the other

  • At first I did using whole. Then I changed only the stack (push) to string.. But I stuck and posted here to see if someone gives me a light.

  • But is it to be with numbers or strings ? With numbers is easier

  • In C if you declare an array of one type, you cannot store another type in the array. There are a few ways around this, one of which would be to have a void* array BUT this is NOT good practice and can give you a lot of headache. You would like to have a stack of strings only?

1 answer

0

#include <stdio.h>
#define STACK_MAXSIZE 5
#define STRING_MAXSIZE 10

struct pilha
{
    char pilhaa[STACK_MAXSIZE][STRING_MAXSIZE];
    int top;

};

typedef struct pilha PILHA;
PILHA p;

void push(void);
void  pop(void);
void display(void);

int main ()
{
    int escolha;
    int opcao = 1;
    p.top = -1;

    printf ("Operacoes da Pilha\n");
    while (opcao)
    {
        printf ("------------------------------------------\n");
        printf ("      1    -->    PUSH               \n");
        printf ("      2    -->    POP               \n");
        printf ("      3    -->    DISPLAY               \n");
        printf ("      4    -->    EXIT                   \n");
        printf ("------------------------------------------\n");

        printf ("Escolha uma opcao!\n");
        scanf    ("%d", &escolha);
        switch (escolha)
        {
        case 1:
            push();
            break;
        case 2:
            pop();
            break;
        case 3:
            display();
            break;
        case 4:
            return 0;
        }
        fflush (stdin);
        printf ("(Digite 0 para 'SAIR' ou 1 para 'CONTINUAR')?\n");
        scanf    ("%d", &opcao);
    }

    return 0;
}
/* Função para adicionar um elemento à pilha */
void push()
{
    char value[STRING_MAXSIZE];

    if (p.top == (STACK_MAXSIZE - 1))
    {
        printf ("Pilha Cheia!\n");
        return;
    }
    else
    {
        printf ("Digite a string \n");
        scanf ("%s", value);

        p.top = p.top + 1;

        for (int i=0; i<STRING_MAXSIZE; i++) {
          p.pilhaa[p.top][i] = value[i];

          if(value[i] == '\0') break;
        }
    }
} 
/* Função para excluir um elemento da pilha */
void pop()
{
    char num[STRING_MAXSIZE];
    if (p.top == - 1)
    {
        printf ("Pilha Vazia!\n");
    }
    else
    {
        printf ("Elemento a ser retirado eh: = %s\n", p.pilhaa[p.top]);
        p.top = p.top - 1;
    }
}
/* Função para exibir o status da pilha */
void display()
{
    int i;
    if (p.top == -1)
    {
        printf ("Pilha Vazia\n");
        return;
    }
    else
    {
        printf ("\n Os status da pilha é: \n");
        for (i = p.top; i >= 0; i--)
        {
            printf ("%s\n", p.pilhaa[i]);
        }
    }
    printf ("\n");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.