|
| 1 | +#include<iostream> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | + |
| 5 | +/* STACK IN C++ */ |
| 6 | +/* |
| 7 | + A stack is similar to real-life stack or a pile of things that we stack one above the other. |
| 8 | + Stack is a linear data structure which follows a particular order in which the operations are performed. |
| 9 | + The order may be LIFO(Last In First Out) or FILO(First In Last Out). |
| 10 | + Mainly the following three basic operations are performed in the stack: |
| 11 | + - Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. |
| 12 | +
|
| 13 | + - Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. |
| 14 | + If the stack is empty, then it is said to be an Underflow condition. |
| 15 | +
|
| 16 | + - Peek or Top: Returns top element of stack. |
| 17 | +
|
| 18 | + Let's learn it by doing |
| 19 | +*/ |
| 20 | + |
| 21 | + |
| 22 | +//Let's Define the maximum size of Stack as `10` using C++ Macros |
| 23 | +#define MAX 10 |
| 24 | + |
| 25 | +/* |
| 26 | +Creating Class named Stack to implement our first data structure(Abstract Data Type) |
| 27 | +We will not use any pointer to keep it basic. |
| 28 | +We will implement it using pointers and dynamic memory allocation in upcoming lectures. |
| 29 | +If you don't know what does it mean to be static, |
| 30 | +Let me tell it to you: In simple words,It doesn’t grow and shrink depending on needs at |
| 31 | +runtime(once MAX is defined using macros as 10,you cannot change it when program is |
| 32 | + executed and user need 20 values to store) |
| 33 | +On the other hand, dynamic can grow or shrink at runtime, which we will see in `Dynamic-Stack.hpp` using pointers. |
| 34 | +*/ |
| 35 | +class Stack |
| 36 | +{ |
| 37 | + private: |
| 38 | + /* |
| 39 | + data: An array of size `MAX` that we defined using MACRO above. |
| 40 | + You can also declare it as `data[10] without using #define MACRO. |
| 41 | + So, it indicates that when user push() 10 values, the stack should be full. |
| 42 | + */ |
| 43 | + int data[MAX]; // Maximum size of Stack |
| 44 | + /* |
| 45 | + TOP: A variable that will store the current location of Stack |
| 46 | + Let's say you stacked 5 books on one another, in this scenario TOP variable will store |
| 47 | + the last book that you kept on the top i.e. TOP = 5 |
| 48 | + */ |
| 49 | + int TOP; |
| 50 | + public: |
| 51 | + /* |
| 52 | + CONSTRUCTOR: The first method that will be executed when an object of Stack class is created. |
| 53 | + We can do our important initialization in constructor. |
| 54 | + That's what it's created for |
| 55 | + */ |
| 56 | + Stack(){ |
| 57 | + /* |
| 58 | + WE WILL SET TOP to -1 which will indicate that the stack is empty |
| 59 | + We can also initialize it by 0, it doesn't matter, Just understand what you are doing. |
| 60 | + */ |
| 61 | + TOP = -1; |
| 62 | + } |
| 63 | + /* |
| 64 | + LETS DEFINE OUR PUSH FUNCTION NOW. AS WE SAW EARLIER, DATA INSERTION IN STACK IS CALLED PUSH. |
| 65 | + It's Just a fancy term which does Insertion of data behind the scenes. |
| 66 | + RETURN VALUES: It will return True if the value is inserted successfully & return false if it's not inserted. |
| 67 | + ARGUMENTS: This method takes one value as an argument which will be stacked into the last |
| 68 | + */ |
| 69 | + bool push(int x){ |
| 70 | + /* |
| 71 | + If VALUE OF TOP is greater or equal to MAX,it means that we cannot insert more values into Stack. |
| 72 | + (MAX-1) refers to the fact that the indexing of arrays start from 0. |
| 73 | +
|
| 74 | + */ |
| 75 | + if (TOP >= (MAX - 1)) { |
| 76 | + // Print that the Stack is full and return boolean value => FALSE |
| 77 | + cout << "Stack Overflow" << endl; |
| 78 | + return false; |
| 79 | + } |
| 80 | + // IF ABOVE CONDITION IS NOT TRUE, IT MeANS THAT THERE IS A SPACE TO ADD THE VALUE SENT BY The USER from main() |
| 81 | + else { |
| 82 | + /* |
| 83 | + So the First step should be => To increment value of TOP as it was initialized by -1. |
| 84 | + If we initialize it by 0 in constructor, then its not necessary to increment it first before inserting |
| 85 | + value at that position, now you got the point of Constructor initialization. |
| 86 | +
|
| 87 | + You can do it in two lines as: |
| 88 | + TOP = TOP + 1; |
| 89 | + data[TOP] = x |
| 90 | + OR SIMPLY in one line as: |
| 91 | + data[++TOP] = x; |
| 92 | + */ |
| 93 | + data[++TOP] = x; |
| 94 | + // PRINTING THE SUCCESS MESSAGE |
| 95 | + cout << x << " Pushed Successfully into the Stack" << endl; |
| 96 | + // RETURN TRUE => SUCCESS |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + /* |
| 102 | + Let's create pop() now which simply removes the top value from the Stack. |
| 103 | + */ |
| 104 | + void pop(){ |
| 105 | + /* |
| 106 | + Maybe Stack is already empty, so we should check it before popping a value. But which variable will |
| 107 | + tell us the status of the Stack as full or empty?? |
| 108 | + Definitely => TOP will indicate the current status of Stack |
| 109 | + IF TOP = -1, it means that there is nothing to remove from stack |
| 110 | + So let's use this condition in IF statement as if TOP is less than 0. |
| 111 | + You can also use it as if TOP == -1, that's upto you. Just understand what you're doing & how it should be. |
| 112 | + */ |
| 113 | + if (TOP < 0) { |
| 114 | + // PRINT MESSAGE THAT STACK IS ALREADY EMPTY(IT HAS NOTHING TO REMOVE) |
| 115 | + cout << "Stack Underflow" << endl; |
| 116 | + |
| 117 | + } |
| 118 | + /* |
| 119 | + If it's not empty, decrease value of TOP by one, which simply means deletion but its not the recommended way. |
| 120 | + We will see the recommended way using pointers in upcoming code snippets but for the sake of understanding |
| 121 | + do it as it is. |
| 122 | + To make it more understandable, let's remind the Book examples. You stacked 5 books on one another |
| 123 | + TOP is incremented on every book placement and finally TOP = 4 after the 5th book is placed(as indexing starts from 0) |
| 124 | + Now, if you put a new book on top, TOP will be incremented to 5 similarly if you want to pickup one book, |
| 125 | + what should happen to the value to TOP?? Don't you think it should be decreased by 1 so that the next placement |
| 126 | + of book should be on right position. That is the theory behind decrement by 1 |
| 127 | + */ |
| 128 | + else { |
| 129 | + //Let's print the value first before removing it |
| 130 | + cout << data[TOP] << " Popped out Successfully" << endl; |
| 131 | + // Remove it now |
| 132 | + TOP = TOP - 1; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /* |
| 137 | + Let's define our Last method which is Peek(). |
| 138 | + Peek or Top: Returns top element of stack. |
| 139 | +
|
| 140 | + */ |
| 141 | + void peek(){ |
| 142 | + // IF TOP == -1 or less than 0, it means Stack is already empty, Nothing to peek |
| 143 | + if (TOP < 0) { |
| 144 | + // PRINTING THE MESSAGE |
| 145 | + cout << "Stack is Empty"; |
| 146 | + } |
| 147 | + /* |
| 148 | + If Stack is not empty, we have to print the value on Top of the stack. |
| 149 | + Only TOP variable can tell us the about the value inserted previously, |
| 150 | + so let's ask TOP. |
| 151 | + */ |
| 152 | + else { |
| 153 | + int value = data[TOP]; |
| 154 | + cout << value << " is the Peek value" << endl; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | +}; |
0 commit comments