Question

In: Computer Science

Given lists L1, L2 and L3, delete all the nodes having even number in info part...

Given lists L1, L2 and L3, delete all the nodes having even number in info part from the list L1 and insert into list L2 and all the nodes having odd numbers into list L3.

Code needed in java.

Solutions

Expert Solution

* Please note that I have created a program to create the list and a function to perform the above operation.
* As odd and even both values have to be deleted, list L1 will be empty after the operation and l2 and l3 will have data of l1 as per the condition.

CODE:

Kindly upvote if this helped. Comment for more info/help.

public class StackList {    
    class Node{    
        int data;    
        Node next;    
            
        public Node(int data) {    
            this.data = data;    
            this.next = null;    
        }    
    }    
     
    public Node h = null;    
    public Node t = null;    
        
    public void push(int data) {    
        Node newNode = new Node(data);    
            
        if(h == null) {    
            h = newNode;    
            t = newNode;    
        }    
        else {    
            t.next = newNode;    
            t = newNode;    
        }    
    }    
    
   
    
   
  
    public void display() {    
        Node current = h;    
        if(h == null) {    
            System.out.println("No data in list");    
            return;    
        }    
        System.out.println("Values in list are ");    
        while(current != null) {    
            System.out.print(current.data + " ");    
            current = current.next;    
        }  
        System.out.println();
    }    
    
    
    public static void popEvenFromL1andPushInL2(StackList l1 , StackList l2 , StackList l3) {
         Node current = l1.h;    
        
        if(l1.h == null) {    
            System.out.println("No data in list");    
            return;    
        }    
        while(current != null) {
                if(current.data % 2 == 0) {
                        l2.push(current.data);
                }else {
                   l3.push(current.data);       
                }
            current = current.next;    
        }    
        l1.h = null;
   }
    
  
    public static void main(String[] args) {    
        StackList sL = new StackList();    
        sL.push(4);    
        sL.push(2);    
        sL.push(5);    
        sL.push(8);
        StackList sL1 = new StackList();    
        sL1.push(1);    
        sL1.push(2);    
        sL1.push(2);    
        sL1.push(5);
        StackList sL2 = new StackList();    
        sL2.push(7);    
        sL2.push(12);    
        sL2.push(90);    
        sL2.push(81);
        popEvenFromL1andPushInL2(sL, sL1, sL2);
        sL.display(); // L1 list after operation
        sL1.display();// L2 after operation
        sL2.display(); // L3 after operation
        
    }    
    
}    


OUTPUT


Explanation :


Related Solutions

Given two sorted lists L1 and L2, write a procedure to compute L1∪L2 using only the...
Given two sorted lists L1 and L2, write a procedure to compute L1∪L2 using only the basic list operations. Pseudo-code is acceptable.
The intersection of two lists L1 and L2, L1 ∩ L2, is defined as the list...
The intersection of two lists L1 and L2, L1 ∩ L2, is defined as the list containing elements in both L1 and L2 only. Given two sorted lists L1 and L2, write a function, called intersection, to compute L1 ∩ L2 using only the basic list operations. The intersection function is defined as follows template list intersection( const list & L1, const list & L2); C++
Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩...
Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩ L2 using only the basic STL list operations. What is the running time of your algorithm?
. Observation data for the three L1, L2 and L3 locations are presented in the table...
. Observation data for the three L1, L2 and L3 locations are presented in the table as below L1 L2 L3 5 3 6 7 2 8 7 5 6 13 11 9 10 using α = 5%, test whether the conditions at the three locations are the same?
1)Given a list L1, create a list L2 that contains all but the last element of...
1)Given a list L1, create a list L2 that contains all but the last element of L1 (e.g. if L1 is ["a","z",True], L2 should be equal to ["a","z"] 2)Given a string s, assign a variable has_z that is True if s contains the letter "z" anywhere within it. 3)Write Python code that calculates the mean of a given list of numbers x and saves the result in a variable m. 4)Given two numeric variables, x and y, assign the maximum...
1) Equations for two lines L1 and L2 are given. Find the angle between L1 and...
1) Equations for two lines L1 and L2 are given. Find the angle between L1 and L2. L1: ? = 7 + 2?, ? = 8 − 4?, ? = −9 + ? L2: ? = −8 − ?, ? = 3 − 3?, ? = 4 + 3? 2) Find polar form of complex number z : ?) ? = 4√3 − 4? ?) ? = 2√3 − 2i
First Last Q0 Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 L0 L1 L2 L3...
First Last Q0 Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 L0 L1 L2 L3 L4 L5 L6 L7 L8 L9 P0 P1 P2 E0 E1 E2 FI ATT ------------------------------------------------------------------------------------------------------------------------------------------ Kevin Smith 90 100 100 100 98 97 87 100 85 87 89 100 100 100 100 90 100 98 90 100 98 98 98 90 90 98 88 0.00 Morgan Kelly 80 100 65 67 69 71 100 100 100 67 95 85 87 89 100 65 67...
First Last Q0 Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 L0 L1 L2 L3...
First Last Q0 Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 L0 L1 L2 L3 L4 L5 L6 L7 L8 L9 P0 P1 P2 E0 E1 E2 FI ATT ------------------------------------------------------------------------------------------------------------------------------------------ Kevin Smith 90 100 100 100 98 97 87 100 85 87 89 100 100 100 100 90 100 98 90 100 98 98 98 90 90 98 88 0.00 Morgan Kelly 80 100 65 67 69 71 100 100 100 67 95 85 87 89 100 65 67...
how do i construct a scatterplot if L1 lists negative values and L2 has values such...
how do i construct a scatterplot if L1 lists negative values and L2 has values such as 0.00339156, 0.00326318, 0.00313725 ? Do i enter the L2 values as exponents??
Problem 1: You have two sorted lists of integers, L1 and L2. You know the lengths...
Problem 1: You have two sorted lists of integers, L1 and L2. You know the lengths of each list, L1 has length N1 and L2 has length N2. Design a linear running time complexity algorithm (ONLY PSEUDOCODE) to output a sorted list L1 ∧ L2 (the intersection of L1 and L2). Important Notes: For this problem, you don’t need to submit any implementation in Java. Only the pseudocode of your algorithm is required. Pseudocode is a simple way of writing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT