Question

Hi! Can someone can convert this java code to c++. ASAP thanks I will thumbs up Here's the code: ...

Hi! Can someone can convert this java code to c++. ASAP thanks I will thumbs up

Here's the code:

package lists;

import bookdata.*;

public class DoublyLinkedList {

int size; //Variable que define el tamano de la lista.

Node head, tail; //Nodos que definen el Head y Tail en la lista.

//Constructor

public DoublyLinkedList(){

this.head = null;

this.tail = null;

this.size = 0;

}

//Insert a new book in alphabetic order.

public void insert(Book nb){

//Creamos uno nuevo nodo con el libro que queremos ingresar.

Node newNode = new Node(nb);

//Insertar si la lista esta vacia.

if(size == 0){

head = newNode;

tail = head;

size++;

return;

}

//Revisar si el libro ya existe en la lista

else{

Node current = head;

for(int i = 0; i < size; i++){

if(nb.getTitle().compareTo(current.stdBook.getTitle()) == 0){

System.out.println("Already exist a book with that name.");

return;

}

current = current.next;

}

}

//Revisar si seria el nuevo head.

if (nb.getTitle().compareTo(head.stdBook.getTitle()) < 0){

newNode.next = head;

newNode.previous = tail;

head.previous = newNode;

tail.next = newNode;

head = newNode;

size++;

return;

}

//Revisar si puede ser el nuevo Tail.

else if (nb.getTitle().compareTo(tail.stdBook.getTitle()) > 0){

newNode.previous = tail;

newNode.next = head;

head.previous = newNode;

tail.next = newNode;

tail = newNode;

size++;

return;

}

//Buscar el lugar correcto para ingresar.

else{

Node current = head.next;

for(int i = 0; i < size; i++){

if (nb.getTitle().compareTo(current.stdBook.getTitle()) < 0){

newNode.previous = current.previous;

current.previous.next = newNode;

current.previous = newNode;

newNode.next = current;

size++;

return;

}

current = current.next;

}

}

}

//Delete a book from the list.

public void deleteBook(String bookTitle){

Node current = head;

//Revisar si la lista esta vacia primero.

if (head == null && tail == null){

System.out.println("There is no items in the list.");

return;

}

//Realizar la busqueda del string que se desea eliminar.

for(int i = 0; i < size; i++){

if(current.stdBook.getTitle().compareTo(bookTitle) == 0){

if(size == 1){

                    head = null;

                    tail = null;

                }

                else if(current == head){

head.previous.next = current.next;

current.next.previous = head.previous;

head = head.next;

current.next = null;

                    current.previous = null;

                }

                else if(current == tail){

                    tail.previous.next = current.next;

current.next.previous = tail.previous;

tail = tail.previous;

current.next = null;

                    current.previous = null;

                }

                else{   

                    current.previous.next = current.next;

                    current.next.previous = current.previous;

                    current.previous = null;

                    current.next = null;

                }

size--;

return;

}

current = current.next;

}


}

//Return the size of the list.

    public int getSize() {

        return size;

    }

//Show the list of books.

public void printDList(){

Node current = head;

for(int i = 0; i < size; i++){

            System.out.println(current.stdBook.getTitle());

            current = current.next;

        }

}

//Find a book in the list by title.

public boolean findBook(String bookTitle){

Node current = head;

if(current == null){

return false;

}

for(int i = 0; i < size; i++){

            if(current.stdBook.getTitle().compareTo(bookTitle) == 0){

                return true;

            }

            current = current.next;

        }

        return false;

}

//Return a book finding by index.

public Book selectBook(int index){

Node current = head;

for(int i = 0; i < index; i++){

current = current.next;

}

return current.stdBook;

}

//Get a book in the list by title.

public Book getBook(String bookTitle){

Node current = head;

for(int i = 0; i < size; i++){

            if(current.stdBook.getTitle().compareTo(bookTitle) == 0){

                return current.stdBook;

            }

            current = current.next;

        }

        return null;

}

}

class Node{

Book stdBook;

Node next;

Node previous;

//Constructor

public Node(){

stdBook = null;

next = null;

previous = null;

}

//Constructor con argumento

    public Node(Book bookR){

        this.stdBook = bookR;

next = null;

previous = null;

    }

}

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


// java classes are reference objects by default
//just replacing them by pointers will work.
// replace . with ->
//cout instead print
//direct comparison of strings done


class Node
{

    private:

        Book* stdBook;
        Node* next;
        Node* previous;

      

    public:

        //Constructor
        Node()
        {

            stdBook = NULL;
            next = NULL;
            previous = NULL;

        }

        //Constructor con argumento
        Node(Book* bookR)
        {
            this -> stdBook = bookR;
            next = NULL;
            previous = NULL;
        }

};


class DoublyLinkedList
{
  
    private:

    int size; //Variable que define el tamano de la lista.
    Node* head;
    Node* tail; //Nodos que definen el Head y Tail en la lista.


    public:

    //Constructor

    DoublyLinkedList()
    {

        this -> head = NULL;
        this -> tail = NULL;
        this -> size = 0;

    }

