Question

Please help me on all the questions !!!!!!!! Really need help! Will give a thumb up...

Please help me on all the questions !!!!!!!! Really need help! Will give a thumb up for helping.

Short Answer (6)

  1. Explain why a public method should be declared to be final if it is called by a constructor.

  2. In the linked implementation of a list, why shouldn’t the constructor call the method clear even

    though they do the same exact assignment statements?

  3. Outline the basic steps to add a node to the end of a linked implementation of a list.

  4. Outline the basic steps to add a node to the beginning of an empty linked implementation of a

    list.

  5. Outline the basic steps to add a node to the beginning of a non-empty linked implementation of

    a list.

  6. Outline the basic steps to remove a node from the beginning of a list.

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

Ans 1.  

When a method is declared with finalkeyword, it is called a final method. A final method cannot be overridden. The Objectclass does this—a number of its methods are final.We must declare methods with final keyword for which we required to follow the same implementation throughout all the derived classes. The following fragment illustrates final keyword with a method:

class A 
{
    final void m1() 
    {
        System.out.println("This is a final method.");
    }
}

class B extends A 
{
    void m1()
    { 
        // COMPILE-ERROR! Can't override.
        System.out.println("Illegal!");
    }
}

Ans 2. Because constructor is executed only during initalisation of object.

Ans 3.  Add a node at the end: (6 steps process)
The new node is always added after the last node of the given Linked List. For example if the given Linked List is 5->10->15->20->25 and we add an item 30 at the end, then the Linked List becomes 5->10->15->20->25->30.
Since a Linked List is typically represented by the head of it, we have to traverse the list till end and then change the next of last node to new node.

Following are the 6 steps to add node at the end.It is in the form of C code.

/* Given a reference (pointer to pointer) to the head

   of a list and an int, appends a new node at the end */

void append(struct Node** head_ref, int new_data)

{

    /* 1. allocate node */

    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

  

    struct Node *last = *head_ref; /* used in step 5*/

   

    /* 2. put in the data */

    new_node->data = new_data;

  

    /* 3. This new node is going to be the last node, so make next

          of it as NULL*/

    new_node->next = NULL;

  

    /* 4. If the Linked List is empty, then make the new node as head */

    if (*head_ref == NULL)

    {

       *head_ref = new_node;

       return;

    }  

  /* 5. Else traverse till the last node */

    while (last->next != NULL)

        last = last->next;

   

    /* 6. Change the next of last node */

    last->next = new_node;

    return;    

}

Time complexity of append is O(n) where n is the number of nodes in linked list. Since there is a loop from head to end, the function does O(n) work.
This method can also be optimized to work in O(1) by keeping an extra pointer to tail of linked list/

Ans 4. Add node at front of empty link list .

Simply initalise head as the value to be added in link list .

Ans 5.

Add a node at the front: (A 4 steps process)
The new node is always added before the head of the given Linked List. And newly added node becomes the new head of the Linked List. For example if the given Linked List is 10->15->20->25 and we add an item 5 at the front, then the Linked List becomes 5->10->15->20->25. Let us call the function that adds at the front of the list is push(). The push() must receive a pointer to the head pointer, because push must change the head pointer to point to the new node (See this)

Following are the 4 steps to add node at the front in form of C code :

C

/* Given a reference (pointer to pointer) to the head of a list

   and an int, inserts a new node on the front of the list. */

void push(struct Node** head_ref, int new_data)

{

    /* 1. allocate node */

    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

   

    /* 2. put in the data */

    new_node->data = new_data;

   

    /* 3. Make next of new node as head */

    new_node->next = (*head_ref);

   

    /* 4. move the head to point to the new node */

    (*head_ref)    = new_node;

}

Time complexity of push() is O(1) as it does constant amount of work.

Ans 6.

To remove first node, we need to make second node as head and delete memory allocated for first node.

// CPP program to remove first node of

// linked list.

#include <iostream>

using namespace std;

  

/* Link list node */

struct Node {

    int data;

    struct Node* next;

};

  

/* Function to remove the first node

   of the linked list */

Node *removeFirstNode(struct Node* head)

{

    if (head == NULL)

       return NULL;

  

    // Move the head pointer to the next node

    Node *temp = head;

    head = head->next;

    

    delete temp;

  

    return head;

}

  

// Function to push node at head

void push(struct Node** head_ref, int new_data)

{

    struct Node* new_node = new Node;

    new_node->data = new_data;

    new_node->next = (*head_ref);

    (*head_ref) = new_node;

}

  

// Driver code

int main()

