Stacks and Queues Array implementation

Stacks and Queues Array implementation

Data Structures are very important for anyone who's aiming to crack interviews of Big Tech Companies. Stacks and Queues are one of the important Data Structures, so let's start learning Stacks and Queues without leaving any stone unturned.

Refer to the full code in the following Github repository: github.com/kritikaparmar-programmer/DSA_CP_..

Stacks

Let's take a simple example of a pile of books that are placed on each other. Now imagine what will you do if you want to add one more book to the compilation, easy to answer right? We will put that book on the top of the pile of books. And similarly what if you want to take out a book, normally we will do it by taking out the book that's been kept on the top of the compile. And that's what stack is all about i.e. LIFO(Last In First Out). And what you just imagined are the two main functions of the Stack i.e. PUSH and POP. So now coming to the part where you have to implement in code what you just learned. Let's imagine an array which we declared of size N. Here we will also declare a top variable that will increase with the addition of an element in stack and decrease with the deletion of the element in a stack just as we learned above. The next step will be to set-top variable initially to -1 when the stack is empty.

PUSH & POP

Suppose we want to add an element 5 to the stack, so first, we will increment the top value, and then we will simply assign the array top value to the element we want to insert. Remember here, if our stack is full then it is obvious that the top value would be N-1 and thus initially we have to check whether the Stack is full or there is any space left. And we will do that by just adding a condition. This is all about what the Push function consists of. Take a look at the code below :

bool Stack :: push(int x){
    if(top >= MAX-1){
        cout<<"Stack Overflow\n";
        return false;
    }
    else{
        top += 1;
        a[top] = x;
        cout<<x<<" "<<"is pushed into Stack\n";
        return true;
    }
}

So now when we have performed the push function and we wish to delete an element from the stack then we will first define a temp that will store the stack top value, then we have to decrement the top as I mentioned above and return temp. Here also we will have to keep a check at the condition that if our stack is Empty or Full. If our stack is empty then we cannot perform any pop function and for that, we will have to add a condition at the beginning. See the code below:

int Stack :: pop(){
    if (top < 0){
        cout<<"Stack is Empty\n";
        return 0;
    }
    else{
        int x;
        x = a[top];
        top -= 1;
        return x;
    }
}

PEEK

Now that we have performed push and pop functions perfectly, the next function is peeked, which is the simplest function. In this, we just have to return the element which is currently at the top of the stack and it's done. Code:

int Stack::peek(){
    if (top < 0){
        cout<<"Stack is Empty\n";
        return 0;
    }
    else{
        int x;
        x = a[top];
        return x;
    }
}

DISPLAY

Now when we know how to add, remove, and take a look at the top of the stack. The next and the last step is to display them. Which one can simply do by using for a loop. The below code explains the display function:

void Stack::display(){
    if (isEmpty()){
        cout<<"Stack is Empty\n";
        return ;
    }
    cout<<"Stack element: \n";
    for(int i=top; i>=0; i--){
        cout<<a[i]<<"\n";
    }
}

Queues

Everyone in their life has encountered big waiting queues. So if we talk about queue what's the main procedure that everyone follows like if one wants to enter the queue he/she will enter from the last and will leave the queue from the beginning. And similarly are the main functions of queue i.e ENQUEUE and DEQUEUE. And Queue follows FIFO i.e. First In First Out. So now let's take a look at the implementation of the Queues. Declare an array of size N. Declare front and rear variables that will increase/decrease with the addition/deletion of an element in the queue. Then we will initialize these variables with -1.

Enqueue & Dequeue

Whenever we want to insert an element in a queue then we insert it at the end of the queue for this we will first check that our queue is empty i.e. front is -1 and if for this we will first increment the front to 0. And then, we will simply increment rear whenever we will insert an element.

void Queue::insert(int data){
    if(isFull()){
        cout<<"Queue Overflow\n";
        return;
    }
    if(front == -1)
        front = 0;
    rear = rear + 1;
    queue_arr[rear] = data;
}

For the delete operation, we will simply store the front value in a variable and increment front. Later we can return that variable.

int Queue::del(){
    int data;
    if(isEmpty()){
        cout<<"Queue Underflow\n";
        exit(1);
    }
    data = queue_arr[front];
    front += 1;
    return data;
}

PEEK & DISPLAY

The below code explains the peek & display function.

int Queue::peek(){
    if(isEmpty()){
        cout<<"Queue Underflow\n";
        exit(1);
    }
    return queue_arr[front];
}

void Queue::display(){
    if(isEmpty()){
        cout<<"Queue Underflow\n";
        return ;
    }
    cout<<"Queue is: \n";
    for(int i=front; i<=rear; i++){
        cout<<queue_arr[i]<<"\n";
    }
}

Congratulations

So now you know the theory and implementation of both the data structures. Hope you enjoyed the learning :)