Stack Data Structure | Last In First Out (LIFO)
- - Push (Add Element): Add an element to the top of the stack.
- - pop (delete element): Remove element from the top.
- - isfull: Checks if the stack has reached its limit (10 in this case).
- - isempty: Check whether the stack is empty.
- - Display: Show stack elements.
1. Example:
Index.html
<meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>stack | last in first out (lifo) or first in last out | - by sudhanshu gaikwad (filo)</title> <h3 id="stack-in-javascript">stack in javascript</h3> <script> let data = []; // add an element to the array function adde(ele) { if (isfull()) { console.log("array is full, element can't be added!"); } else { console.log("element added!"); data.push(ele); } } // check if the array is full function isfull() { return data.length >= 10; } // remove an element from the array function remove() { if (isempty()) { console.log("array is empty, can't remove element!"); } else { data.pop(); console.log("element removed!"); } } // check if the array is empty function isempty() { return data.length === 0; } // display the array elements function display() { console.log("updated array >> ", data); } // example usage adde(55); adde(85); adde(25); remove(); display(); // [55, 85] </script>
2. Example:
index2.html
<meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>what is stack in javascript | by sudhanshu gaikwad</title> <style> * { box-sizing: border-box; } body { font-family: "roboto condensed", sans-serif; background-color: #f4f4f4; margin: 0; padding: 0; display: flex; flex-direction: column; justify-content: center; align-items: center; height: 100vh; } .container { background-color: white; padding: 20px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); max-width: 400px; width: 100%; margin-bottom: 20px; } h3 { color: #333; text-align: center; margin-bottom: 20px; } input { padding: 10px; width: calc(100% - 20px); margin-bottom: 10px; border: 1px solid #ccc; border-radius: 5px; } button { padding: 10px; margin: 10px 0; border: none; border-radius: 5px; background-color: #292f31; color: white; cursor: pointer; width: 100%; } button:hover { background-color: #e9e9ea; color: #292f31; } .message { margin-top: 15px; color: #333; font-size: 16px; text-align: center; } .footer { text-align: center; margin-top: 20px; font-size: 14px; color: #555; } /* responsive design */ @media (max-width: 768px) { .container { padding: 15px; max-width: 90%; } button { font-size: 14px; } input { font-size: 14px; } } </style> <div class="container"> <!-- title --> <h3 id="stack-in-javascript">stack in javascript</h3> <!-- input section --> <input type="text" id="adde" placeholder="enter an element"> <!-- buttons section --> <button onclick="adddata()">add element</button> <button onclick="removeele()">remove element</button> <button onclick="display()">show array</button> <!-- message sections --> <div id="add" class="message"></div> <div id="remove" class="message"></div> <div id="display" class="message"></div> </div> <!-- footer with copyright symbol --> <div class="footer"> © 2024 sudhanshu developer | all rights reserved </div> <script> let data = []; // function to add an element to the stack function adddata() { let newele = document.getelementbyid("addele").value; if (isfull()) { document.getelementbyid("add").innerhtml = "array is full, element cannot be added!"; } else if (newele.trim() === "") { document.getelementbyid("add").innerhtml = "please enter a valid element!"; } else { data.push(newele); document.getelementbyid("add").innerhtml = `element "${newele}" added!`; document.getelementbyid("adde").value = ""; console.log("current array: ", data); display(); } } function isfull() { return data.length >= 10; } function removeele() { if (isempty()) { document.getelementbyid("remove").innerhtml = "array is empty!"; } else { let removedelement = data.pop(); document.getelementbyid("remove").innerhtml = `element "${removedelement}" removed!`; console.log("current array: ", data); display(); } } function isempty() { return data.length === 0; } function display() { let displayarea = document.getelementbyid("display"); displayarea.innerhtml = ""; if (data.length === 0) { displayarea.innerhtml = "no elements in the array!"; console.log("array is empty."); } else { for (let i = 0; i < data.length; i ) { displayarea.innerhtml = `element ${i 1}: ${data[i]}<br>`; } console.log("displaying array: ", data); } } </script>
Output:
C language stack with user input
#include <stdio.h> #include <stdbool.h> #define max 10 int data[max]; int top = -1; // function to check if the stack is full bool isfull() { return top >= max - 1; } // function to check if the stack is empty bool isempty() { return top == -1; } // function to add an element to the stack (push operation) void adde() { int ele; if (isfull()) { printf("array is full, element can't be added!\n"); } else { printf("enter an element to add: "); scanf("%d", &ele); // read user input data[top] = ele; // increment top and add element printf("element %d added!\n", ele); } } // function to remove an element from the stack (pop operation) void remove() { if (isempty()) { printf("array is empty, can't remove element!\n"); } else { printf("element %d removed!\n", data[top--]); // remove element and decline top } } // function to display all elements in the stack void display() { if (isempty()) { printf("array is empty!\n"); } else { printf("updated array >> "); for (int i = 0; i <p><strong>Sample output:</strong><br></p> <pre class="brush:php;toolbar:false"> 1. Add Element 2. Remove Element 3. Display Stack 4. Exit Enter your choice: 1 Enter an element to add: 55 Element 55 Added! 1. Add Element 2. Remove Element 3. Display Stack 4. Exit Enter your choice: 3 Updated Array >> 55 1. Add Element 2. Remove Element 3. Display Stack 4. Exit Enter your choice: 4 Exiting...
The above is the detailed content of Stack Data Structure | Last In First Out (LIFO). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Bitcoin’s price ranges from $20,000 to $30,000. 1. Bitcoin’s price has fluctuated dramatically since 2009, reaching nearly $20,000 in 2017 and nearly $60,000 in 2021. 2. Prices are affected by factors such as market demand, supply, and macroeconomic environment. 3. Get real-time prices through exchanges, mobile apps and websites. 4. Bitcoin price is highly volatile, driven by market sentiment and external factors. 5. It has a certain relationship with traditional financial markets and is affected by global stock markets, the strength of the US dollar, etc. 6. The long-term trend is bullish, but risks need to be assessed with caution.

The top ten cryptocurrency exchanges in the world in 2025 include Binance, OKX, Gate.io, Coinbase, Kraken, Huobi, Bitfinex, KuCoin, Bittrex and Poloniex, all of which are known for their high trading volume and security.

The top ten cryptocurrency trading platforms in the world include Binance, OKX, Gate.io, Coinbase, Kraken, Huobi Global, Bitfinex, Bittrex, KuCoin and Poloniex, all of which provide a variety of trading methods and powerful security measures.

The top ten digital currency exchanges such as Binance, OKX, gate.io have improved their systems, efficient diversified transactions and strict security measures.

MeMebox 2.0 redefines crypto asset management through innovative architecture and performance breakthroughs. 1) It solves three major pain points: asset silos, income decay and paradox of security and convenience. 2) Through intelligent asset hubs, dynamic risk management and return enhancement engines, cross-chain transfer speed, average yield rate and security incident response speed are improved. 3) Provide users with asset visualization, policy automation and governance integration, realizing user value reconstruction. 4) Through ecological collaboration and compliance innovation, the overall effectiveness of the platform has been enhanced. 5) In the future, smart contract insurance pools, forecast market integration and AI-driven asset allocation will be launched to continue to lead the development of the industry.

Currently ranked among the top ten virtual currency exchanges: 1. Binance, 2. OKX, 3. Gate.io, 4. Coin library, 5. Siren, 6. Huobi Global Station, 7. Bybit, 8. Kucoin, 9. Bitcoin, 10. bit stamp.

Recommended reliable digital currency trading platforms: 1. OKX, 2. Binance, 3. Coinbase, 4. Kraken, 5. Huobi, 6. KuCoin, 7. Bitfinex, 8. Gemini, 9. Bitstamp, 10. Poloniex, these platforms are known for their security, user experience and diverse functions, suitable for users at different levels of digital currency transactions

Using the chrono library in C can allow you to control time and time intervals more accurately. Let's explore the charm of this library. C's chrono library is part of the standard library, which provides a modern way to deal with time and time intervals. For programmers who have suffered from time.h and ctime, chrono is undoubtedly a boon. It not only improves the readability and maintainability of the code, but also provides higher accuracy and flexibility. Let's start with the basics. The chrono library mainly includes the following key components: std::chrono::system_clock: represents the system clock, used to obtain the current time. std::chron
