Which statement best describes the memory usage difference between a stack implemented using a linked list and one using an array?
Think about how linked lists store data and what extra information they keep for each element.
Linked list stacks store each element in a node that contains the data and a pointer to the next node, which adds extra memory overhead per element. Array stacks allocate a fixed-size block of memory, which may be larger than needed but does not store pointers per element.
What is the printed output after executing the following C# code that uses a linked list stack?
public class Node { public int Data; public Node Next; public Node(int data) { Data = data; Next = null; } } public class LinkedListStack { private Node top = null; public void Push(int data) { Node newNode = new Node(data); newNode.Next = top; top = newNode; } public int Pop() { if (top == null) throw new InvalidOperationException("Stack is empty"); int value = top.Data; top = top.Next; return value; } public void PrintStack() { Node current = top; while (current != null) { Console.Write(current.Data + " -> "); current = current.Next; } Console.WriteLine("null"); } } var stack = new LinkedListStack(); stack.Push(10); stack.Push(20); stack.Push(30); stack.Pop(); stack.PrintStack();
Remember that push adds to the top and pop removes from the top.
Push adds 10, then 20, then 30 on top. Pop removes 30. Remaining stack top is 20, then 10.
Which statement correctly explains the performance trade-off when using an array stack that resizes dynamically compared to a linked list stack?
Consider how array resizing affects push operations and how linked lists manage memory.
Array stacks resize by allocating a larger array and copying elements, causing occasional slow push operations. However, average push/pop is fast (amortized O(1)). Linked list stacks have consistent O(1) push/pop but use extra memory for pointers.
What is the output of the following C# code that uses an array-based stack after the operations?
public class ArrayStack { private int[] arr = new int[3]; private int top = -1; public void Push(int data) { if (top == arr.Length - 1) { int[] newArr = new int[arr.Length * 2]; for (int i = 0; i < arr.Length; i++) newArr[i] = arr[i]; arr = newArr; } arr[++top] = data; } public int Pop() { if (top == -1) throw new InvalidOperationException("Stack is empty"); return arr[top--]; } public void PrintStack() { for (int i = top; i >= 0; i--) { Console.Write(arr[i] + " -> "); } Console.WriteLine("null"); } } var stack = new ArrayStack(); stack.Push(5); stack.Push(10); stack.Push(15); stack.Push(20); stack.Pop(); stack.PrintStack();
Remember that pop removes the top element and that the array resizes when full.
Push adds 5, 10, 15, 20. Array resizes when pushing 20. Pop removes 20. Remaining stack is 15, 10, 5 from top to bottom.
In a real-time system where consistent operation time is critical, which stack implementation is generally more suitable and why?
Think about operation time consistency and memory allocation delays.
Linked list stacks have consistent O(1) push and pop times without pauses for resizing. Array stacks may have occasional delays during resizing, which can violate real-time constraints.
