0

I am trying to implement an array based stack in C. My stack is supposed to have two parameters, top (the number of the top element in the array), and array (the array itself). My implementation follows.

typedef struct
    {
    char array[20];
    int top;
    }
    stack;

stack mystack;

int Push(char,stack);
char Pop(stack);
char Top(stack);
int isFull(stack);

char input;
char save;

void main()
    {
    mystack.top = -1;

    printf("Please input the characters you would like in your stack \n     

    while(input != '^')
            {
            Push( (scanf("%c",&input)) , mystack );

            if (isFull(mystack) == 1)
            printf("Your Stack is full, please input '^'\n");
            }

    char junk;
    scanf("enter any character to continue %c",&junk);

    while(mystack.top != -1)
            {
            printf("%c \n",Pop(mystack));
            }
    scanf("enter any character to terminate the program",&junk);
    }

int Push(char charpush,stack stackpush)
    {
    if(stackpush.top >=20 )
            return -1;
    else
            {
            stackpush.array[stackpush.top + 1] = charpush;
            stackpush.top = stackpush.top +1;
            return 0;
            }
    }
char Pop(stack stackpop)
    {
    if (stackpop.top != -1)
            {
            save = stackpop.array[stackpop.top];
            stackpop.top = stackpop.top-1;
            return save;
            }
    }
char Top(stack stacktop)
    {
    if (stacktop.top != -1)
            return stacktop.array[stacktop.top];
    }
int isFull(stack stackisfull)
    {
    if (stackisfull.top = -1)
            return 0;
    else if (stackisfull.top >= 20)
            return 1;
    else return -1;
    }

Currently my program accepts characters from the user, but the program automatically terminates when '^' is entered. It doesn't display the stack, and it doesn't do anything if characters come through the input and the stack is already full.

Please let me know if i need to be more specific or any further information is needed.

Sangrida
  • 1
  • 1

2 Answers2

2

You've got a whole lot of problems to correct...

  • You have misunderstood scanf greatly

    • It doesn't return what it read
    • It doesn't accept a prompt
    • When reading from the terminal, it doesn't "see" anything until return is pressed
  • What does your method Pop() return when the stack is empty?

  • What does your method Top() return when the stack is empty?

  • Why did you write while(mystack.top != -1)? Would it make more sense to write while (!isEmpty(mystack)) and then write an isEmpty method?

  • You didn't initialize input - do yo know what is in it at the start?

  • Your indention "scheme" makes my head hurt. :)

John Hascall
  • 9,176
  • 6
  • 48
  • 72
1

In addition to the points @John Hascall has mentioned, C is a pass by value language. Meaning for every function call the arguments you provide are local in scope see this other stackoverflow post.

Having your global mystack variable (not the best practice either) will work but not how you are currently using it. By passing mystack to each function the changes are only visible on the argument used thereby defeating the purpose of having that global.

I've made the minor edits to indentation and logical errors but the main change was editing your functions to not take a "stack" argument and use your global:

#include <stdio.h>

typedef struct
{
    char array[20];
    int top;
} stack;

stack mystack; // your global
int Push(char); // remove "stack" arg for each stack-utility function
char Pop(void);
char Top(void);
int isFull(void);

char input;
char save;

// main as returning int and excepting argc/*argv[]
int main(int argc, char *argv[])
{
    mystack.top = -1;

    printf("Please input the characters you would like in your stack \n"); 

    while(input != '^')
    {
        // by including scanf inside the function call return is passed
        scanf("%c",  &input);
        Push( input );

        if (isFull() == 1)
            printf("Your Stack is full, please input '^'\n");
    }

    char junk;
    // scanf will not print
    printf("enter any character to continue\n");
    scanf("%c",&junk);

    while(mystack.top != -1)
    {
        printf("%c \n",Pop());
    }
    // same as last comment
    printf("enter any character to terminate the program\n");
    scanf("%c",&junk);
}

int Push(char charpush)
{
    if(mystack.top >=20 )
        return -1;
    else
    {
        mystack.array[mystack.top + 1] = charpush;
        mystack.top = mystack.top +1;
        return 0;
    }
}

char Pop(void)
{
    if (mystack.top != -1)
    {
        save = mystack.array[mystack.top];
        mystack.top = mystack.top-1;
        return save;
    }
        // return has to match declaration type
    return 0;
}

char Top(void)
{
    if (mystack.top != -1)
        return mystack.array[mystack.top];
    // same as last comment
    return 0;
}

int isFull(void)
{
    // you were assigning not comparing
    if (mystack.top == -1)
        return 0;
    else if (mystack.top >= 20)
        return 1;
    else return -1;
}
Community
  • 1
  • 1
tijko
  • 7,599
  • 11
  • 44
  • 64