堆疊資料結構|後進先出 (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 中的應用主要包括發現內存管理問題、檢查代碼邏輯錯誤和提高代碼安全性。 1)靜態分析可以識別內存洩漏、雙重釋放和未初始化指針等問題。 2)它能檢測未使用變量、死代碼和邏輯矛盾。 3)靜態分析工具如Coverity能發現緩衝區溢出、整數溢出和不安全API調用,提升代碼安全性。

C 通過第三方庫(如TinyXML、Pugixml、Xerces-C )與XML交互。 1)使用庫解析XML文件,將其轉換為C 可處理的數據結構。 2)生成XML時,將C 數據結構轉換為XML格式。 3)在實際應用中,XML常用於配置文件和數據交換,提升開發效率。

使用C 中的chrono庫可以讓你更加精確地控制時間和時間間隔,讓我們來探討一下這個庫的魅力所在吧。 C 的chrono庫是標準庫的一部分,它提供了一種現代化的方式來處理時間和時間間隔。對於那些曾經飽受time.h和ctime折磨的程序員來說,chrono無疑是一個福音。它不僅提高了代碼的可讀性和可維護性,還提供了更高的精度和靈活性。讓我們從基礎開始,chrono庫主要包括以下幾個關鍵組件:std::chrono::system_clock:表示系統時鐘,用於獲取當前時間。 std::chron

C 的未來將專注於並行計算、安全性、模塊化和AI/機器學習領域:1)並行計算將通過協程等特性得到增強;2)安全性將通過更嚴格的類型檢查和內存管理機制提升;3)模塊化將簡化代碼組織和編譯;4)AI和機器學習將促使C 適應新需求,如數值計算和GPU編程支持。

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

DMA在C 中是指DirectMemoryAccess,直接內存訪問技術,允許硬件設備直接與內存進行數據傳輸,不需要CPU干預。 1)DMA操作高度依賴於硬件設備和驅動程序,實現方式因係統而異。 2)直接訪問內存可能帶來安全風險,需確保代碼的正確性和安全性。 3)DMA可提高性能,但使用不當可能導致系統性能下降。通過實踐和學習,可以掌握DMA的使用技巧,在高速數據傳輸和實時信號處理等場景中發揮其最大效能。
