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

Chapter 16 question No. 3 Extend the class LinkedListClass by adding the followi

ID: 3534708 • Letter: C

Question

Chapter 16 question No. 3

Extend the class LinkedListClass by adding the following operations:

a. Write a method that returns the info of the kth element of the linked list. If no such element exists, terminate the program.

b. Write a method that delete the kth element of the linked list. If no such element exists, output an appropriate message.

Provide the definitions of these methods in the class LinkedListClass. Also write a program to test these methods. (Use either the class UnorderedLinkedList of the class OrderedLinkedList to test your method

Explanation / Answer

import java.io.*;

class Node

{

int data;

Node next;

}

class Operations

{

public Node insertfront(int item,Node header)throws IOException

{

Node newnode=new Node();

newnode.next=null;

if(header!=null)

{

newnode.next=header;

}

header=newnode;

newnode.data=item;

return(header);

}

public Node insertlast(int item,Node header)throws IOException

{

Node newnode=new Node();

Node ptr;

if(header==null)

{

header=newnode;

}

else

{

ptr=header;

while(ptr.next!=null)

{

ptr=ptr.next;

}

ptr.next=newnode;

}

newnode.data=item;

newnode.next=null;

return(header);


}



public Node insertbtw(int key,int item,Node header)throws IOException

{

Node ptr;

ptr=header;

while(ptr.data!=key&&ptr.next!=null)

{

ptr=ptr.next;

}

if(ptr.data==key)

{

Node newnode=new Node();

newnode.next=ptr.next;

ptr.next=newnode;

newnode.data=item;

}

else

{System.out.println("Key not found");}


return(header);


}



public Node deletefront(Node header)throws IOException

{

if(header==null)

System.out.print("Its empty There is no element to delete");

else

header=header.next;

return(header);

}


public Node deletelast(Node header)throws IOException

{

Node ptr,ptr1;

ptr=header;

ptr1=null;

while(ptr.next!=null)

{ ptr1=ptr;

ptr=ptr.next;

}

ptr1.next=null;

return(header);

}



public Node deletebtw(int key,Node header)throws IOException

{

Node ptr,ptr1;

ptr=header;

ptr1=null;

while(ptr.data!=key&&ptr.next!=null)

{ptr1=ptr;

ptr=ptr.next;

}

if(ptr.data==key)

ptr1.next=ptr.next;

else

System.out.println("Key not found");

return(header);

}


public void traverse(Node header)throws IOException

{

while(header!=null)

{

System.out.print(" "+header.data);

header=header.next;

}

}


public void search(int key,Node header)throws IOException

{

Node ptr=header;

while(ptr.data!=key&&ptr.next!=null)

{

ptr=ptr.next;

}

if(ptr.data==key)

System.out.println("Search success key found");

else System.out.println("Search failed key not found");

}


}



class SingLinkList

{

public static void main(String [] args)throws IOException

{

Operations op=new Operations();

Node header;

header=null;

int ch,ins,del,item,key,n=0;

String s;

do

{

System.out.println("Enter your choice-1 to Insert,2 to Delete,3 to Search and 4 to Traverse");

InputStreamReader read=new InputStreamReader(System.in);

BufferedReader ob =new BufferedReader(read);

s=ob.readLine();

ch=Integer.parseInt(s);

switch(ch)

{

case 1:{System.out.println("Enter the item to be inserted");


s=ob.readLine();

item=Integer.parseInt(s);

System.out.println("Where do you wanna insert?");

System.out.println("Enter 1 to Insert at beginning,2 Insert at end,3 to Insert inbetween");


s=ob.readLine();

ins=Integer.parseInt(s);

switch(ins)

{

case 1:header=op.insertfront(item,header);break;

case 2:header=op.insertlast(item,header);break;

case 3:System.out.println("Enter the key");


s=ob.readLine();

key=Integer.parseInt(s);

header=op.insertbtw(key,item,header);

break;

}

op.traverse(header);

break;}

case 2:{System.out.println("From where do you wanna delete?");

System.out.println("Enter 1 to del from beginning 2 to del from end and 3 to del an item inbetween");


s=ob.readLine();

del=Integer.parseInt(s);

switch(del)

{

case 1:{ header=op.deletefront(header);break;}

case 2:{header=op.deletelast(header);break;}

case 3:{System.out.println("Enter the key");


s=ob.readLine();

key=Integer.parseInt(s);

header=op.deletebtw(key,header);

op.traverse(header);

break;}

}

}

case 3:{System.out.println("Enter the key");

s=ob.readLine();

key=Integer.parseInt(s);

op.search(key,header);op.traverse(header);break;}

case 4:{op.traverse(header);break;} default : System.out.println("WRONG CHOICE.... PLEASE REENTER ;;;");

}

System.out.println();

System.out.println("Enter 1 to continue else any other number to quit:...");

s=ob.readLine();

n=Integer.parseInt(s);

}

while(n==1);

System.out.println(" thank you ");

} }

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