Stack

Introduction

The stack is a data structure that works on the Last In First Out (LIFO) principle. They will have the top variable that always keeps track of the top element.

Operations

Push/Insert: To insert an element

Pop/Delete: To delete an element

The operations like push and pop that are performed on the stack will depend upon the top value. If an element has to be deleted then the topmost value will be deleted first.

Explanation

Let us look into an example and understand how the it stores the data.

Here the size of the stack is 4, and no data element is present hence top is equal to -1, which represents it as an empty.

Now, let us perform the push operation, to insert the data value of 10 into it. Then the data is stored in the bottom location and the top value is updated to 0, indicated one element is present in the stack

Now push the data element 20 into the stack, then the top value is updated with 1, represents it consists of two elements.

Now, push data element 30 and update the top accordingly.

Finally, you can push element 40 and update the top value to 3. This shows that the entire stack is filled.

Now, if you try to push another data element into the stack, it shows the overflow condition.

Then you can pop out the elements, the first element that can be deleted is the topmost element. After deleting the top element the top value is updated to the top-1 value.

This completes the clear understanding of the push and pop operations on it.

Now, let us understand the algorithm of push and pop operations.

Algorithm of Push Operation

The algorithm, initially checks whether the stack is full or not. If the it is full it shows the overflow, when the top value is the maximum size.

Otherwise, the top value is incremented by 1, and the data is inserted into the stack.

Algorithm of Pop Operation

It checks whether the stack is empty or not. If the top value is -1, then the underflow is shown.

Otherwise, the top element will be deleted and the top value is reduced by 1.

Visit other sections in the website related to the electronics – 8051 & 8086

Spread knowledge

Leave a Comment

Your email address will not be published.