In this tutorial, I suppose everyone knows how to use C ++ classes for work, because all my data structures should be based on them. I have encountered a tutorial on data structures, but it is difficult to find a document written using OOP. Therefore, this will mainly focus on writing data structures with a class.
Stack
A stack is the most common data structure when writing code. It has a simple concept and is easy to write. In this case, there are five books piled up on the table. You want to add one. What should I do? 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 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.
The following is a reference clip:
- #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;
- }
Output:
3 is Pushed
10 is Pushed
1 is Pushed
1 is Popped
10 is Popped
3 is Popped
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 data structure has a disadvantage. We can only put up to 10 elements.