堆栈数据结构|后进先出 (LIFO)

wufei123 2025-01-26 阅读:6 评论:0
- 推送(添加元素):将元素添加到堆栈顶部。 - pop(删除元素):从顶部删除元素。 - isfull:检查堆栈是否已达到其限制(在本例中为 10)。 - isempty:检查堆栈是否为空。 - 显示:显示堆栈元素。 1.示例: 索引...
  1. - 推送(添加元素):将元素添加到堆栈顶部。
  2. - pop(删除元素):从顶部删除元素。
  3. - isfull:检查堆栈是否已达到其限制(在本例中为 10)。
  4. - isempty:检查堆栈是否为空。
  5. - 显示:显示堆栈元素。

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">
      &copy; 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>

输出:

堆栈数据结构|后进先出 (LIFO)

带有用户输入的 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!
");
    } 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!
", ele);
    }
}

// function to remove an element from the stack (pop operation)
void remove() {
    if (isempty()) {
        printf("array is empty, can't remove element!
");
    } else {
        printf("element %d removed!
", data[top--]); // remove element and decrement top
    }
}

// function to display all elements in the stack
void display() {
    if (isempty()) {
        printf("array is empty!
");
    } else {
        printf("updated array >> ");
        for (int i = 0; i <= top; i++) {
            printf("%d ", data[i]);
        }
        printf("
");
    }
}

int main() {
    int choice;
    do {
        printf("
1. add element
2. remove element
3. display stack
4. exit
");
        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...
");
                break;
            default:
                printf("invalid choice! please select a valid option.
");
        }
    } 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)的详细内容,更多请关注知识资源分享宝库其它相关文章!

版权声明

本站内容来源于互联网搬运,
仅限用于小范围内传播学习,请在下载后24小时内删除,
如果有侵权内容、不妥之处,请第一时间联系我们删除。敬请谅解!
E-mail:dpw1001@163.com

分享:

扫一扫在手机阅读、分享本文

发表评论
热门文章
  • 华为 Mate 70 性能重回第一梯队 iPhone 16 最后一块遮羞布被掀

    华为 Mate 70 性能重回第一梯队 iPhone 16 最后一块遮羞布被掀
    华为 mate 70 或将首发麒麟新款处理器,并将此前有博主爆料其性能跑分将突破110万,这意味着 mate 70 性能将重新夺回第一梯队。也因此,苹果 iphone 16 唯一能有一战之力的性能,也要被 mate 70 拉近不少了。 据悉,华为 Mate 70 性能会大幅提升,并且销量相比 Mate 60 预计增长40% - 50%,且备货充足。如果 iPhone 16 发售日期与 Mate 70 重合,销量很可能被瞬间抢购。 不过,iPhone 16 还有一个阵地暂时难...
  • 酷凛 ID-COOLING 推出霜界 240/360 一体水冷散热器,239/279 元

    酷凛 ID-COOLING 推出霜界 240/360 一体水冷散热器,239/279 元
    本站 5 月 16 日消息,酷凛 id-cooling 近日推出霜界 240/360 一体式水冷散热器,采用黑色无光低调设计,分别定价 239/279 元。 本站整理霜界 240/360 散热器规格如下: 酷凛宣称这两款水冷散热器搭载“自研新 V7 水泵”,采用三相六极马达和改进的铜底方案,缩短了水流路径,相较上代水泵进一步提升解热能力。 霜界 240/360 散热器的水泵为定速 2800 RPM 设计,噪声 28db (A)。 两款一体式水冷散热器采用 27mm 厚冷排,...
  • 惠普新款战 99 笔记本 5 月 20 日开售:酷睿 Ultra / 锐龙 8040,4999 元起

    惠普新款战 99 笔记本 5 月 20 日开售:酷睿 Ultra / 锐龙 8040,4999 元起
    本站 5 月 14 日消息,继上线官网后,新款惠普战 99 商用笔记本现已上架,搭载酷睿 ultra / 锐龙 8040处理器,最高可选英伟达rtx 3000 ada 独立显卡,售价 4999 元起。 战 99 锐龙版 R7-8845HS / 16GB / 1TB:4999 元 R7-8845HS / 32GB / 1TB:5299 元 R7-8845HS / RTX 4050 / 32GB / 1TB:7299 元 R7 Pro-8845HS / RTX 2000 Ada...
  • python中def什么意思

    python中def什么意思
    python 中,def 关键字用于定义函数,这些函数是代码块,执行特定任务。函数语法为 def (参数列表)。函数可以通过其名字和圆括号调用。函数可以接受参数作为输入,并在函数体中使用参数名访问。函数可以使用 return 语句返回一个值,它将成为函数调用的结果。 Python 中 def 关键字 在 Python 中,def 关键字用于定义函数。函数是代码块,旨在执行特定任务。 语法 def 函数定义的语法如下: def (参数列表): # 函数体 示例 定义...
  • python中int函数的用法

    python中int函数的用法
    int() 函数将值转换为整数,支持多种类型(字符串、字节、浮点数),默认进制为 10。可以指定进制数范围在 2-36。int() 返回 int 类型的转换结果,丢弃小数点。例如,将字符串 "42" 转换为整数为 42,将浮点数 3.14 转换为整数为 3。 Python 中的 int() 函数 int() 函数用于将各种类型的值转换为整数。它接受任何可以解释为整数的值作为输入,包括字符串、字节、浮点数和十六进制表示。 用法 int(object, base=10) 其中...