Question

Java

Write the function void insertAtTail (int v). Don’t add any class variables to the List class.

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

I have added function definitions of Node class to understand the things easiy. So, the code that is asked in the question is:

Code:

class Node
{
private Node next;
  
private int key;
  
Node(Node nxt, int keyValue)
{
next = nxt;
key = keyValue;
}
  
Node getNext()
{
return next;
}
  
void putNext(Node nxt)
{
next = nxt;
}
  
int getKey()
{
return key;
}
}

public class List
{
private Node head;
  
void insertAtTail(int v)
{
if(head == null)
{
head = new Node(null, v);
}
  
else
{
Node n = new Node(null, v);

Node tmp = head;
  
while(tmp.getNext()!=null)
tmp = tmp.getNext();
  
tmp.putNext(n);
  
return;
}
}
}

Working:

First, it checks whether the list has a headd node i.e. the list is initialized or not. So, if the head is not initialized, it sets new node as head. So,, as there is only 1 node in the list, I set null to its next while writing the constructor and v is the value that will be set in the 'key' variable.

Now, in the else part. This means the list has at least 1 node. So, the new node will not be the head node but it will be inserted at last. From its constructor it is clear that the new node is supposed to be inserted at last, so its next will contain .... null and the value v is set as key.

Now, I created a node tmp which points to head initially. The purpose of this is to traverse the list till I get last node. So, the loop goes on until it reaches to the last node which is having null as its next. And for that, the newly created node is inserted in its next. So, the newly created node is added at last position.

If there is any confusion regarding this, please comment. I'll clear it for sure. Thank you. :)

Add a comment
Know the answer?
Add Answer to:
Java Write the function void insertAtTail (int v). Don’t add any class variables to the List...
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
  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • Class SortedList { Public: SortedType(); int getLength(); //returns size of the list void putItem(Itemtype item); //inserts...

    Class SortedList { Public: SortedType(); int getLength(); //returns size of the list void putItem(Itemtype item); //inserts an item Itemtype getNextItem(); //returns the next item pointed by the index void deleteItem(Itemtype item) //deletes a specified item Private: int length; //length of the list Nodetype* listData //head of the list Nodetype* currentPos; //current pointer to the list } Class Itemtype { Public: Itemtype(); int getValue();// returns the value Private: int value; } struct Nodetype { Itemtype info; Nodetype* next; } Add a...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

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

  • PROGRAMMING 1. The LinkList class that we discussed in class is implemented using pointer t constructed...

    PROGRAMMING 1. The LinkList class that we discussed in class is implemented using pointer t constructed with a list of nodes, and the first node pointer points to the front I a) (10 pts) Complete the following class declaration which is similar to Linklist class ecter class (Write only the prototypes and the private data field; definitions will follow the class declaration) template stypenane T> olass Linkedveoter t private olass Node publie T info Node "next typedet Node *nodePtr publie...

  • Q1: You can find a file that defines the CircularlyLinked List class similar to what we...

    Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the file and work on it. Your task is to: 1. Complete the missing methods in the file as discussed in the class. Search for the comment/" MISSING / in the file to see the methods that need to be completed. 2. Add the following methods to the class a. public Node getMin 1. Task: find the node with...

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

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype...

    Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node    //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...

  • You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided)....

    You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance from...

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