堆栈数据结构|后进先出 (LIFO)
- - 推送(添加元素):将元素添加到堆栈顶部。
- - Pop(删除元素):从顶部删除元素。
- - isFull:检查堆栈是否已达到其限制(在本例中为 10)。
- - isEmpty:检查堆栈是否为空。
- - 显示:显示堆栈元素。
1.示例:
索引.html
<!DOCTYPE html> <html lang="en"> <head> <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> </head> <body> <h3>Stack in Javascript</h3> <script> let Data = []; // Add an element to the array function AddEle(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 AddEle(55); AddEle(85); AddEle(25); Remove(); Display(); // [55, 85] </script> </body> </html>
2.示例:
index2.html
<!DOCTYPE html> <html lang="en"> <head> <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> </head> <body> <div class="container"> <!-- Title --> <h3>Stack in JavaScript</h3> <!-- Input section --> <input type="text" id="AddEle" 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("AddEle").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> </body> </html>
输出:
带有用户输入的 C 语言堆栈
#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 AddEle() { 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 decrement 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 <= top; i++) { printf("%d ", Data[i]); } printf("\n"); } } int main() { int choice; do { printf("\n1. Add Element\n2. Remove Element\n3. Display Stack\n4. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); // Read the user's choice switch (choice) { case 1: AddEle(); break; case 2: Remove(); break; case 3: Display(); break; case 4: printf("Exiting...\n"); break; default: printf("Invalid choice! Please select a valid option.\n"); } } while (choice != 4); return 0; }
示例输出:
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...
以上是堆栈数据结构|后进先出 (LIFO)的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

C#和C 的历史与演变各有特色,未来前景也不同。1.C 由BjarneStroustrup在1983年发明,旨在将面向对象编程引入C语言,其演变历程包括多次标准化,如C 11引入auto关键字和lambda表达式,C 20引入概念和协程,未来将专注于性能和系统级编程。2.C#由微软在2000年发布,结合C 和Java的优点,其演变注重简洁性和生产力,如C#2.0引入泛型,C#5.0引入异步编程,未来将专注于开发者的生产力和云计算。

C#和C 的学习曲线和开发者体验有显着差异。 1)C#的学习曲线较平缓,适合快速开发和企业级应用。 2)C 的学习曲线较陡峭,适用于高性能和低级控制的场景。

C 通过第三方库(如TinyXML、Pugixml、Xerces-C )与XML交互。1)使用库解析XML文件,将其转换为C 可处理的数据结构。2)生成XML时,将C 数据结构转换为XML格式。3)在实际应用中,XML常用于配置文件和数据交换,提升开发效率。

静态分析在C 中的应用主要包括发现内存管理问题、检查代码逻辑错误和提高代码安全性。1)静态分析可以识别内存泄漏、双重释放和未初始化指针等问题。2)它能检测未使用变量、死代码和逻辑矛盾。3)静态分析工具如Coverity能发现缓冲区溢出、整数溢出和不安全API调用,提升代码安全性。

使用C 中的chrono库可以让你更加精确地控制时间和时间间隔,让我们来探讨一下这个库的魅力所在吧。C 的chrono库是标准库的一部分,它提供了一种现代化的方式来处理时间和时间间隔。对于那些曾经饱受time.h和ctime折磨的程序员来说,chrono无疑是一个福音。它不仅提高了代码的可读性和可维护性,还提供了更高的精度和灵活性。让我们从基础开始,chrono库主要包括以下几个关键组件:std::chrono::system_clock:表示系统时钟,用于获取当前时间。std::chron

C 在现代编程中仍然具有重要相关性。1)高性能和硬件直接操作能力使其在游戏开发、嵌入式系统和高性能计算等领域占据首选地位。2)丰富的编程范式和现代特性如智能指针和模板编程增强了其灵活性和效率,尽管学习曲线陡峭,但其强大功能使其在今天的编程生态中依然重要。

C 的未来将专注于并行计算、安全性、模块化和AI/机器学习领域:1)并行计算将通过协程等特性得到增强;2)安全性将通过更严格的类型检查和内存管理机制提升;3)模块化将简化代码组织和编译;4)AI和机器学习将促使C 适应新需求,如数值计算和GPU编程支持。

1)c relevantduetoItsAverity and效率和效果临界。2)theLanguageIsconTinuellyUped,withc 20introducingFeaturesFeaturesLikeTuresLikeSlikeModeLeslikeMeSandIntIneStoImproutiMimproutimprouteverusabilityandperformance.3)
