ProAnswers.org

algorithm Pop an element out of the stack

algorithm Pop an element out of the stack

[b]Popping out of a stack

Array implementation[/b]

    
int pop(stack *stk){
    if(stk -> top == -1) //Stack is empty
        return -1; //unsuccessful
    else{
        stk -> top = stk -> top - 1;
        return 1; //successful
    }
}

[b]Linked list implementation[/b]

    
int pop(stack *stk){
    SNode *snode;
    if(stk -> top == NULL) // Stack is empty
        return -1; //Unsuccessful
    else{
        snode = stk -> top;
        stk -> top = stk -> top -> next;
        free(snode);
        return 1; //Successful
    }
}