Tuesday, 23 February 2016

Procedure 6.2 (Pop Item From 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 top, i, item;

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

int main()
{
    printf("Please, print the max stack size.\n");
    scanf("%d", &top);
    int stack[top], command;
    printf("Please, print %d element.\n", top);
    for(i=0; i<top; i++)
        scanf("%d", &stack[i]);
    while(1)
    {
        printf("For delete last item enter 1.\n");
        printf("For end the task enter 2.\n");
        scanf("%d", &command);
        if(command == 2) break;
        else
        {
            pop(stack);
            if(top<0) break;
        }
    }
    return 0;
}

0 comments:

Post a Comment

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