Stack - Data Structure Java

Ace your homework & exams now with Quizwiz!

Real world applications of a stack

Expression evaluation and conversion (infix to postfix, prefix, etc.) Backtracking (like maze solving problems, the "undo" function in software) Call stack (used for managing function calls in programming languages) Browser back button (the history of web pages you visited)

Not handling dynamic resizing

If using an array implementation, you must be cautious about stack size.

Describe Dynamic Arrays (Resizing) of a stack

Instead of having a fixed-size array, you can have an array that doubles its size when it's full. This way, you don't have to know the maximum number of items beforehand. When the array is half empty, you can also shrink it to free memory.

Describe Array Implementation of a stack

Involves static memory allocation. You need to define the size beforehand. Although this method is memory-efficient, the limitation is that the size cannot be modified once defined.

Stack is outdated, what should you use instead?

The Java's built-in Stack class is considered somewhat outdated. For modern uses, the Deque (which stands for "double ended queue") is recommended. You can use it as a stack by using its methods push(), pop(), and peek().

How would you visualize a stack

Think of a stack like a stack of plates. You add a plate to the top and you take a plate off the top. You cannot easily remove a plate from the middle or bottom.

Time and Space Complexity of stack operations

push(): O(1) pop(): O(1) peek(): O(1) isEmpty(): O(1)

Define a stack

A stack is a linear data structure that follows the LIFO (Last In First Out) principle. It means the last element added to the stack will be the first element to be removed.

Stack Underflow

Trying to pop an element from an empty stack.

What should you consider when working with a stack?

When working with stacks, it's essential to consider and handle potential overflows (when pushing onto a full stack) and underflows (when popping from an empty stack).

Explain Stack Overflow

When you keep pushing elements onto the stack without popping, you may run out of memory. In programming terms, this term also relates to the situation where there are too many recursive function calls, filling up the call stack.

What are the core operations of a stack?

push(): Adds an element to the top of the stack. pop(): Removes and returns the top element from the stack. If the stack is empty, an error or exception occurs. peek(): Returns the top element without removing it. isEmpty(): Checks if the stack is empty. size(): Returns the number of elements in the stack.

What's java's built in class for a stack

import java.util.Stack;

How do you implement a stack with an array?

public class ArrayStack { private int maxSize; private int top; private int[] stackArray; public ArrayStack(int size) { maxSize = size; stackArray = new int[maxSize]; top = -1; } public void push(int value) { if (top == maxSize - 1) { System.out.println("Stack is full."); } else { stackArray[++top] = value; } } public int pop() { if (top == -1) { System.out.println("Stack is empty."); return -1; } else { return stackArray[top--]; } } public int peek() { if (top == -1) { System.out.println("Stack is empty."); return -1; } return stackArray[top]; } public boolean isEmpty() { return (top == -1); } public int size() { return top + 1; } }

How else can you implement a stack?

using linked list


Related study sets

Probability and Statistics in Data Science using Python

View Set

Chapter 7: Drug misuse, abuse, & addiction

View Set

Chapter 57: Care of Pts With Inflammatory Intestinal Disorders

View Set

Chapter 3 Income and Taxes Review (Money Matters)

View Set

CHAPTER 4 WHAT IS A CONTRACT?( types of contracts

View Set