Stack In Data Structure

In this blog, I am going to discuss the basic data structure of stack. This is a very basic article, which might be helpful for the fresh students or beginners.

Introduction

In general, we use the word stack for a bundle of packets or pile of plates .The things which are placed on one over another till the top/peek is called a stack.

Stack

In programming world, we use stack to store data at a runtime on temporary basis. In stack data structure, we use two types of operations, which are as follows.

  1. PUSH
  2. POP

The logic which is used behind these operations are Last In First Out(LIFO). LIFO is a method, which tells us that the thing which is PUSH last should be POP first.

Now, we are moving forward to our implementation, which is coded on C sharp

First, we declare three variables to control our stack .The reason for these variables are mentioned in the comments.

  1. int max = 3; // Size of the Stack , You can handle the size of your stack dynamically  
  2. int top = -1; // it is a pointer which keep track that whether your stack is filled or not   
  3. string respnse = " "// this variable is used to store your response whether you want to PUSH or POP   
  4. string[] array = new string[max]; //Here we declare an array for our stack implementation  
Now, we take an infinite loop, which continuously asks you to PUSH and POP the data.

 

  1. while (true) {  
  2.     Console.WriteLine("Press P for push and O for POP");  
  3.     respnse = Console.ReadLine().ToUpper();  
  4.     if (respnse == "P") {  
  5.         Console.WriteLine("Enter value");  
  6.         top = top + 1; // on Pushing each value stack counter will be incremented  
  7.         if (top == max) {  
  8.             Console.WriteLine("Stack overflow");  
  9.             return;  
  10.         }  
  11.   
  12.         array[top] = Console.ReadLine();  
  13.   
  14.     }  
  15.     -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - x-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - x-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  
  16.     This is an accumulated code  
  17.     using System;  
  18.     using System.Collections.Generic;  
  19.     using System.Linq;  
  20.     using System.Text;  
  21.   
  22.     namespace stackNque {  
  23.         class Program {  
  24.             static void Main(string[] args) {  
  25.                 int max = 3;  
  26.                 int top = -1;  
  27.                 string respnse = "";  
  28.   
  29.   
  30.                 string[] array = new string[max];  
  31.   
  32.                 while (true) {  
  33.   
  34.                     Console.WriteLine("Press P for push and O for POP");  
  35.   
  36.                     respnse = Console.ReadLine().ToUpper();  
  37.   
  38.   
  39.                     if (respnse == "P") {  
  40.   
  41.                         Console.WriteLine("Enter value");  
  42.                         top = top + 1;  
  43.   
  44.                         if (top == max) {  
  45.                             Console.WriteLine("Stack overflow");  
  46.   
  47.                             return;  
  48.   
  49.                         }  
  50.   
  51.   
  52.   
  53.                         array[top] = Console.ReadLine();  
  54.   
  55.   
  56.   
  57.                     }  
  58.   
  59.                     if (respnse == "O") {  
  60.                         array[top] = " ";  
  61.                         top = top - 1;  
  62.   
  63.                         if (top < max) {  
  64.                             Console.WriteLine("Stack Underflow");  
  65.   
  66.   
  67.   
  68.                         }  
  69.   
  70.                     }  
  71.   
  72.   
  73.                 }  
  74.   
  75.   
  76.             }  
  77.   
  78.   
  79.         }  
  80.     }  
Conclusion

Stack is a container, where we insert our data in a pile. It is most useful where we track our last insertion record or use old data on a first level.

We can do PUSH and PULL operations on the basis of LIFO method.
Next Recommended Reading Data Types In Programming Languages