ProAnswers.org

algorithm Pushing an element onto the stack

algorithm Pushing an element onto the stack

[b]Pushing an element onto a stack

Array implementation[/b]

    
void push(stack *stk, int elem){
    if(stk -> top == MAX_SIZE)
        return;
    else{
        stk -> top = stk -> top + 1;
        stk -> SArray[stk -> top] = elem;
    }
}

[b]Linked list implementation[/b]

    
void push(stack *stk, int elem){
    SNode *snode;
    snode = (SNode *)malloc(sizeof(SNode));
    snode -> data = elem;
    snode -> next = stk -> top;
    stk -> top = snode;
}