megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Queues & Stacks

Let's look at the differences between the two data structures:

  1. Queues: First-In First-Out
  2. Stacks: Last-In Last-Out

Queues

This follows a First-In First-Out processing order i.e. the first element added to a queue will be processed first. A queue should support two operations:

  • Enqueue
  • Dequeue

Enqueue

Adds the element to the tail of a queue. The tail position gets incremented.

Dequeue

Removes the first element of a queue i.e. the head element. Once, it's removed, the subsequent element becomes the new head element of the queue. The position of the new head element gets incremented and the previous one is assigned a negative integer like -1 or some garbage value.

Implementation of a standard queue using C++:

class Queue {
    private:
        int pos;
        vector<int> data;

    public:
        Queue() {
            pos = 0;
        }

        bool enqueue(int value) {
            data.push_back(value);
            return true;
        }

        bool dequeue() {
            if(isEmpty()){
                return false;
            }
            pos++;
            return true;
        }

        int front() {
            return data[pos];
        }

        bool isEmpty() {
            return pos >= data.size();
        }
}

In terms of memory management, a standard Queue is quite inefficient and incapable of handling dynamic memory.

Stacks

This follows a Last-In First-Out processing order i.e. the last element added to a queue will be the first to be removed. Just like queues, it has two simple operations:

  • Push
  • Pop

Push

Each element is pushed towards the end of the stack. Think of it as a card deck where you stack a card on top of another card.

Pop

It removes the most recent element i.e. the newly added element from the stack.

Implementation of a stack using C++:

class Stack {
    private:
        vector&ltint> data;
    public:
        void push(int value) {
            data.push_back(value);
        }

        bool isEmpty() {
            return data.empty();
        }

        int top() {
            return data.back();
        }

        bool pop() {
            if(!isEmpty()) {
                data.pop_back();
                return true;
            }
            else {
                return false;
            }
        }
}

Unlike queues, stacks are easier to implement and pretty efficient at managing dynamic memory.

Oh, if you ever get to use these, don't worry about implementing them, nearly all programming languages have their own implementations of stack and queue that comes with it's own standard library.

Extract a specific folder from a zipped archive

First, you need to view what's inside of the .zip archive:

unzip -v archive.zip

Once, you've found the folder you wanted to extract, just type this:

unzip archive.zip "folder_to_extract/*" -d .

Select all elements except the current element

If you don't want the current element to be selected in an array of elements that belongs to same class or type, just use the .not() method like the example below:

$(".btn").click(function(){
    $(".btn").not(this).text('selected');
});

The above code will change the text for all buttons except the current element.

Remove patterns from multiple files

If you wanted to remove a specific pattern in a list of files, like the ones below:

23_2020_03_01_article-three.md
22_2020_02_01_article-two.md
21_2020_01_01_article-one.md

You can simply do that using Regular Expressions and the rename tool:

rename 's/[0-9]+_[0-9]+_[0-9]+_[0-9]+_//' *.md

Now, the desired output should look like this:

article-three.md
article-two.md
article-one.md

This should come in handy if you're lazy to rename each file manually! :)

Rename extensions of multiple files

In this example, we're going to change a list of .txt files to .md files:

#!/bin/bash

shopt -s nullglob
files=($(ls -v *.txt))

for file in "${files[@]}"
do
    mv ${file} ${file}.md
done

s You can use modify this script to rename any extension you want.

Checkout branch

Want to create a new branch in your project? Simple, just do this:

git checkout -b new_branch

By doing this, you'll automatically be shifted to a new branch of your project. To check which branch you're working on, type this:

git branch

And you should be able to see your current branch marked with a *:

master
* new_branch

View the filesize in human-readable format

Wanted to view the size of a file in terminal but don't understand the number of bytes displayed? No worries, just type this command and it'll display the size of the in human-readable format:

du -sh filename.ext

Check RAM and disk space

RAM space

Type the following command to view available memory in your system:

free -h

Disk space

Type the following command to view available disk space in your system:

df -h