编程中的 LIFO(后进先出)方法
先决条件 - 编程中的 FIFO(先进先出)方法,编程中的 FIFO 与 LIFO 方法
LIFO是后进先出的缩写。它是一种处理数据结构的方法,其中第一个元素最后处理,最后一个元素先处理。
现实生活中的例子:
在此示例中,需要考虑以下事项:
- 有一个装球的桶。
- 不同类型的球被放入桶中。
- 最后入桶的球将首先被取出。
- 倒数第二个进入桶的球将在其上方的球(较新的球)之后被取出。
- 这样,首先进入桶的球将最后离开桶。
- 因此,最后一个进入桶的球(蓝色)首先被移除,第一个进入桶的球(红色)最后被移除。
这称为后进先出方法或 LIFO。
LIFO 在哪里使用:
- 数据结构 -
某些数据结构(例如 Stacks 和 Stacks 的其他变体)使用 LIFO 方法来处理数据。 - 提取最新信息——
有时,当从数组或数据缓冲区中提取数据时,计算机会使用 LIFO。当需要获取输入的最新信息时,使用 LIFO 方法。
LIFO 的程序示例 –
使用 Stack 数据结构:
C++
// C++ program to demonstrate
// working of LIFO
// using stack in C++
#include
using namespace std;
// Pushing element on the top of the stack
stack stack_push(stack stack)
{
for (int i = 0; i < 5; i++)
{
stack.push(i);
}
return stack;
}
// Popping element from the top of the stack
stack stack_pop(stack stack)
{
cout << "Pop :";
for (int i = 0; i < 5; i++)
{
int y = (int)stack.top();
stack.pop();
cout << (y) << endl;
}
return stack;
}
// Displaying element on the top of the stack
void stack_peek(stack stack)
{
int element = (int)stack.top();
cout << "Element on stack top : " << element << endl;
}
// Searching element in the stack
void stack_search(stack stack, int element)
{
int pos = -1,co = 0;
while(stack.size() > 0)
{
co++;
if(stack.top() == element)
{
pos = co;
break;
}
stack.pop();
}
if (pos == -1)
cout << "Element not found" << endl;
else
cout << "Element is found at position " << pos << endl;
}
// Driver code
int main()
{
stack stack ;
stack = stack_push(stack);
stack = stack_pop(stack);
stack = stack_push(stack);
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack, 6);
return 0;
}
// This code is contributed by Arnab Kundu
Java
// Java program to demonstrate
// working of LIFO
// using Stack in Java
import java.io.*;
import java.util.*;
class GFG {
// Pushing element on the top of the stack
static void stack_push(Stack stack)
{
for (int i = 0; i < 5; i++) {
stack.push(i);
}
}
// Popping element from the top of the stack
static void stack_pop(Stack stack)
{
System.out.println("Pop :");
for (int i = 0; i < 5; i++) {
Integer y = (Integer)stack.pop();
System.out.println(y);
}
}
// Displaying element on the top of the stack
static void stack_peek(Stack stack)
{
Integer element = (Integer)stack.peek();
System.out.println("Element on stack top : " + element);
}
// Searching element in the stack
static void stack_search(Stack stack, int element)
{
Integer pos = (Integer)stack.search(element);
if (pos == -1)
System.out.println("Element not found");
else
System.out.println("Element is found at position " + pos);
}
public static void main(String[] args)
{
Stack stack = new Stack();
stack_push(stack);
stack_pop(stack);
stack_push(stack);
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack, 6);
}
}
Python3
# Python3 program to demonstrate working of LIFO
# Pushing element on the top of the stack
def stack_push(stack):
for i in range(5):
stack.append(i)
return stack
# Popping element from the top of the stack
def stack_pop(stack):
print("Pop :")
for i in range(5):
y = stack[-1]
stack.pop()
print(y)
return stack
# Displaying element on the top of the stack
def stack_peek(stack):
element = stack[-1]
print("Element on stack top :", element)
# Searching element in the stack
def stack_search(stack, element):
pos = -1
co = 0
while(len(stack) > 0):
co+=1
if(stack[-1] == element):
pos = co
break
stack.pop()
if (pos == -1):
print( "Element not found")
else:
print("Element is found at position", pos)
stack = []
stack_push(stack)
stack_pop(stack)
stack_push(stack)
stack_peek(stack)
stack_search(stack, 2)
stack_search(stack, 6)
# This code is contributed by rameshtravel07.
C#
// C# program to demonstrate
// working of LIFO
// using Stack in C#
using System;
using System.Collections.Generic;
class GFG
{
// Pushing element on the top of the stack
static void stack_push(Stack stack)
{
for (int i = 0; i < 5; i++)
{
stack.Push(i);
}
}
// Popping element from the top of the stack
static void stack_pop(Stack stack)
{
Console.WriteLine("Pop :");
for (int i = 0; i < 5; i++)
{
int y = (int)stack.Pop();
Console.WriteLine(y);
}
}
// Displaying element on the top of the stack
static void stack_peek(Stack stack)
{
int element = (int)stack.Peek();
Console.WriteLine("Element on stack top : " + element);
}
// Searching element in the stack
static void stack_search(Stack stack, int element)
{
bool pos = stack.Contains(element);
if (pos == false)
Console.WriteLine("Element not found");
else
Console.WriteLine("Element is found at position " + pos);
}
// Driver code
public static void Main(String[] args)
{
Stack stack = new Stack();
stack_push(stack);
stack_pop(stack);
stack_push(stack);
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack, 6);
}
}
// This code contributed by Rajput-Ji
Javascript
输出:
Pop:
4
3
2
1
0
Element on stack top : 4
Element is found at position 3
Element not found