Computer Science – 10.4 Introduction to Abstract Data Types (ADT) | e-Consult
10.4 Introduction to Abstract Data Types (ADT) (1 questions)
Login to see all questions.
Click on a question to view the answer
A stack follows the LIFO (Last-In, First-Out) principle. An array is a natural fit for stack implementation. A single pointer, often called the 'top' pointer, is used to indicate the top of the stack. New elements are added to the top of the stack, and elements are removed from the top. When the stack is full, the top pointer needs to be advanced, potentially overwriting the oldest element. This is a key characteristic of array-based stack implementations.
Operations:
- push(element): Add an element to the top (top pointer incremented).
- pop(): Remove and return the element from the top (top pointer decremented).
- peek(): Return the element at the top without removing it.
- isEmpty(): Check if the stack is empty (top == -1 or top == 0, depending on the implementation).
- isFull(): Check if the stack is full (top == stack size - 1).
Advantages: Simple to implement, efficient for push and pop operations.
Disadvantages: Fixed size, potential for wasted space if the stack is rarely full, can be inefficient if frequent resizing is needed. The stack size is limited by the array's capacity.