Question

Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k...

Add the following method to xxxxxp3:
// deletes x from sorted list k if exist, k = 0, 1, 2
private void delete(x, k)
// returns position of x in sorted list k if exist otherwise, -1. k = 0, 1, 2
private int search(x, k)

import java.util.Scanner;

class xxxxxp3{

private node[] head = new node[3];

private class node{

int num;

node link;

node(int x){

num=x;

link = null;

}

}

public void prtlst(int k){

System.out.printf("\nContents of List-%d:",k);

for(node cur = head[k];cur!=null;cur = cur.link)

System.out.printf("%4d ",cur.num);

}

public void insertsorted(int x , int k){

node current = head[k];

node newNode = (new node(x));

if(current==null ||current.num > x){

newNode.link = current;

head[k]=newNode;

return;

}

node previous = current;

while (current != null && current.num < x)

{

previous = current;

current = current.link;

}

previous.link = newNode;

newNode.link = current;

}

public node merge(){

node list1 = head[0];

node list2 = head[1];

node newNode , tail = null;

int data;

while(list1!=null && list2 != null) {

if(list1.num < list2.num) {

data = list1.num;

list1 = list1.link;

}

else {

data = list2.num;

list2 = list2.link;

}

newNode = new node(data);

if(tail == null) {

head[2] = newNode;

tail = newNode;

}

else {

tail.link = newNode;

tail = newNode;

}

}

while(list1!=null) {

newNode = new node(list1.num);

list1 = list1.link;

if(tail == null) {

head[2] = newNode;

tail = newNode;

}

else {

tail.link = newNode;

tail = newNode;

}

}

while(list2!=null) {

newNode = new node(list2.num);

list2 = list2.link;

if(tail == null) {

head[2] = newNode;

tail = newNode;

}

else {

tail.link = newNode;

tail = newNode;

}

}

return head[2];

}

public void delete(int x , int k) {

node current = head[k],prev = head[k];

if(current==null)return;

if(current.num == x) {

head[k] = current.link;

return;

}

while(current!=null && current.num!=x) {

prev = current;

current = current.link;

}

if(current==null)return;

prev.link = current.link;

}

public int search(int x , int k) {

node current = head[k];

if(current == null)return -1;

int count = 0;

while(current!=null) {

if(current.num == x)return count;

current = current.link;

count++;

}

return -1;

}

public static void main(String args[])throws Exception{

int k ,j,n,x;

try{

Scanner inf = new Scanner(System.in);

xxxxxp3 lst = new xxxxxp3();

for(k=0;k<=1;k++){

System.out.println("\nEnter count of elements you want to enter in list:");

n = inf.nextInt();

System.out.printf("\nEnter Contents of List-%d: \n",k);

for(j=1;j<=n;j++){

x = inf.nextInt();

lst.insertsorted(x,k);

}

lst.prtlst(k);

}

inf.close();

lst.merge();

for(k=0;k<=2;k++)lst.prtlst(k);

}catch(Exception e){System.out.print("\nException "+e+"\n");}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// if you have any doubt let me know ,i will help you.

import java.util.Scanner;

class xxxxxp3{

private node[] head = new node[3];

private class node{

int num;

node link;

node(int x){

num=x;

link = null;

}

}

public void prtlst(int k){

System.out.printf("\nContents of List-%d:",k);

for(node cur = head[k];cur!=null;cur = cur.link)

System.out.printf("%4d ",cur.num);

}

public void insertsorted(int x , int k){

node current = head[k];

node newNode = (new node(x));

if(current==null ||current.num > x){

newNode.link = current;

head[k]=newNode;

return;

}

node previous = current;

while (current != null && current.num < x)

{

previous = current;

current = current.link;

}

previous.link = newNode;

newNode.link = current;

}

public node merge(){

node list1 = head[0];

node list2 = head[1];

node newNode , tail = null;

int data;

while(list1!=null && list2 != null) {

if(list1.num < list2.num) {

data = list1.num;

list1 = list1.link;

}

else {

data = list2.num;

list2 = list2.link;

}

newNode = new node(data);

if(tail == null) {

head[2] = newNode;

tail = newNode;

}

else {

tail.link = newNode;

tail = newNode;

}

}

while(list1!=null) {

newNode = new node(list1.num);

list1 = list1.link;

if(tail == null) {

head[2] = newNode;

tail = newNode;

}

else {

tail.link = newNode;

tail = newNode;

}

}

while(list2!=null) {

newNode = new node(list2.num);

list2 = list2.link;

if(tail == null) {

head[2] = newNode;

tail = newNode;

}

else {

tail.link = newNode;

tail = newNode;

}

}

return head[2];

}

public void delete(int x , int k) {

       node current = head[k],prev = head[k];
      
       if(current==null)return;
      
       if(current.num == x) {
      
       head[k] = current.link;
      
       return;
      
       }
      
       while(current!=null && current.num!=x) {
      
       prev = current;
      
       current = current.link;
       }
       if(current==null)return;
       prev.link = current.link;
}

public int search(int x , int k) {
       node current = head[k];
       if(current == null)
           return -1;
       int count = 0;
       while(current!=null) {
           if(current.num == x)return count;
           current = current.link;
           count++;
       }
return -1;
}
public static void main(String args[])throws Exception{
int k ,j,n,x;
try{

Scanner inf = new Scanner(System.in);

xxxxxp3 lst = new xxxxxp3();

for(k=0;k<=1;k++){

System.out.println("\nEnter count of elements you want to enter in list:");

n = inf.nextInt();

System.out.printf("\nEnter Contents of List-%d: \n",k);

for(j=1;j<=n;j++){

x = inf.nextInt();

lst.insertsorted(x,k);

}

lst.prtlst(k);

}

inf.close();

lst.merge();

for(k=0;k<=2;k++)
   lst.prtlst(k);
System.out.println();
for(k=0;k<=2;k++)
System.out.println("The position of 4 in list "+k+" is "+lst.search(4, k));
System.out.println("\ncontents of list after deleting the elements in every list");
for(k=0;k<=2;k++)
{
  
   lst.delete(4, k);
   lst.prtlst(k);
  
}
}catch(Exception e){System.out.print("\nException "+e+"\n");}

}

}

Screenshots of sample input and output :

relield Adnart sc/estjava Eclipse Rite Edit Source Refactor Navigate Search Project Run Window Help Console terminated TestAl

Add a comment
Know the answer?
Add Answer to:
Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

  • Improve the speed of public T get(int i) by having it work backward from the end...

    Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> {    private static class Node<T> {        public Node<T> prev, next;...

  • I have a problem with merging the two linked lists together. In the function AscendMerge, I...

    I have a problem with merging the two linked lists together. In the function AscendMerge, I am trying to compare the values of each node in the two lists and output them into one list in ascended order. Everything works except for the one function. Can someone tell me what im doing wrong, when I run it it skips over values. #include <iostream> #include <cassert> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *current;...

  • Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the...

    Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • **HELP** Write a function that takes a linked list of items and deletes all repetitions from the ...

    **HELP** Write a function that takes a linked list of items and deletes all repetitions from the list. In your implementation, assume that items can be compared for equality using ==, that you used in the lab. The prototype may look like: void delete_repetitions(LinkedList& list); ** Node.h ** #ifndef Node_h #define Node_h class Node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR Node(const value_type& init_data = value_type( ), Node* init_link = NULL) { data_field = init_data; link_field = init_link;...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a...

    Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a Circular Doubly Linked List and am having serious trouble creating a method retainAll. Here's the code, and my attempt. Initialization: public class CDoublyLinkedList {    private class Node {        private Object data; //Assume data implemented Comparable        private Node next, prev;        private Node(Object data, Node pref, Node next)        {            this.data = data;       ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT