- - 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!

C Learners and developers can get resources and support from StackOverflow, Reddit's r/cpp community, Coursera and edX courses, open source projects on GitHub, professional consulting services, and CppCon. 1. StackOverflow provides answers to technical questions; 2. Reddit's r/cpp community shares the latest news; 3. Coursera and edX provide formal C courses; 4. Open source projects on GitHub such as LLVM and Boost improve skills; 5. Professional consulting services such as JetBrains and Perforce provide technical support; 6. CppCon and other conferences help careers

C# is suitable for projects that require high development efficiency and cross-platform support, while C is suitable for applications that require high performance and underlying control. 1) C# simplifies development, provides garbage collection and rich class libraries, suitable for enterprise-level applications. 2)C allows direct memory operation, suitable for game development and high-performance computing.

C Reasons for continuous use include its high performance, wide application and evolving characteristics. 1) High-efficiency performance: C performs excellently in system programming and high-performance computing by directly manipulating memory and hardware. 2) Widely used: shine in the fields of game development, embedded systems, etc. 3) Continuous evolution: Since its release in 1983, C has continued to add new features to maintain its competitiveness.

The future development trends of C and XML are: 1) C will introduce new features such as modules, concepts and coroutines through the C 20 and C 23 standards to improve programming efficiency and security; 2) XML will continue to occupy an important position in data exchange and configuration files, but will face the challenges of JSON and YAML, and will develop in a more concise and easy-to-parse direction, such as the improvements of XMLSchema1.1 and XPath3.1.

The modern C design model uses new features of C 11 and beyond to help build more flexible and efficient software. 1) Use lambda expressions and std::function to simplify observer pattern. 2) Optimize performance through mobile semantics and perfect forwarding. 3) Intelligent pointers ensure type safety and resource management.

C The core concepts of multithreading and concurrent programming include thread creation and management, synchronization and mutual exclusion, conditional variables, thread pooling, asynchronous programming, common errors and debugging techniques, and performance optimization and best practices. 1) Create threads using the std::thread class. The example shows how to create and wait for the thread to complete. 2) Synchronize and mutual exclusion to use std::mutex and std::lock_guard to protect shared resources and avoid data competition. 3) Condition variables realize communication and synchronization between threads through std::condition_variable. 4) The thread pool example shows how to use the ThreadPool class to process tasks in parallel to improve efficiency. 5) Asynchronous programming uses std::as

C's memory management, pointers and templates are core features. 1. Memory management manually allocates and releases memory through new and deletes, and pay attention to the difference between heap and stack. 2. Pointers allow direct operation of memory addresses, and use them with caution. Smart pointers can simplify management. 3. Template implements generic programming, improves code reusability and flexibility, and needs to understand type derivation and specialization.

C is suitable for system programming and hardware interaction because it provides control capabilities close to hardware and powerful features of object-oriented programming. 1)C Through low-level features such as pointer, memory management and bit operation, efficient system-level operation can be achieved. 2) Hardware interaction is implemented through device drivers, and C can write these drivers to handle communication with hardware devices.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment