DEV Community

Cover image for Introduction to Stacks
TheCSPandz
TheCSPandz

Posted on • Updated on

Introduction to Stacks

What are Stacks?

A Stack is a Linear Data Structure that follows a process of FILO or First In - Last Out.

To understand how a stack works, you can think of it as a pile of plates. If you were to add a plate, you would add it to the top of the pile, and if you were to remove a plate, you can only remove the top plate.

We will be using an array called stack for the demonstration and a variable top initialized as -1 to indicate an empty stack and let us assume that the stack has to be of size 5. The various operations that can be performed on a stack are:

Push

The push operation adds the value to the top of the stack. If the top value is the same as the size of the stack, if a push operation is attempted, it will lead to Stack Overflow indicating that you're trying to add a value past the allocated space. The function for the push operation:

def push():
    #let us assume the stack to be of size 5
    global top
    global stack 

# value from 0 to size-1, as top begins from -1
    if(top == 4):  
        print("Stack Overflow")
    else:
        a = int(input("Enter value"))
        stack.append(a)
        top+=1
        print(f"{a} pushed to stack")
Enter fullscreen mode Exit fullscreen mode

Pop

The pop operation removes the value at the top of the stack. If the value of top is -1 (ie, the stack is empty) and the pop operation is attempted, it will lead to Stack Underflow indicating that you're trying to remove a value from an empty stack. The function for the pop operation:

def pop():
    global top
    global stack 

    if(top == -1):
        print("Stack Underflow")
    else:
        print(f"{stack[top]} has been popped")
        stack.pop(top)
        top -= 1
Enter fullscreen mode Exit fullscreen mode

Peek

The Peek Operation displays the value at the top of the stack. The function for the peek operation is:

def peek():
    global top
    global stack 

    if(top == -1):
        print("Stack is empty")
    else:
        print(f"Top: {stack[top]}")

Enter fullscreen mode Exit fullscreen mode

State of Stack

This function combines the three operations is_empty() which tells you whether or not the stack is empty, is_full() which tells you whether or not the stack is full. The function for the operation is:

def state_of_stack():
    if(top==4):
        print("Stack is full")
    elif(top == -1):
        print("Stack is empty")
    else:
        print("Stack is partially empty")
Enter fullscreen mode Exit fullscreen mode

Stack Process

To simulate the functioning of the stack, the complete code is given below

import array as ar

## Global variables

stack = ar.array('i', [])
top = -1


def push():
    global top
    global stack 

    if(top == 4):
        print("Stack Overflow")
    else:
        a = int(input("Enter value"))
        stack.append(a)
        top+=1
        print(f"{a} pushed to stack")


def pop():
    global top
    global stack 

    if(top == -1):
        print("Stack Underflow")
    else:
        print(f"{stack[top]} has been popped")
        stack.pop(top)
        top -= 1


def peek():
    global top
    global stack 

    if(top == -1):
        print("Stack is empty")
    else:
        print(f"Top: {stack[top]}")


def state_of_stack():
    if(top==4):
        print("Stack is full")
    elif(top == -1):
        print("Stack is empty")
    else:
        print("Stack is partially empty")


## Driver Code
while(True):
    print("1.Push\n2.Pop\n3.Peek\n4.State of Stack\n5.Exit")

    ch = int(input())

    if(ch==1):
        push()
    elif(ch==2):
        pop()
    elif(ch==3):
        peek()
    elif(ch==4):
        state_of_stack()
    else:
        break
Enter fullscreen mode Exit fullscreen mode

Note

Since we will be modifying the stack in the functions, we use the global value to modify the variables that are being used

Top comments (0)