6 Lecture

CS301

Midterm & Final Term Short Notes

Stack From the Previous Lecture

A stack is an abstract data type that represents a collection of elements with two main operations: push, which adds an element to the top of the stack, and pop, which removes the top element from the stack. It follows the Last-In-First-Out (LIF


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. Which of the following is NOT a characteristic of a stack? a. Follows LIFO principle b. Has two main operations: push and pop c. Can only be implemented using arrays d. Topmost element is the last one added

Answer: c. Can only be implemented using arrays

  1. What is the time complexity of push and pop operations in a stack implemented using an array? a. O(1) b. O(log n) c. O(n) d. O(n^2)

Answer: a. O(1)

  1. Which data structure is often used to implement a stack? a. Array b. Linked List c. Queue d. Binary Tree

Answer: b. Linked List

  1. Which of the following is NOT a common application of stacks? a. Reversing a string b. Evaluating postfix expressions c. Implementing depth-first search in a graph d. Sorting an array

Answer: d. Sorting an array

  1. Which operation in a stack does not modify the stack? a. Push b. Pop c. Peek d. Size

Answer: c. Peek

  1. What happens when we try to pop an element from an empty stack? a. The program crashes b. An error message is displayed c. The topmost element becomes NULL d. Nothing happens

Answer: b. An error message is displayed

  1. Which of the following is NOT a disadvantage of using an array to implement a stack? a. Fixed size b. Elements must be contiguous in memory c. Dynamic resizing is difficult d. Push operation is slower than pop operation

Answer: d. Push operation is slower than pop operation

  1. What is the maximum number of elements a stack implemented using an array can hold if its size is n? a. n b. n-1 c. 2n d. 2n-1

Answer: b. n-1

  1. Which of the following is NOT a potential application of stacks in computer science? a. Function calls and return values b. Undo and redo operations c. Implementing breadth-first search in a graph d. Checking for balanced parentheses in an expression

Answer: c. Implementing breadth-first search in a graph

  1. What is the time complexity of searching for an element in a stack implemented using a linked list? a. O(1) b. O(log n) c. O(n) d. O(n^2)

Answer: c. O(n)



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a stack, and how does it follow the LIFO principle? Answer: A stack is an abstract data type that represents a collection of elements with two main operations: push, which adds an element to the top of the stack, and pop, which removes the top element from the stack. It follows the Last-In-First-Out (LIFO) principle, where the last element added is the first to be removed.

  2. How can you implement a stack using an array? What are the advantages and disadvantages of this approach? Answer: A stack can be implemented using an array by maintaining a variable to keep track of the topmost element's index. Advantages of this approach include constant time complexity for push and pop operations and efficient use of memory. Disadvantages include fixed size and difficulty in dynamic resizing.

  3. How can you implement a stack using a linked list? What are the advantages and disadvantages of this approach? Answer: A stack can be implemented using a linked list by using the head of the list to represent the topmost element. Advantages of this approach include dynamic resizing, efficient use of memory, and easy implementation. Disadvantages include slower access time than an array-based implementation.

  4. What is the purpose of the peek operation in a stack? Answer: The peek operation allows you to view the topmost element in the stack without removing it.

  5. What happens when you try to pop an element from an empty stack? Answer: When you try to pop an element from an empty stack, an error message is displayed.

  6. What is the time complexity of push and pop operations in a stack implemented using a linked list? Answer: The time complexity of push and pop operations in a stack implemented using a linked list is O(1).

  7. How can stacks be used to evaluate postfix expressions? Answer: Stacks can be used to evaluate postfix expressions by iterating over the expression and performing operations based on the current element. When an operand is encountered, it is pushed onto the stack. When an operator is encountered, the two most recent operands are popped from the stack, and the operation is performed, with the result being pushed back onto the stack.

  8. What is a potential application of stacks in checking for balanced parentheses in an expression? Answer: A stack can be used to check for balanced parentheses in an expression by pushing opening parentheses onto the stack and popping them off when a closing parenthesis is encountered. If the stack is empty at the end of the expression, then the parentheses are balanced.

  9. What is a potential disadvantage of using an array to implement a stack? Answer: A potential disadvantage of using an array to implement a stack is that the size is fixed and cannot be dynamically resized.

  10. What is the time complexity of searching for an element in a stack implemented using an array? Answer: The time complexity of searching for an element in a stack implemented using an array is O(n), as you need to iterate over each element to find the desired one.

A stack is a fundamental data structure in computer science that follows the Last-In-First-Out (LIFO) principle. It is an abstract data type that represents a collection of elements, with two primary operations: push and pop. The push operation adds an element to the top of the stack, while the pop operation removes the top element from the stack. There are two main ways to implement a stack: using an array or a linked list. In the array-based implementation, a variable keeps track of the topmost element's index, while in the linked list implementation, the head of the list represents the topmost element. One of the essential operations in a stack is the peek operation, which allows you to view the topmost element in the stack without removing it. When you try to pop an element from an empty stack, an error message is displayed. Stacks have many applications, including evaluating postfix expressions, checking for balanced parentheses in an expression, and implementing undo-redo functionality in text editors. In postfix expression evaluation, stacks are used to keep track of operands and operators and perform the necessary calculations. In checking for balanced parentheses, the stack is used to push opening parentheses onto the stack and pop them off when a closing parenthesis is encountered. If the stack is empty at the end of the expression, then the parentheses are balanced. One potential disadvantage of using an array to implement a stack is that the size is fixed and cannot be dynamically resized. However, the advantage of an array-based implementation is that push and pop operations have constant time complexity. Linked list-based implementations, on the other hand, have dynamic resizing and efficient memory usage but slower access time than array-based implementations. Overall, understanding stacks and their various implementations and applications is crucial for computer science students and professionals alike.