Search code examples
cdata-structuresstructstackfunction-definition

Difficulties with implementation of stack


I am trying to get into structures, but i got stuck. Here I tried to implement the main functions of the stack:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#define N 10

    struct docEdit 
{
    char text[20];
    int StrNum;
};


struct Stack {
    struct docEdit items[N];
    int top;
};
    
void pushstack(struct Stack* st, struct docEdit myEvent1) {
if (st->top == N - 1) {
    printf("Stack is full");
    return;
    }   
    
    st->items[st->top++] = myEvent1;
}

    void popstack(struct Stack* st) {
    if (st->top == -1) {
        printf("Stack is empty");
        return;
    }
        st->top--;
    }

    int emptystack(struct Stack* st) {
        return st->top = 0;
    }

    int sizestack(struct Stack* st) {
        return st->top;
    }

    (//function data type//) top(struct Stack* st) {
        return st->items[st->top];
    }



    int main() {
    setlocale(LC_ALL, "");
    struct Stack st;
    st.top = -1;
    struct docEdit myEvent1 = {"string 1", 1};

    pushstack(&st, myEvent1);
    popstack(&st);

    return 0;
    }

There are two questions: why does it write that the st variable is not initialized and what data type should the top function have? I would also be very grateful if you point out my mistakes and inaccuracies.


Solution

  • The function definitions contradict each other. For example the function popstack

    void popstack(struct Stack* st) {
    if (st->top == -1) {
        printf("Stack is empty");
        return;
    }
        st->top--;
    }
    

    states that the stack is empty when st->top == -1. On the other hand the function emptystack (that maybe has a typo and uses the assignment operator = instead of the comparison operator == That is unclear whether the function cleans the stack or reports whether it is empty)

    int emptystack(struct Stack* st) {
            return st->top = 0;
        }
    

    states that the stack is empty when st->top is equal to 0.

    Or the function sizestack

    int sizestack(struct Stack* st) {
            return st->top;
        }
    

    states that the size is equal to the value st->top. It is evident that the maximum size of the stack is equal to N due to the declaration

    struct docEdit items[N];
    

    However the function pushstack

    void pushstack(struct Stack* st, struct docEdit myEvent1) {
    if (st->top == N - 1) {
        printf("Stack is full");
        return;
        }   
        
        st->items[st->top++] = myEvent1;
    }
    

    states that the stack is full when st->top is equal to N - 1.

    That is you should decide whether the initial value of the data member top must be equal either -1 or 0 and adjust correspondingly all function definitions.

    As for the function top then it should return a pointer to an element of the stack.

    struct docEdit * top( struct Stack* st) {
            if ( st->top != 0 )
            return st->items + st->top - 1;
            else
            return NULL; 
        }
    

    Using the returned pointer you can change values of the element on the top of the stack. In this function I suppose that when the stack is empty then st->top is equal to 0.