# Which Data Structure Allows First in Last Out Operation?

//

Heather Bennett

Which Data Structure Allows First in Last Out Operation?

When it comes to organizing and managing data, data structures play a crucial role. They provide an efficient way to store and retrieve data based on specific requirements. One common requirement is the need to perform a first-in-last-out (FILO) operation, where the last element inserted is the first one to be removed.

## The Stack Data Structure

The data structure that allows for a FILO operation is called a stack. A stack is a linear data structure that follows the Last In First Out (LIFO) principle. It can be imagined as a stack of plates, where you can only access the topmost plate.

In a stack, two main operations are performed:

• Push: This operation adds an element to the top of the stack.
• Pop: This operation removes and returns the topmost element from the stack.

The push operation increases the size of the stack, while the pop operation decreases it. The topmost element always represents the most recently added item, making it easily accessible.

### Implementation of Stack

In programming languages like C++, Java, or Python, you can implement a stack using arrays or linked lists. Both approaches have their own advantages and trade-offs depending on your requirements.

#### Array-based Stack Implementation

An array-based implementation is simple and intuitive. You can use an array with fixed size or dynamically resize it based on your needs. The top of the stack can be represented by a variable that keeps track of the index of the last inserted element.

``````
class Stack {
private int[] data;
private int top;

public Stack(int size) {
data = new int[size];
top = -1;
}

public void push(int element) {
if (top == data.length - 1) {
System.out.println("Stack Overflow");
return;
}
data[++top] = element;
}

public int pop() {
if (top == -1) {
System.println("Stack Underflow");
return -1;
}
return data[top--];
}
}
```
```

A linked list-based implementation provides more flexibility in terms of size. Each element in the stack is represented by a node that contains the element itself and a reference to the next node. The top of the stack is represented by the first node.

``````
class Node {
private int data;
private Node next;

public Node(int data) {
this.data = data;
}

public void setNext(Node next) {
this.next = next;
}

public Node getNext() {
return next;
}

public int getData() {
return data;
}
}

class Stack {
private Node top;

public void push(int element) {
Node newNode = new Node(element);

if (top == null)
top = newNode;
else {
newNode.setNext(top);
top = newNode;
}
}

public int pop() {
if (top == null) {
System.println("Stack Underflow");
return -1;
}
int poppedValue = top.getData();
top = top.getNext();
return poppedValue;
}
}
```
```

## Common Use Cases of Stacks

Stacks have various applications in computer science and programming. Here are a few common use cases:

• Function Call Stack: Stacks are used to manage function calls in programming languages. When a function is called, it gets pushed onto the stack, and when it returns, it gets popped.
• Expression Evaluation: Stacks can be used to evaluate expressions by converting them from infix to postfix or prefix notation.
• Undo/Redo Functionality: Stacks can be utilized to implement undo and redo functionality in applications.
• Balanced Parentheses: Stacks can help determine whether an expression has balanced parentheses or not.

In conclusion, if you need a data structure that allows for a first-in-last-out operation, a stack is your answer. It provides an efficient way to manage data by following the Last In First Out (LIFO) principle.

Whether you choose an array-based or linked list-based implementation depends on your specific requirements and constraints. So dive into stacks and unlock their potential in solving various programming problems!