{

    /* Start with the empty list */

    Node* head = NULL;

  

    /* Use push() function to construct  

       the below list 8 -> 23 -> 11 -> 29 -> 12 */

    push(&head, 12);

    push(&head, 29);

    push(&head, 11);

    push(&head, 23);

    push(&head, 8);

  

    head = removeFirstNode(head);

    for (Node *temp = head; temp != NULL; temp = temp->next)

       cout << temp->data << " ";

  

    return 0;

}

Kindly please do upvote. We are working very hard to provide you with best content. Upvoting is very essential for sustaining us. Kindly upvote please .

Add a comment
Know the answer?
Add Answer to:
Please help me on all the questions !!!!!!!! Really need help! Will give a thumb up...
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
  • Please help me on all the questions !!!!!!!! Really need help! Will give a thumb up...

    Please help me on all the questions !!!!!!!! Really need help! Will give a thumb up for helping. True/False (13) Chapter 14 - A List Implementation that Links Data Adding a node to an empty chain is the same as adding a node to the beginning of a chain. Adding a node at the end of a chain of n nodes is the same as adding a node at position n. You need a temporary variable to reference nodes as...

  • Please help me on all the questions !!!!!!!! Really need help! Will give a thumb up...

    Please help me on all the questions !!!!!!!! Really need help! Will give a thumb up for helping. True/False (11) Chapter 12 - Lists The ADT list only works for entries that are strings. The ADT list is more general than common lists and has entries that are objects of the same type. Adding entries to the end of a list does not change the positions of entries already in the list. The first entry is a list is at...

  • I need help with the Implementation of an Ordered List (Template Files) public interface Ordered...

    I need help with the Implementation of an Ordered List (Template Files) public interface OrderedStructure { public abstract int size(); public abstract boolean add( Comparable obj ) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList other ); } import java.util.NoSuchElementException; public class OrderedList implements OrderedStructure { // Implementation of the doubly linked nodes (nested-class) private static class Node { private Comparable value; private...

  • I need help with a c++ code, i am new learner on stack, please help me...

    I need help with a c++ code, i am new learner on stack, please help me write Stack.cpp according to Stack.h provided, thanks(Notes: That data structure is a singly linked list in which pushed items are placed at the tail of the linked list. Similarly, popped items will be removed from the tail of the list.) Actually,I can write a regular one, but i'm not sure how to use the tail in the question. class Stack { private: // Desc:...

  • I need a python 3 help. Please help me with this question Part 2. Linked Lists...

    I need a python 3 help. Please help me with this question Part 2. Linked Lists You start working with the class LinkNode that represents a single node of a linked list. It has two instance attributes: value and next. class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Before you start with the coding questions, answer the following questions about the constructor Valid Constructor or Not? LinkNode(1, 3) LinkNode(1, None) LinkNode(1,...

  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

  • plzzz help me. in java it should all be in one program. plz help 1.3.19 Give...

    plzzz help me. in java it should all be in one program. plz help 1.3.19 Give a code fragment that removes the last node in a linked list whose first node is first. 1.3.21 Write a method find() that takes a linked list and a string key as arguments and returns true if some node in the list has key as its item field, false otherwise. 1.3.26 Write a method remove() that takes a linked list and a string key...

  • Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test...

    Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test class: Remember your header with name, date, and assignment. Also include class names that will be tested. Psuedocode (level 0 or mixture of level 0 and algorithm [do not number steps]) is required if main() contains more than simple statements (for example, your program includes constructs for decisions (if/else), loops, and methods. For Secondary class(es): Include a JavaDoc comment that describes the purpose of...

  • Please answer the following Questions with A,B,C... and I will give a thumbs up If you...

    Please answer the following Questions with A,B,C... and I will give a thumbs up If you can give a little context to why you chose that answer that'd be great too. Which of the following are the right steps to remove and return the linear node pointed to by current if it is not the first node in the list and previous points to the node before current. Question 1 previous.setNext(current.getNext()); return current; current.setNext(previous.getNext()); return previous; current.setPrevious(current.getNext()); return previous; previous.setNext(current.getPrevious());...

  • Modify Dlinkedlist.java so it includes a new method named getPriorNode that accepts the String value of...

    Modify Dlinkedlist.java so it includes a new method named getPriorNode that accepts the String value of a potential element in the doubly linked list and returns the string value of the predecessor node (or an appriopriate message String if there is no such node or predecessor. class DLinkedList { /** The Node class stores a list element and a reference to the next node. */ private class Node { String value; // Value of a list element Node next; //...

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