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");}
}
}
// 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 :

Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k...
Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 { private Node head, tail; private int size; public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...
CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node { char c; Node *next; }; class LinkedString { Node *head; public: LinkedString(); LinkedString(char[]); LinkedString(string); char charAt(int) const; string concat(const LinkedString &obj) const; bool isEmpty() const; int length() const; LinkedString substring(int, int) const; //added helper function to add to linekd list private: void add(char c); };...
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...
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 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;...
Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). /* definition of the list node class */ class Node { friend class LinkedList; private: int value; Node *pNext; public: /* Constructors with No Arguments...
Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...
Write a method with signature "concatenate(LinkedQueue<E> Q2)" for the LinkedQueue<E> class that takes all elements of Q2 and appends them to the end of the original queue. The operation should run in O(1) time and should result in Q2 being an empty queue. Write the necessary code to test the method.You may just modify the SinglyLinkedList class to add necessary support. LinkedQueue Class public class LinkedQueue<E> implements Queue<E> { /** The primary storage for elements of the queue */ private...
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;...
(1) Implement the countKey(T element) method, which should return a count of the number of times that the given key (the element) is found in the list. (2) Implement the indexOf(T element) method, which is similar as the indexOf method in the String class. It returns the index (the position starting from the head node) of the first occurrence of the given element, or -1, if the element does not occur in the list. You will need to track the...