Introduction To Data Structures - Study Mode

[#196] 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()
push(5)
top()
Correct Answer

(D) 5

[#197] What is the functionality of the following piece of code? public int function(int data)
{
Node temp = head
int var = 0
while(temp != null)
{
if(temp.getData() == data)
{
return var
}
var = var+1
temp = temp.getNext()
}
return Integer.MIN_VALUE
}
Correct Answer

(C) Find and return the position of the given element in the list

[#198] What is the functionality of the following piece of code? public Object delete_key()
{
if(count == 0)
{
System.out.println("Q is empty")
System.exit(0)
}
else
{
Node cur = head.getNext()
Node dup = cur.getNext()
Object e = cur.getEle()
head.setNext(dup)
count--
return e
}
}
Correct Answer

(C) Delete the first element in the list

[#199] Select the code snippet which returns the top of the stack.
Correct Answer

(C) public int top() { if(q1.size()>0) { return q1.peek() } else if(q2.size()>0) { return q2.peek() } return 0 }

[#200] 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
}
}
Correct Answer

(A) public void push(Object item) { Node temp = new Node(item,first) first = temp size++ }