Tuesday 23 February 2016

Procedure 6.1 (Push Item in a Stack.)


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>

int maxstk, top=-1, i;

void push(int stack[], int item)
{
    top += 1;
    if(top == maxstk)
    {
        printf("OVERFLOW\n");
        return;
    }
    stack[top] = item;
    for(i = 0; i<=top; i++)
        printf("%d  ", stack[i]);
    printf("\n");
}

int main()
{
    printf("Please, print the max stack size.\n");
    scanf("%d", &maxstk);
    int stack[maxstk], item;
    while(1)
    {
        printf("Please, print an item.\n");
        scanf("%d", &item);
        push(stack, item);
        if(top == maxstk) break;
    }
    return 0;
}

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.