Asking code suggestions about data structure and algorithm

37 views Asked by At

I recently posted an entry here in stackoverflow for a specific problem I've encountered after run and did asked a few. It's a similar one but this time I need your thoughts and comments to atleast better up my programming skills. For those who didn't know I'm new at coding in C so some conventions in this language were new to me due to the fact that I got introduce to high level language such as Python and Java first. Suggestions and criticisms are highly appreciated.

The code below is about stacks from Data Structure and Algorithms and I wrote this code to understand how the algorithm work.

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5

int y = 5;
int z = 10;
struct stack
{
    int a[MAXSIZE];
    int Top;
};

void push(struct stack *pU)
{
    int size;
    const int x = 4; // Each element is 4 bytes 

    pU->Top = -1;
    size = sizeof(pU->a[pU->Top]) / x;

    if(size > MAXSIZE)
    {
        printf("stack is full\n");
        exit(2);
    } else {
    /*
     * First Element
     */
        pU->a[++pU->Top] = y;
        printf("Top element: %d\n", pU->a[pU->Top]);

    /*
     * Second Element
     */
    pU->a[++pU->Top] = z;
    printf("New top element: %d\n", pU->a[pU->Top]);
    }

    /*
     * Count index of array 
     */
    for(int i = -1; i <= pU->Top; ++i)
    {
        if(pU->a[i] >= 0 && pU->a[i] <= 99)
    {
        size = size + 1;

        if(size > MAXSIZE)
        {
                printf("Stack is full\n");
            exit(2);
        } else if(size == size){
            printf("Number of elements in array: %d\n", size);
        }
    }
    break;
    }

}

void pop(struct stack *pO)
{
    int size;
    const int x = 4;

    for(int i = 0; i < 1; ++i)
    {
         pO->Top = pO->Top - 1;  // Decrement Top
     printf("Recently added stack removed. Current stack is: %d\n", pO->a[pO->Top]);
    }
}

int main()
{
    struct stack *M;
    
    push(M);
    pop(M);
}

0

There are 0 answers