search
HomeBackend DevelopmentC++Stack Data Structure | Last In First Out (LIFO)

  1. - Push (Add Element): Add an element to the top of the stack.
  2. - pop (delete element): Remove element from the top.
  3. - isfull: Checks if the stack has reached its limit (10 in this case).
  4. - isempty: Check whether the stack is empty.
  5. - 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&#39;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&#39;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:

Stack Data Structure | Last In First Out (LIFO)

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C   XML Parsing: Techniques and Best PracticesC XML Parsing: Techniques and Best PracticesMay 07, 2025 am 12:06 AM

The DOM and SAX methods can be used to parse XML data in C. 1) DOM parsing loads XML into memory, suitable for small files, but may take up a lot of memory. 2) SAX parsing is event-driven and is suitable for large files, but cannot be accessed randomly. Choosing the right method and optimizing the code can improve efficiency.

C   in Specific Domains: Exploring Its StrongholdsC in Specific Domains: Exploring Its StrongholdsMay 06, 2025 am 12:08 AM

C is widely used in the fields of game development, embedded systems, financial transactions and scientific computing, due to its high performance and flexibility. 1) In game development, C is used for efficient graphics rendering and real-time computing. 2) In embedded systems, C's memory management and hardware control capabilities make it the first choice. 3) In the field of financial transactions, C's high performance meets the needs of real-time computing. 4) In scientific computing, C's efficient algorithm implementation and data processing capabilities are fully reflected.

Debunking the Myths: Is C   Really a Dead Language?Debunking the Myths: Is C Really a Dead Language?May 05, 2025 am 12:11 AM

C is not dead, but has flourished in many key areas: 1) game development, 2) system programming, 3) high-performance computing, 4) browsers and network applications, C is still the mainstream choice, showing its strong vitality and application scenarios.

C# vs. C  : A Comparative Analysis of Programming LanguagesC# vs. C : A Comparative Analysis of Programming LanguagesMay 04, 2025 am 12:03 AM

The main differences between C# and C are syntax, memory management and performance: 1) C# syntax is modern, supports lambda and LINQ, and C retains C features and supports templates. 2) C# automatically manages memory, C needs to be managed manually. 3) C performance is better than C#, but C# performance is also being optimized.

Building XML Applications with C  : Practical ExamplesBuilding XML Applications with C : Practical ExamplesMay 03, 2025 am 12:16 AM

You can use the TinyXML, Pugixml, or libxml2 libraries to process XML data in C. 1) Parse XML files: Use DOM or SAX methods, DOM is suitable for small files, and SAX is suitable for large files. 2) Generate XML file: convert the data structure into XML format and write to the file. Through these steps, XML data can be effectively managed and manipulated.

XML in C  : Handling Complex Data StructuresXML in C : Handling Complex Data StructuresMay 02, 2025 am 12:04 AM

Working with XML data structures in C can use the TinyXML or pugixml library. 1) Use the pugixml library to parse and generate XML files. 2) Handle complex nested XML elements, such as book information. 3) Optimize XML processing code, and it is recommended to use efficient libraries and streaming parsing. Through these steps, XML data can be processed efficiently.

C   and Performance: Where It Still DominatesC and Performance: Where It Still DominatesMay 01, 2025 am 12:14 AM

C still dominates performance optimization because its low-level memory management and efficient execution capabilities make it indispensable in game development, financial transaction systems and embedded systems. Specifically, it is manifested as: 1) In game development, C's low-level memory management and efficient execution capabilities make it the preferred language for game engine development; 2) In financial transaction systems, C's performance advantages ensure extremely low latency and high throughput; 3) In embedded systems, C's low-level memory management and efficient execution capabilities make it very popular in resource-constrained environments.

C   XML Frameworks: Choosing the Right One for YouC XML Frameworks: Choosing the Right One for YouApr 30, 2025 am 12:01 AM

The choice of C XML framework should be based on project requirements. 1) TinyXML is suitable for resource-constrained environments, 2) pugixml is suitable for high-performance requirements, 3) Xerces-C supports complex XMLSchema verification, and performance, ease of use and licenses must be considered when choosing.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use