adplus-dvertising
frame-decoration

Question

Select the appropriate code that inserts elements into the list based on the given key value.
(head and trail are dummy nodes to mark the end and beginning of the list, they do not contain any priority or element)
a)

public void insert_key(int key,Object item) 
{
 if(key<0)
 {
  Systerm.our.println("invalid");
  System.exit(0);
 }
 else
 {
  Node temp = new Node(key,item,null);
  if(count == 0)
  {
   head.setNext(temp);
   temp.setNext(trail);
  }
  else
  {
   Node dup = head.getNext();
   Node cur = head;
   while((key>dup.getKey()) && (dup!=trail))
   {
    dup = dup.getNext();
    cur = cur.getNext();
   }
   cur.setNext(temp);
   temp.setNext(dup);
  }
  count++;
 }
}

b)

public void insert_key(int key,Object item) 
{
 if(key<0)
 {
  Systerm.our.println("invalid");
  System.exit(0);
 }
 else
 {
  Node temp = new Node(key,item,null);
  if(count == 0)
  {
   head.setNext(temp);
   temp.setNext(trail);
  }
  else
  {
   Node dup = head.getNext();
   Node cur = dup;
   while((key>dup.getKey()) && (dup!=trail))
   {
    dup = dup.getNext();
    cur = cur.getNext();
   }
   cur.setNext(temp);
   temp.setNext(dup);
  }
  count++;
 }
}

c)

public void insert_key(int key,Object item) 
{
 if(key<0)
 {
  Systerm.our.println("invalid");
  System.exit(0);
 }
 else
 {
  Node temp = new Node(key,item,null);
  if(count == 0)
  {
   head.setNext(temp);
   temp.setNext(trail);
  }
  else
  {
   Node dup = head.getNext();
   Node cur = head;
   while((key>dup.getKey()) && (dup!=trail))
   {
    dup = dup.getNext();
    cur = cur.getNext();
   }
   cur.setNext(dup);
   temp.setNext(cur);
  }
  count++;
 }
}

d) None of the mentioned

a.

a

b.

b

c.

c

d.

d

Answer: (a).a

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. Select the appropriate code that inserts elements into the list based on the given key value. (head and trail are dummy nodes to mark the end and beginning of the list, they do...