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

Write an algorithm to perform each of the following operations append an element

ID: 3532221 • Letter: W

Question

Write an algorithm to perform each of the following operations append an element to the end of the list? Delete the last element from the list?Delete the nth element from the list? Create a list containing the union of the elements of the two lists? Insert an element after the nth element of the list.? Write an algorithm to perform each of the following operations append an element to the end of the list? Delete the last element from the list?Delete the nth element from the list? Create a list containing the union of the elements of the two lists? Insert an element after the nth element of the list.?

Explanation / Answer

import java.util.Scanner;




public class IntNode

{

private int data;

private IntNode link;


IntNode()

{

data=0;

link=null;

}


IntNode(int num)

{

setData(num);

link=null;

}



public void setData(int data)

{

this.data=data;

}

public int getdata()

{

return data;

}


public static IntNode addAtBeg(IntNode head,int data)

{

if(head==null)

{

IntNode temp=new IntNode();

temp.data=data;

temp.link=null;

head=temp;

return head;

}

else

{

IntNode temp=new IntNode();

temp.data=data;

temp.link=head;

head=temp;

return head;


}

}

public static IntNode append(IntNode head,int data)

{

if(head==null)

{

IntNode temp=new IntNode();

temp.data=data;

temp.link=null;

head=temp;

return head;

}

else

{

IntNode current=head;

while(current.link!=null)

current=current.link;

IntNode temp=new IntNode();

temp.data=data;

temp.link=null;

current.link=temp;

return temp;

}

}

public static IntNode addAfterNth(IntNode head,int data,int n)

{

if(head==null)

{

System.out.println("list doesn't have "+n +"elements");

return null;

}

else

{

IntNode current=head;

for(int i=1;i<n;i++)

{

current=current.link;

if(current==null)

{

System.out.println("list doesn't have "+n +"elements");

}

}

IntNode temp=new IntNode();

temp.data=data;

temp.link=current.link;

current.link=temp;

return temp;

}

}

public static IntNode deleteAtEnd(IntNode head)

{

if(isEmpty(head))

return null;

else

{

IntNode prev=head;;

IntNode current=head;

while(current.link!=null)

{

prev=current;

current=current.link;

}

prev.link=null;

return current;


}



}

public static IntNode union(IntNode head1,IntNode head2)

{

IntNode temp1=head1;

IntNode temp2=head2;

while(temp1.link!=null)

temp1=temp1.link;

temp1.link=head2;

return head1;

}


public static IntNode deleteNth(IntNode head,int n)

{

if(isEmpty(head))

return null;

else

{

IntNode prev=head;;

IntNode current=head;

for(int i=1;i<n;i++)

{

prev=current;

current=current.link;

if(current==null)

{

System.out.println("list have less than "+ n+ "element");

return null;

}

}

prev.link=current.link;

return current;


}



}


public static boolean isEmpty(IntNode head)

{

if(head!=null)

return false;

else

return false;


}



public static void Display(IntNode head)

{

if(head==null)

return;

IntNode curr=head;

System.out.println("data components are");


while(curr!=null)

{

System.out.println(curr.data);

curr=curr.link;

}


}


public static void main(String[] args)

{

IntNode head=null;

Scanner input=new Scanner(System.in);

int option=0;

int num;

int n;

do

{

System.out.println("choose one of the option");

System.out.println("1. append");

System.out.println("2. delete at end");

System.out.println("3. dispaly");

System.out.println("4. delete nth element list");

System.out.println("5. add after nth element in list");

System.out.println("6. Exit");

option=input.nextInt();


switch(option)

{

case 1:

System.out.println(" enter the element you want to add at the end");

num=input.nextInt();

head=IntNode.append(head, num);

break;

case 2:

System.out.print(" deleteded element:");

System.out.println(IntNode.deleteAtEnd(head).getdata());


break;

case 3:

IntNode.Display(head);

break;

case 4:

System.out.println("Enter the value of n");

n=input.nextInt();

IntNode.deleteNth(head,n);

break;

case 5:

System.out.println(" enter the element you want to add after n element");

num=input.nextInt();

System.out.println("Enter the value of n");

n=input.nextInt();

IntNode.addAfterNth(head, num, n);

break;

case 6:

break;

case 7:


break;


default:

System.out.println("wrong selection");




}

} while(option<=5);

}

}

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