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
The key operations associated with a stack ADT are:
- Push: Adds an element to the top of the stack.
- Pop: Removes the element from the top of the stack.
- Peek: Returns the element at the top of the stack without removing it.
- isEmpty: Checks if the stack is empty.
Here's how these operations can be implemented using a linked list:
- Push: A new node is created containing the data to be added. This new node's 'next' pointer is set to point to the current top of the stack (the previous top node). The top of the stack is then updated to point to this new node.
- Pop: The top node is removed from the stack. The 'next' pointer of the previous top node is updated to skip the removed node, effectively making the previous top node the new top. If the stack is empty, an error condition (e.g., underflow) is signaled.
- Peek: The data in the top node is accessed, but the node itself is not removed.
- isEmpty: The stack is empty if the top node's 'next' pointer is null.
Diagram:
Before Push: Data: 10 -> Next: null |
After Push: Data: 10 -> Next: Data: 5 |
Before Pop: Data: 10 -> Next: Data: 5 |
After Pop: Data: 5 -> Next: null |