    //Insert a new book in alphabetic order.
    void insert(Book* nb)
    {

        //Creamos uno nuevo nodo con el libro que queremos ingresar.
        Node* newNode = new Node(nb);

        //Insertar si la lista esta vacia.
        if(size == 0)
        {
            head = newNode;
            tail = head;
            size++;
            return;
        }

        //Revisar si el libro ya existe en la lista
        else
        {

            Node* current = head;

            for(int i = 0; i < size; i++)
            {

                if(nb -> getTitle() == current -> stdBook -> getTitle())
                {
                    cout << "Already exist a book with that name.";
                    return;
                }

                current = current -> next;
            }

        }

        //Revisar si seria el nuevo head.

        if (nb -> getTitle() < head->stdBook->getTitle())
        {
            newNode -> next = head;
            newNode -> previous = tail;
            head -> previous = newNode;
            tail -> next = newNode;
            head = newNode;
            size++;
            return;
        }

        //Revisar si puede ser el nuevo Tail.

        else if (nb -> getTitle() > head->stdBook->getTitle())
            {
                newNode -> previous = tail;
                newNode -> next = head;
                head -> previous = newNode;
                tail -> next = newNode;
                tail = newNode;
                size++;
                return;
            }


        //Buscar el lugar correcto para ingresar.
            else
            {

                 Node* current = head->next;
                 for(int i = 0; i < size; i++)
                 {
                    if (nb->getTitle() < current->stdBook->getTitle())
                    {
                        newNode->previous = current->previous;
                        current->previous->next = newNode;
                        current->previous = newNode;
                        newNode->next = current;
                        size++;
                        return;
                    }

                    current = current->next;
            }

        }

    }


//Delete a book from the list.

void deleteBook(string bookTitle)
{

    Node* current = head;
    //Revisar si la lista esta vacia primero.

    if (head == NULL && tail == NULL)
    {
        cout<<"There is no items in the list.";
        return;
    }

//Realizar la busqueda del string que se desea eliminar.

    for(int i = 0; i < size; i++)
    {

        if(current->stdBook->getTitle() == bookTitle)
        {

            if(size == 1)
            {

                head = null;
                tail = null;

            }

                  
            else if(current == head)
                {

                head->previous->next = current->next;
                current->next->previous = head->previous;
                head = head->next;
                current->next = null;
                current->previous = null;

                }

                else if(current == tail)
                    {

                       tail->previous->next = current->next;
                       current->next->previous = tail->previous;
                       tail = tail->previous;
                       current->next = null;
                       current->previous = null;

                    }

                    else
                    {

                        current->previous->next = current->next;
                        current->next->previous = current->previous;
                        current->previous = null;
                        current->next = null;

                    }

            size--;
            return;
        }

        current = current.next;

    }


}

//Return the size of the list.
int getSize()
{

    return size;

}

//Show the list of books.

void printDList()
{
    Node* current = head;
        for(int i = 0; i < size; i++)
        {

            cout<< current->stdBook->getTitle());
            current = current.next;

        }
}

//Find a book in the list by title.

bool findBook(string bookTitle)
{

        Node current = head;

        if(current == NULL)
        {
            return 0;
        }

    for(int i = 0; i < size; i++)
    {

            if(current->stdBook->getTitle()==bookTitle)
            {
                return 1;
            }

            current = current->next;
        }
        return 0;
    }

//Return a book finding by index.
public Book selectBook(int index){

    Node* current = head;
    for(int i = 0; i < index; i++){
        current = current->next;
    }
    return current->stdBook;
}


//Get a book in the list by title.

public Book getBook(String bookTitle)
{

    Node* current = head;
    for(int i = 0; i < size; i++)
    {

        if(current->stdBook->getTitle() == (bookTitle))
        {
            return current->stdBook;
        }

        current = current->next;
    }

    return NULL;

}

};

COMMENT DOWN FOR ANY QUERIES AND,

LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.

Add a comment
Know the answer?
Add Answer to:
Hi! Can someone can convert this java code to c++. ASAP thanks I will thumbs up Here's the code: ...
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
  • 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;...

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

  • 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...

  • 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);...

  • In java Write a method public void printReverse() that prints the elements of a doubly linked...

    In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...

  • In java Write a method public void printReverse() that prints the elements of a doubly linked...

    In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...

  • Can Anyone help me to convert Below code to C++! Thanks For example, in C++, the...

    Can Anyone help me to convert Below code to C++! Thanks For example, in C++, the function headers would be the following: class MaxHeap { vector<int> data; public: MaxHeap() { // ... } int size() { // ... } int maxLookup() { // ... } void extractMax() { // ... } void insert(int data) { // ... } void remove(int index) { // ... } }; ======================== import java.util.Arrays; import java.util.Scanner; public class MaxHeap { Integer[] a; int size; //...

  • Convert into pseudo-code for below code =============================== class Main {    public static void main(String args[])...

    Convert into pseudo-code for below code =============================== class Main {    public static void main(String args[])    {        Scanner s=new Scanner(System.in);        ScoresCircularDoubleLL score=new ScoresCircularDoubleLL();        while(true)        {            System.out.println("1--->Enter a number\n-1--->exit");            System.out.print("Enter your choice:");            int choice=s.nextInt();            if(choice!=-1)            {                System.out.print("Enter the score:");                int number=s.nextInt();                GameEntry entry=new GameEntry(number);   ...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

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