Javatpoint Logo
Javatpoint Logo

Push and Pop Operation in Stack in Data Structure

The Last-In-First-Out (LIFO) concept is used by Stacks, a type of linear data structure. The Queue has two endpoints, but the Stack only has one (front and rear). It only has one pointer, top pointer, which points to the stack's topmost member. When an element is added to the stack, it is always added to the top, and it can only be removed from the stack. To put it another way, a stack is a container that allows insertion and deletion from the end known as the stack's top.

LIFO (Last in First Out)

According to this method, the piece that was added last will appear first. As an actual illustration, consider a stack of dishes stacked on top of one another. We may claim that the plate we put last comes out first because the plate we put last is on top and we take the plate at the top.

Due to the possibility of understating inventory value, LIFO is not a reliable indicator of ending inventory value. Due to increasing COGS, LIFO leads to reduced net income (and taxes). However, under LIFO during inflation, there are fewer inventory write-downs. Results from average cost are in the middle of FIFO and LIFO.

Real Life Example of LIFO

Let's say that business A sells 10 widgets. $100 per, the first five widgets were delivered two days ago. The remaining five widgets, which cost $200 apiece, just got here yesterday. The last items received are the first to go out on the market, according to the LIFO technique of inventory management. How much may the accountant put down as a cost even though seven widgets were sold?

Although the income from each widget is the same because of its fixed sales price, the cost of each widget is determined by the inventory method used. The last item received is the first item sold according to the LIFO mechanism. Accordingly, the $200 widgets were the first to sell. Following that, the business sold two more of the $100 widgets. Five widgets are priced at $200 each, and two are priced at $100, for a total cost of $1,200 for the widgets using the LIFO approach. As opposed to FIFO, which sells the $200 widgets last, the $100 widgets are sold first. As a result, the price of the sold widgets, which breaks down to five at $100 and two at $200, will be reported as $900. That's why LIFO generates bigger profits during price-increasing periods.

Because of this, LIFO increases expenses during times of price growth and decreases net revenue, which also lowers taxable income. Similar to this, LIFO reduces expenses and raises net income during periods of declining prices, which also raises taxable revenue.

Stack Vs Array

Stack: A stack is a type of linear data structure whose components may only be added to or removed from the top side of the list. The Last in First out (LIFO) principle governs stacks, meaning that the element put last is the first element to be removed. Push operations and pop operations are the terms used to describe the addition and removal of elements from stacks, respectively. A pointer named top is used in stack to maintain track of the last piece that is currently present in the list.

Array: A group of objects kept in consecutive memory regions is known as an array. The goal is to group objects of the same category for storage. As a result, it is simpler to determine each element's position by simply adding an offset to a base value, or the address in memory where the array's first element is stored (generally denoted by the name of the array).

Push and Pop Operation in Stack in Data Structure

push()

It stacks up a new item. A stack overflow circumstance is when the stack is completely full.

Algorithm for push():

push()

It stacks up a new item. A stack overflow circumstance is when the stack is completely full.

Algorithm for push():

pop()

It takes something out of the stack. In the opposite sequence from which they were pushed, the things are popped. The condition is referred to as an underflow if the stack is empty.

Algorithm for pop():

Program to demonstrate push and pop operation in stack in data structure in C

Output

10 pushed into stack
20 pushed into stack
30 pushed into stack
30 Popped from stack
Top element is : 20
Elements present in stack : 20 10
...........................................................
Process executed in 3.22 seconds
Press any key to continue.

Explanation

As we can see in the above example of push and pop operation in c, we pushed 10, 20 and 30. After successfully pushing elements into the stack we pop out 30 from the stack and to check if the element is pop out or not, we checked it by performing top operation on the stack, which found out to be 20.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA