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

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

1.示例:
索引.html

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

PHP
<!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 语言堆栈

PHP
#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;
}

示例输出:

PHP
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

分享:

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

发表评论
热门文章
  • BioWare埃德蒙顿工作室面临关闭危机,龙腾世纪制作总监辞职引关注(龙腾.总监.辞职.危机.面临.....)

    BioWare埃德蒙顿工作室面临关闭危机,龙腾世纪制作总监辞职引关注(龙腾.总监.辞职.危机.面临.....)
    知名变性人制作总监corrine busche离职bioware,引发业界震荡!外媒“smash jt”独家报道称,《龙腾世纪:影幢守护者》制作总监corrine busche已离开bioware,此举不仅引发了关于个人职业发展方向的讨论,更因其可能预示着bioware埃德蒙顿工作室即将关闭而备受关注。本文将深入分析busche离职的原因及其对bioware及游戏行业的影响。 Busche的告别信:挑战与感激并存 据“Smash JT”获得的内部邮件显示,Busche离职原...
  • 闪耀暖暖靡城永恒怎么样-闪耀暖暖靡城永恒套装介绍(闪耀.暖暖.套装.介绍.....)

    闪耀暖暖靡城永恒怎么样-闪耀暖暖靡城永恒套装介绍(闪耀.暖暖.套装.介绍.....)
    闪耀暖暖钻石竞技场第十七赛季“华梦泡影”即将开启!全新闪耀性感套装【靡城永恒】震撼来袭!想知道如何获得这套精美套装吗?快来看看吧! 【靡城永恒】套装设计理念抢先看: 设计灵感源于夜色中的孤星,象征着淡然、漠视一切的灰色瞳眸。设计师希望通过这套服装,展现出在虚幻与真实交织的夜幕下,一种独特的魅力。 服装细节考究,从面料的光泽、鞋跟声响到裙摆的弧度,都力求完美还原设计初衷。 【靡城永恒】套装设计亮点: 闪耀的绸缎与金丝交织,轻盈的羽毛增添华贵感。 这套服装仿佛是从无尽的黑...
  • python怎么调用其他文件函数

    python怎么调用其他文件函数
    在 python 中调用其他文件中的函数,有两种方式:1. 使用 import 语句导入模块,然后调用 [模块名].[函数名]();2. 使用 from ... import 语句从模块导入特定函数,然后调用 [函数名]()。 如何在 Python 中调用其他文件中的函数 在 Python 中,您可以通过以下两种方式调用其他文件中的函数: 1. 使用 import 语句 优点:简单且易于使用。 缺点:会将整个模块导入到当前作用域中,可能会导致命名空间混乱。 步骤:...
  • 斗魔骑士哪个角色强势-斗魔骑士角色推荐与实力解析(骑士.角色.强势.解析.实力.....)

    斗魔骑士哪个角色强势-斗魔骑士角色推荐与实力解析(骑士.角色.强势.解析.实力.....)
    斗魔骑士角色选择及战斗策略指南 斗魔骑士游戏中,众多角色各具特色,选择适合自己的角色才能在战斗中占据优势。本文将为您详细解读如何选择强力角色,并提供团队协作及角色培养策略。 如何选择强力角色? 斗魔骑士的角色大致分为近战和远程两种类型。近战角色通常拥有高攻击力和防御力,适合冲锋陷阵;远程角色则擅长后方输出,并依靠灵活走位躲避攻击。 选择角色时,需根据个人游戏风格和喜好决定。喜欢正面硬刚的玩家可以选择战士型角色,其高生命值和防御力能承受更多伤害;偏好策略性玩法的玩家则可以选择法...
  • 奇迹暖暖诸星梦眠怎么样-奇迹暖暖诸星梦眠套装介绍(星梦.暖暖.奇迹.套装.介绍.....)

    奇迹暖暖诸星梦眠怎么样-奇迹暖暖诸星梦眠套装介绍(星梦.暖暖.奇迹.套装.介绍.....)
    奇迹暖暖全新活动“失序之圜”即将开启,参与活动即可获得精美套装——诸星梦眠!想知道这套套装的细节吗?一起来看看吧! 奇迹暖暖诸星梦眠套装详解 “失序之圜”活动主打套装——诸星梦眠,高清海报震撼公开!少女在无垠梦境中,接受星辰的邀请,馥郁芬芳,预示着命运之花即将绽放。 诸星梦眠套装包含:全新妆容“隽永之梦”、星光面饰“熠烁星光”、动态特姿连衣裙“诸星梦眠”、动态特姿发型“金色绮想”、精美特效皇冠“繁星加冕”,以及动态摆件“芳馨酣眠”、“沉云余音”、“流星低语”、“葳蕤诗篇”。...