Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When this code is run it has errors. Could someone please help me figure this ou

ID: 3694645 • Letter: W

Question

When this code is run it has errors. Could someone please help me figure this out?

p = list;
q = next(p);

while (q! == null)
{
   if(next(q! == null)
   {
      insAfter q, x);
   }
   else
   {
      p = q;
      q = next(q);
   }
}

//e. Delete last element from list
p = list;
q = next(p);

if(p == null)
{
   System.out.println("Void deletion");
   System.exit(1);
}
else
{
   while (q! == null)
   {
      if (next (p) == null)
      {
         next(p) = null;
         freenode (q);
      }
      else
      {
         p = q;
         q = next(q);
      }
   }
}

//f. Delete nth element from list
int n;
p = list;
q = next(p);
if(p == null)
{
System.out.println("Void deletion");
System.exit(1);

}
else
{
   for (int i = 0; i < n; i++)
   {
      p = q;
      q = next(q);
   }
   next(p) = null
   freenode(q);
   }

//j. Insert an element after the nth element
int n;
p = list;
q = next(p);

for (int i = 0; i < n; i++)
{
   p = q;
   q = next(q);
}
insAfter (q, x);

//n. Return number of elements in list
p = list
int i = 0;
while (p! - null)
{
   i++;
   p = next(p);
}
System.out.println("The number of elements in list is:" + i);

Explanation / Answer

p = list;
q = next(p);

while (q! == null)
{
   if(next(q! == null)
   {
      insAfter q, x);
   }
   else
   {
      p = q;
      q = next(q);
   }
}

//e. Delete last element from list
p = list;
q = next(p);

if(p == null)
{
   System.out.println("Void deletion");
   System.exit(1);
}
else
{
   while (q! == null)
   {
      if (next (p) == null)
      {
         next(p) = null;
         freenode (q);
      }
      else
      {
         p = q;
         q = next(q);
      }
   }
}

//f. Delete nth element from list
int n;
p = list;
q = next(p);
if(p == null)
{
System.out.println("Void deletion");
System.exit(1);

}
else
{
   for (int i = 0; i < n; i++)
   {
      p = q;
      q = next(q);
   }
   next(p) = null
   freenode(q);
   }

//j. Insert an element after the nth element
int n;
p = list;
q = next(p);

for (int i = 0; i < n; i++)
{
   p = q;
   q = next(q);
}
insAfter (q, x);

//n. Return number of elements in list
p = list
int i = 0;
while (p! - null)
{
   i++;
   p = next(p);
}
System.out.println("The number of elements in list is:" + i);

in while loop , just remove ! operator or bcoz you should perform only != or == operator only and last while loop you need to use same thing but u have used !- its not valid. please check after remove above .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote