When writing code, the stack is the most commonly used C ++ data structure. It has a simple concept and is easy to write. Now, for example, there are 6 books piled up on the table. What should you do if you want to add one? You just need to put the book on the top.
What if you want to get 3rd books from this pile of books? You only need one next book to move to the top until the first 3rd books are at the top. Then take 3rd books and place others at the top.
You have noticed that I use the top word. That's right. The top is critical to the stack. The stack can only add data from the top, and the stack is also from the top. That's simple.
So what is the use of stack? The stack is used in every process. Each process has a stack, and the data and address are retrieved/added from the stack. The stack top rules also match here.
ESP Register adds a pointer pointing to the top of the stack. In any case, explaining how the stack in the process works is beyond the scope of this tutorial. Let's start writing the C ++ data structure. Before you start, remember some stack terms. Insert a new element into the stack and delete the element from the stack to become the output stack.
- #include
- using namespace std;
- #define MAX 10 // MAXIMUM STACK CONTENT
- class stack
- {
- private:
- int arr[MAX]; // Contains all the Data
- int top; //Contains location of Topmost Data pushed onto Stack
- public:
- stack() //Constructor
- {
- top=-1; //Sets the Top Location to -1 indicating an empty stack
- }
- void push(int a) // Push ie. Add Value Function
- {
- top++; // increment to by 1
- if(top
- {
- arr[top]=a; //If Stack is Vacant store Value in Array
- }
- else
- {
- cout<<"STACK FULL!!"<
- top--;
- }
- }
- int pop() // Delete Item. Returns the deleted item
- {
- if(top==-1)
- {
- cout<<"STACK IS EMPTY!!!"<
- return NULL;
- }
- else
- {
- int data=arr[top]; //Set Topmost Value in data
- arr[top]=NULL; //Set Original Location to NULL
- top--; // Decrement top by 1
- return data; // Return deleted item
- }
- }
- };
- int main()
- {
- stack a;
- a.push(3);
- cout<<"3 is Pushed\n";
- a.push(10);
- cout<<"10 is Pushed\n";
- a.push(1);
- cout<<"1 is Pushed\n\n";
- cout<
- cout<
- cout<
- return 0;
- }
We can clearly see that the data in the last stack is the first data in the stack. This is why the stack is LIFO (Last In First Out, Last In First Out ). I guess you understand why. Let's see how to compile and execute this program. First, we create a variable named top so that it is at the top of the stack. Value-1 indicates that the stack is empty. When there is data input, top automatically adds 1 and stores the data in the arr array. This C ++ data structure has a disadvantage. We can only put up to 10 elements.
- Differences between standard input implementation methods in C and C ++
- How does the C ++ compiler allocate storage space for Const constants?
- Basic Conception and method of C ++ Class Library Design
- Several Methods for converting C ++ Language
- How to better compile C ++ code