# Stack using Linked List | Data Structure MCQs

1.

What is the complexity of searching for a particular element in a Singly Linked List?

A.) O(n)
B.) O(1)
C.) logn
D.) nlogn

Singly Linked List allows you to traverse in one direction only, hence if the element to be found is at the tail of the list, the complexity of searching is O(n) to traverse the entire list.

2.

Which of the following statements are correct with respect to Singly Linked List(SLL) and Doubly Linked List(DLL)?

A.) Complexity of Insertion and Deletion at known position is O(n) in SLL and O(1) in DLL
B.) SLL uses lesser memory per node than DLL
C.) DLL has more searching power than SLL
D.) All of these

To insert and delete at known positions requires complete traversal of the list in worst case in SLL, SLL consists of an item and a node field, while DLL has an item and two node fields, hence SLL occupies lesser memory, DLL can be traversed both ways(left and right), while SLL can traverse in only one direction, hence more searching power of DLL.

3.

Given below is the Node class to perform basic list operations and a Stack class with a no arg constructor.
Select from the options the appropriate pop() operation that can be included in the Stack class. Also ‘first’ is the top-of-the-stack.

```class Node
{
protected Node next;
protected Object ele;
Node()
{
this(null,null);
}
Node(Object e,Node n)
{
ele=e;
next=n;
}
public void setNext(Node n)
{
next=n;
}
public void setEle(Object e)
{
ele=e;
}
public Node getNext()
{
return next;
}
public Object getEle()
{
return ele;
}
}

class Stack
{
Node first;
int size=0;
Stack()
{
first=null;
}
}```

A.)
```public Object pop()
{
if(size == 0)
System.out.println("underflow");
else
{
Object o = first.getEle();
first = first.getNext();
size--;
return o;
}
}```
B.)
```public Object pop()
{
if(size == 0)
System.out.println("underflow");
else
{
Object o = first.getEle();
first = first.getNext().getNext();
size--;
return o;
}
}```
C.)
```public Object pop()
{
if(size == 0)
System.out.println("underflow");
else
{
first = first.getNext();
Object o = first.getEle();
size--;
return o;
}
}```
D.)
```public Object pop()
{
if(size == 0)
System.out.println("underflow");
else
{
first = first.getNext().getNext();
Object o = first.getEle();
size--;
return o;
}
}```

pop() should return the Object pointed to by the node ‘first’. The sequence of operations is, first, get the element stored at node ‘first’ using getEle(), and second, make the node point to the next node using getNext().

4.

What does the following function do?

```public Object some_func()throws emptyStackException
{
if(isEmpty())
throw new emptyStackException("underflow");
return first.getEle();
}```

A.) pop
B.) delete the top-of-the-stack element
C.) retrieve the top-of-the-stack element
D.) none of these

This code is only retrieving the top element, note that it is not equivalent to pop operation as you are not setting the ‘next’ pointer point to the next node in sequence.

5.

What is the functionality of the following piece of code?

```public void display()
{
if(size == 0)
System.out.println("underflow");
else
{
Node current = first;
while(current != null)
{
System.out.println(current.getEle());
current = current.getNext();
}
}
}```

A.) reverse the list
B.) display the list
C.) display the list excluding top-of-the-stack-element
D.) reverse the list excluding top-of-the-stack-element

An alias of the node ‘first’ is created which traverses through the list and displays the elements.

6.

What does ‘stack overflow’ refer to?

A.) accessing item from an undefined stack
B.) adding items to a full stack
C.) removing items from an empty stack
D.) index out of bounds exception

Adding items to a full stack is termed as stack underflow.

7.

Given below is the Node class to perform basic list operations and a Stack class with a no arg constructor. Select from the options the appropriate push() operation that can be included in the Stack class. Also ‘first’ is the top-of-the-stack.

```class Node
{
protected Node next;
protected Object ele;
Node()
{
this(null,null);
}
Node(Object e,Node n)
{
ele=e;
next=n;
}
public void setNext(Node n)
{
next=n;
}
public void setEle(Object e)
{
ele=e;
}
public Node getNext()
{
return next;
}
public Object getEle()
{
return ele;
}
}

class Stack
{
Node first;
int size=0;
Stack()
{
first=null;
}
}```

A.)
```public void push(Object item)
{
Node temp = new Node(item,first);
first = temp;
size++;
}```
B.)
```public void push(Object item)
{
Node temp = new Node(item,first);
first = temp.getNext();
size++;
}```
C.)
```public void push(Object item)
{
Node temp = new Node();
first = temp.getNext();
first.setItem(item);
size++;
}```
D.) none of these

To push an element into the stack, first create a new node with the next pointer point to the current top-of-the-stack node, then make this node as top-of-the-stack by assigning it to ‘first’.

8.

Consider these functions:
push() : push an element into the stack
pop() : pop the top-of-the-stack element
top() : returns the item stored in top-of-the-stack-node
What will be the output after performing these sequence of operations

```push(20);
push(4);
top();
pop();
pop();
pop();
push(5);
top();```

A.) 20
B.) 4
C.) stack underflow
D.) 5

20 and 4 which were pushed are popped by the two pop() statements, the recent push() is 5, hence top() returns 5.

9.

Which of the following data structures can be used for parentheses matching?

A.) n-ary tree
B.) queue
C.) priority queue
D.) stack

For every opening brace, push it into the stack, and for every closing brace, pop it off the stack. Do not take action for any other character. In the end, if the stack is empty, then the input has balanced parentheses.

10.

Minimum number of queues to implement stack is,

A.) 1
B.) 2
C.) 3
D.) 4