Question

In: Computer Science

(Java) Building a Doubly Linked List off of an interface I am having trouble with these...

(Java) Building a Doubly Linked List off of an interface I am having trouble with these two methods

    @Override
    public void add(T newEntry) {
        DoubleLinkedNode newNode = new DoubleLinkedNode(newEntry);

        if (!isEmpty()) {

        }
        if (isEmpty()) {
            first = newNode;
            last = newNode;
        } else {
            last.setNextNode(newNode);
            newNode.setPreviousNode(last);
            last = newNode;
        }
//        length++;
        numElements++;

    }

    @Override
    public void add(int newPosition, T newEntry) {
        DoubleLinkedNode newAdder = new DoubleLinkedNode(newEntry);

        if ((newPosition >= 0) && (newPosition <= numElements)) {
            numElements++;
            if (newPosition == 0) {
                add(newEntry);
            } else if (newPosition >= getLength() + 1) {
                DoubleLinkedNode previousNode = getNodeAt(newPosition - 1);
                DoubleLinkedNode NextNode = previousNode.getNextNode();

                newAdder.setNextNode(NextNode);
                newAdder.setPreviousNode(previousNode);
                previousNode.setNextNode(newAdder);
                NextNode.setPreviousNode(newAdder);
            } else if (newPosition == getLength()) {

                DoubleLinkedNode previousNode = getNodeAt(newPosition - 1);

                previousNode.setNextNode(newAdder);
                newAdder.setPreviousNode(previousNode);

                last = newAdder;
            }
        } else {
            throw new IndexOutOfBoundsException("");
        }
    }
I cannot figure out how to get the add newEntry and other add method to work All the Method calls do what you'd think they'd do

Solutions

Expert Solution

The code snippet shown here represents function overloading rather than function overriding.

Function overriding requires both functions to have exactly the same function prototype (access specifier, return type, name and parameter list). And the functions must be present in two different classes, one of whom inherits the other.

Function overloading, on the other hand, is two or more functions having the same name but different parameter lists.

Which of the overloaded functions is to work for a given function call is determined by the compiler at compile time. For example, if there is a call,

add(entry); //entry is of type T

the function public void add (T newEntry) is invoked. Whereas for a function call like

add(pos, entry); //pos is of type integer and entry is of type T

the function public void add (int newPosition, T newEntry) is invoked.

So, to make the functions work as desired, they have to be invoked with the correct required parameters. The main() function, for example, may be written as follows:

public static void main(String args[])

{

   T entry;

   int pos=5;

   add(pos, entry);

}

This will add a node at position 5 of the doubly linked list with the specified contents.


Related Solutions

******IN JAVA******** I need the following interface implemented accordingly. It is a linked list. The interface...
******IN JAVA******** I need the following interface implemented accordingly. It is a linked list. The interface can be found below: List.java public interface List<T> extends Iterable<T> { /** * Insert an element at a specified location. * @param index * @param obj * @throws IndexOutOfBoundsException */ public void add(int index, T obj); /** * Append an object to the end of the list. * @param obj */ public boolean add(T obj); public void clear(); public boolean contains(T obj); /** *...
I am having trouble with printing the linked list from user input. Can someone tell me...
I am having trouble with printing the linked list from user input. Can someone tell me what I am doing wrong. #include <iostream> using namespace std; struct node {    int info;    node*link;    node*current;    node*prev;    node * head;    node * last; } ; void Insert(node*&head,node*&last,int n); int SplitList(node*&head); void print(node*&head); int main() {    int num;    node*h;    node*l;    cout << "Enter numbers ending with -999" << endl;    cin >> num;   ...
Hello! I am having trouble starting this program in Java. the objective is as follows: "...
Hello! I am having trouble starting this program in Java. the objective is as follows: " I will include a text file with this assignment. It is a text version of this assignment. Write a program that will read the file line by line, and break each line into an array of words using the tokenize method in the String class. Count how many words are in the file and print out that number. " my question is, how do...
This is a Java program that I am having trouble making. 1- Search for the max...
This is a Java program that I am having trouble making. 1- Search for the max value in the linked list. 2- Search for the min value in the linked list. 3- Swap the node that has the min data value with the max one. (Note: Move the nodes to the new positions). 4- Add the min value and the max value and insert the new node with the calculated value before the last node. I already made a generic...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
I am having trouble figuring out how to To-Do list using java. Can anyone guide me...
I am having trouble figuring out how to To-Do list using java. Can anyone guide me through the steps for achieving this?
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
Hello, I am having trouble getting started on my project and building these functions. How do...
Hello, I am having trouble getting started on my project and building these functions. How do I build a function that continuously adds new "slices" to the list if they are below/above the size limit? I didn't copy the entire problem, but just for reference, when the code is run it will take user input for size limit (L), time cost for a random slice(R), and time cost for an accurate slice(A). Question: In real life, a steak is a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT