Home  >  Article  >  Web Front-end  >  JavaScript Stack Using LIFO Principle

JavaScript Stack Using LIFO Principle

PHPz
PHPzOriginal
2024-08-14 10:38:04585browse

This JavaScript program implements a simple stack using an array, demonstrating key operations like adding, removing, and displaying elements according to the Last In, First Out (LIFO) principle.

Initial Array (Data):

let Data = [10, 20, 30, 40, 50, 60, 70, 80, 90];

  • The array Data starts with 9 elements, ranging from 10 to 90.

Displaying the Original Array:

console.log("Varignal Array ", Data);

  • This line prints the original array to the console.

AddEle Function:

function AddEle(val) {
  if (isFull()) {
    console.log("Array is Full ,Element Can't add ..!");
  } else {
    console.log(`Add New >> ${val} Element..!`);
    Data.push(val);
  }
}

  • This function adds a new element (val) to the array.
  • It first checks if the array is full using the isFull() function.
  • If the array is full (10 elements), it prints a message indicating that no more elements can be added.
  • Otherwise, it adds the new element to the end of the array using push(val) and prints a confirmation message.

isFull Function:

function isFull() {
  if (Data.length >= 10) {
    return true;
  } else {
    return false;
  }
}

  • This function checks if the array has reached its capacity (10 elements).
  • It returns true if the array length is 10 or more, indicating it's full, and false otherwise.

Remove Function:

function Remove(item) {
  if (isEmpty()) {
    console.log("Array is empty..!");
  } else {
    console.log("Removed Arry's Last Element..!");
    Data.pop(item);
  }
}

  • This function removes the last element from the array.
  • It first checks if the array is empty using the isEmpty() function.
  • If the array is empty, it prints a message indicating that no elements can be removed.
  • Otherwise, it removes the last element using pop() and prints a message indicating the removal.

isEmpty Function:

function isEmpty() {
  if (Data.length === 0) {
    return true;
  } else {
    return false;
  }
}

  • This function checks if the array is empty.
  • It returns true if the array length is 0, indicating it's empty, and false otherwise.

Display Function:

function Display() {
  console.log("Upadted Array ..!", Data);
}

  • This function prints the current state of the array to the console.

Executing the Functions:

AddEle(200);  // Attempts to add 200 to the array.
Remove();     // Removes the last element from the array.
Display();    // Displays the updated array.

Output:

JavaScript Stack Using LIFO Principle

The above is the detailed content of JavaScript Stack Using LIFO Principle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn