Skip to content

Latest commit

 

History

History
86 lines (80 loc) · 1.87 KB

Stack.md

File metadata and controls

86 lines (80 loc) · 1.87 KB

Stacks

A stack is a list with the restriction that inserts and deletes can only be performed on the top of the list.

Stack Array Version

typedef Struct{
int tos;
int stk[size];
} Stack;

tos(top of stack)

The index where the top most/left most value of an array. Starts at index -1.

stk

The actual array of the Stack

Vertical version

vertical_stack

Horizontal version

horizontal_stack

Stack Operations

Push

The function increases the tos by 1 and adds a value in that index.

void push(Stack* stack, int val){
    stack->tos += 1;
    stack->stk[stack->tos] = val;
}

push_gif

Pop

The function outputs the value in tos and moves tos lower by 1.

int pop(Stack* stack){
    int data = stack->stk[stack->tos];
    stack->tos -= 1;
    return data;
}

pop_gif

Even though the numbers are not removed in the array because its above tos, we cant really see them anymore.

IsFull

The function checks if the stack is full by checking if tos is equal to the last index.

bool isFull(int tos){
    bool full = false;
    if(tos == size - 1){
        full = true;
    }
    return full;
}

IsEmpty

The function checks if the stack is empty by checking if tos is equal to -1.

bool isEmpty(int tos){
    bool empty = false;
    if(tos == -1){
        empty = true;
    }
    return empty;
}

Peek

Function will output the value in tos but will not move tos.

int peek(Stack stack){
    int data = stack.stk[stack.tos];
    return data;
}

References

Linked List Version

Stack Linked List

Examples

Binary Convertion
PostFix Calculator
InFix to PostFix Converter

Back To Main