Skip to main content

Stack implementation using C#

A stack is a data structure.

And the definition as on Wikipedia:

In computer science, a stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed.

Data structures are so simple if you can understand everything in it. They are just another programs which are tied together with small pieces of implementation.
An Array follows LIFO(Last In First Out) or FIFO (First In Last Out) order. So, an item inserted first will be the first to remove, if you wish to remove it.

Stack Operations:

Push(): To insert an item to the stack.
Pop(): To remove an item off of stack.

Implementation details:
To implement we need a pointer to point to the top of the stack, and an object array called data or whatever variable name which will hold the data that is pushed to stack.

Note: The stack that we're implementing here in this blog post is a finite one.

Per the note above, we will have to provide a capacity of the stack(to be a finite stack) during object creation.

The constructor here is just to help with the initial setup to create an array and to set the pointer’s initial position.

Public ArrayStack(int capacity)
     data = new obj[capacity];
     pointer = 0;

A push operation on stack requires the element to be pushed. Once we add element on to the stack we'll have to increment the pointer to point to the top of the stack.

Public void Push(object o)
          data[pointer] = o;

The pop operator doesn't need any input but we'll give the caller what is the element removed now and again the pointer will be decremented to stay on top of the stack.

Public object Pop()
    If(IsEmpty()) return null;
    object o = data[pointer];
    data[pointer] = null;
    return o;

IsEmpty() used in Pop method is a private method just to check if the array is empty or not. It returns a boolean.

Public bool IsEmpty()
    return pointer == 0;

That's it. So simple.

Here is everything to wrap up.


Popular posts from this blog

Losing precision after multiplication in SQL Server

Yesterday I was doing a little multiplication operation in SQL SERVER. The computation involves multiplication of two decimal numbers. A point to note here is that I've to be very careful while performing the calculations because it's a monetary thing. So, Whatever the calculation made, it must be very accurate such that I shouldn't miss any fractions as it costs in MILLIONS. The scenario is that I've two numbers, after multiplying each other I'm losing the precision of the values. I've tried to check my database for any faulty data that caused the issue. But the data is absolutely fine.

Hacker Rank: 2D Array - DS Solution

I've been trying to solve the puzzles on lately. This post is just to solve the problem in Arrays section of Hacker Rank

I feel better to solve the problems though. It certainly improves some area in programming language.

As I'm a C# developer the problem is solved using C# as programming language.


And the solution is