Q.

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.  
public void push(Object item)
{
	Node temp = new Node();
	first = temp.getNext.getNext();
	first.setItem(item);
	size++;
}
Similar Questions
1. In a Stack the command to access nth element from the top of the stacks will be
A.  S[Top-n] B.  S [Top+n] C.  S [top-n-1] D.  None of the above
2. Consider the usual algorithm for determining whether a sequence of parentheses is balanced. What is the maximum number of parentheses that will appear on the stack AT ANY ONE TIME when the algorithm analyzes: (()(())(()))
A.  1 B.  2 C.  3 D.  4
3. ………… is very useful in situation when data have to stored and then retrieved in reverse order.
A.  Stack B.  Queue C.  List D.  Link list
4. Stack is used for
A.  CPU Resource Allocation B.  Breadth First Traversal C.  Recursion D.  None of the above
5. push() and pop() functions are found in
A.  queues B.  lists C.  stacks D.  trees
6. If locality is a concern, you can use _______ to traverse the graph.
A.  Breadth First Search B.  Depth First Search C.  Either BFS or DFS D.  None of the above!
7. Aposterior analysis are more accurate than apriori analysis because −
A.  it contains the real data. B.  it assumes all other factors to be dynamic. C.  it assumes all other factors to be constant. D.  it is a result of reverse-engineering.
8. Prefix notation is also known as
A.  Reverse Polish Notation B.  Reverse Notation C.  Polish Reverse Notation D.  Polish Notation
9. In conversion from prefix to postfix using stack data-structure, if operators and operands are pushed and popped exactly once, then the run-time complexity is
A.  Ο(1) B.  Ο(n) C.  Ο(log n) D.  Ο(n2)
10. In C programming, when we remove an item from bottom of the stack, then −
A.  The stack will fall down. B.  Stack will rearranged items. C.  It will convert to LIFO D.  This operation is not allowed.
DATA STRUCTURE TOPICS