Saturday, 1 August 2015

Working of Stack (Explained)

Stack is the simplest Data Structure with the policy of LIFO (Last In First Out), remember stack of trays ? Stack of coins placed above on each? Or you may think about it when you are Undoing and Redoing something on computer, Yes it works according to stack as well. This was the pretty much theory you need to know for stack now lets get implement it in programming.
What Ingredients you need to have to make a perfect working stack ?
Here are the followings:

  • You need to PUSH any object in stack
  • You need to POP any object from stack
  • You may check the current POSITION 
  • For PUSH you need to know if it isn't FULL so you'll be making it as well
  • For POP you need to know if it isn't EMPTY.
  • Then the last you need to built a MENU for taking user input again and again
First make the functions is_Empty and is_Full so that Push and Pop functions can check the error and not get caught in exception.
So for making the function is_Full we need to define variables and stuff to get know how bigger is stack and at what condition stack will be called full. right?

We'll be using Class here which is named "Stack". So in the body we have declared top to locate the current position of stack , size to ask user how much bigger stack user wants. Then in constructor "Stack()" assigned the values to the pointer array for dynamic input in the program.

Here we go straight after making Constructor we defined prototypes of our functions we'll be defining outside of the class (Prototypes are just a promise to function that declare them here but we'll define them later in the program). Our functions will be returning the True or False (e.g if stack is empty ? Yes or No ?) except current_place( ) because it has to return the integer with the current place. 

We'll be defining these functions outside the class. For that you have to do it as " Type NameOfClass ScopeResolutionOperator FunctionName(your declared prototype) " in code picture you can see it as " bool Stack :: is_full( ) ".
Till now we got set our Stack program to ask user how much bigger stack should work for you. What'll be function you'll be defining in this program. Next we want to start defining our functions to work with.

Simply put "is_Empty( )" & "is_Full( )" inside the if condition to check whether there's any exception or not. While the function "Current_Place( )" will simply tell you what's in your stack and if your stack is empty or not. How easy is that?

Function for displaying the menu list for the user to select the option and perform their operations.

No comments:

Post a Comment