AdBlock Detected!

Our website is made possible by displaying ads to our visitors. Please support us by whitelisting our website.

Computer Neek

What is a stack

A stack is just a collection of data in a linear format that is added from the bottom to the top but items are removed from top to bottom. This is called LIFO - Last in first out.

There are five operations of a stack

Push(data)
pop()
peak()
is_empty()
is_full()

Push(data)

In a stack, there is a top. This top represents the item that has recently been added to the stack. An array is usually used to hold the items in it but a list can be used too. So in programming terms, if you add "item 1" and "item 2" to the stack, the current top position would be at index position 1 because you've added two items(remember that indexes start at 0 and not 1). "item1" is at index 0 and "item 2" is at index position 1. If you were to use an array in this example and wanted to add another item to the stack then you would first increment the top so it goes to the next available space in the array and then add the item to that array index. Look at the example below that illustrates this.

Example

//before adding an item to the array
     
top = 1;
     
//after wanting to add an item to the array
     
string addedItem = "item 3";
     
top = top + 1;
     
arrayItems[top] = addedItem;

In this example, the top variable before was 1. We then set the string variable "addedItem" to "item 3". After that we increment the top by one. This is so it goes to the next empty space in the array. After that, we set the string variable "addedItem" to the index position if the array "arraItems". The top position is now 2.

Pop()

The pop() operation removes the top element from the stack. It does this by setting the array with the top index as null or ("") and decreasing the top count by one.

Example

//before removing an item 
top = 2;
//after removing 
arrayItems[2] = "";
top = top - 1;
// top now equals 1

Peak()

Peak works by outputting the top item of the stack without increasing or decreasing the top count.

Example

//outputting the top item on the stack
Console.WriteLine(arrayItems[top]);
     

is_full()

is_full() just check if the stack is full. In a program, a static stack will have a maximum amount of data to be able to be stored. The program can check if the top has equals the data size - 1(the maximum amount to be able to be stored). It will return is full or if it's not full, it won't return is full.

Why would it be full if it equals the data size - 1?

First, you have to know why we set the top at the very begining to -1. Well, it's because the top is set at -1 to start with. This is because the top increments first before setting the data to the stack. This is because arrays and lists start at index 0. The issue with 0 being a starting point is that if we are incrementing the top first, which would make the top = 1, and then set it to that index position, there will be space missed. The space missed would be index position 0. So if we set the index position to -1 first and then increment by 1, the next item to be added to the stack will be at index position 0 which is the start of an array/list.


Because we start at -1 and the first data set in the stack will be at index position 0, this means that the stack size is actually one less then that at the dataSize(look at the variablle "dataSize below"). We humans start counting at 1 but when working with lists/arrays the count starts at 0. This means that the lists/array count will always be one less than ours. This is why we minus 1 when we check if the stack is full.


int dataSize = 5;
         
if(top == dataSize - 1)
{
         
    Console.WriteLine("Full");
}

is_empty()

is_empty() checks if the stack is empty.It does this by checking if the array at the index position of the value of "top" is empty.

A stack algorithm C#

class Stack
    {
        public string[] items;
        private int size;
        private int top = -1;
 
        public Stack(int stackSize)
        {
            this.size = stackSize;
            this.items = new string[this.size];
 
 
        }
        //adding to stack
        public void push(string itemsAdd)
        {
            this.top++;
            this.items[this.top] = itemsAdd;
        }
        //removing from stack
        public void pop()
        {
            string removing = this.items[this.top];
            top--;
 
 
        }
        //viewing stack
        public void peak()
        {
            string peak = this.items[this.top];
            Console.WriteLine(peak);
        }
    }
 
    static void Main(string[] args)
    {
        //setting stack size
        Console.WriteLine("What is the size of your stack");
        int stackSize = int.Parse(Console.ReadLine());
        Stack stack = new Stack(stackSize);
        //asking user for which operation
        Console.WriteLine("Do you want to peak pop or push");
        string options = Console.ReadLine().ToLower();
        if(options == "peak")
        {
            stack.peak();
        }
        else if(options == "pop")
        {
            stack.pop();
 
        }
        else if(options == "push")
        {
            Console.WriteLine("How many numbers?");
            int amount = int.Parse(Console.ReadLine());
            stack = new Stack(amount);
            for (int i = 0; i < amount; i++)
            {
                Console.WriteLine("Enter in a number");
                string num = Console.ReadLine();
                stack.push(num);
 
            }
        }
 
    } 

Overall notes

Push()-- adds items to the top of the stack

Pop()-- removes items at the top of the stack

Peak()-- views the item at the top of the stack

is_full() -- checks if the stack is full

is_empty() -- checks if stack is empty

A stack is a LIFO. Last in first out

To read about Queues click here or to read about different topics, click here