LIFO oxirgi kiruvchi, birinchi chiqadi degan qisqartma hisoblanadi . Bu birinchi element oxirgi marta qayta ishlanadigan va oxirgi element birinchi bo'lib qayta ishlanadigan ma'lumotlar tuzilmalari bilan ishlash usuli .
Haqiqiy hayotdan misol:
Ushbu misolda quyidagi narsalarni hisobga olish kerak:
To'plarni ushlab turadigan chelak bor.
Paqirga har xil turdagi to'plar kiritiladi.
Paqirga oxirgi kirgan to'p birinchi bo'lib chiqariladi.
Paqirga oxirgi navbatda kirgan to'p uning ustidagi to'pdan keyin (yangisi) chiqariladi.
Shunday qilib, chelakka birinchi bo'lib kirgan to'p chelakni oxirgi tark etadi.
Shunday qilib, chelakka kirgan oxirgi to'p (Ko'k) birinchi bo'lib chelakka kirgan birinchi to'p (Qizil) oxirgi bo'lib chiqariladi.
Bu oxirgi kiruvchi birinchi chiqish yondashuvi yoki LIFO sifatida tanilgan.
LIFO qayerda ishlatiladi:
Ma'lumotlar tuzilmalari - Stacks va boshqa Stacks variantlari kabi ba'zi ma'lumotlar tuzilmalari ma'lumotlarni qayta ishlash uchun LIFO yondashuvidan foydalanadi.
Eng so'nggi ma'lumotlarni olish - Ba'zida kompyuterlar massivdan yoki ma'lumotlar buferidan ma'lumotlar chiqarilganda LIFO-dan foydalanadilar. Eng so'nggi ma'lumotlarni olish kerak bo'lganda, LIFO usuli qo'llaniladi.
LIFO uchun dastur misollari - Stack ma'lumotlar strukturasidan foydalanish:
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;
}
# 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)
using namespace std;
int findSmallestElement(int arr[], int n){
/* We are assigning the first array element to
* the temp variable and then we are comparing
* all the array elements with the temp inside
* loop and if the element is smaller than temp
* then the temp value is replaced by that. This
* way we always have the smallest value in temp.
* Finally we are returning temp.
*/
int temp = arr[0];
for(int i=0; iif(temp>arr[i]) {
temp=arr[i];
}
}
return temp;
}
int main() {
int n;
cout<<"Enter the size of array: ";
cin>>n; int arr[n-1];
cout<<"Enter array elements: ";
for(int i=0; icin>>arr[i];
}
int smallest = findSmallestElement(arr, n);
cout<<"Smallest Element is: "<return 0;
}