Question

2. (30 pts) Given the single-linked list below, assume that the variable tail references the last Node SingleLinkedList Node Node Node Node head next data = next data = next = data next null data = String Strin String String value Tom value Dick valueHarry value Sam Write the code to do each of the following operations. Assume that the result of each operation does not affect the other operations. In another word, assume that each operation always starts with the linked list in the figure above. Also assume that your code are inside a method in the class SingleLinkedList<E> and have direct access to data field head. You are allowed to call other methods in the class SingleLinked List<E> given in Lec#7 and Lec#8 a. Insert Bill before Tom b. Insert Sue before Sam c. Remove Tom d. Remove “Sam,

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

I have coded SingleLinkedList class. It has 2 methods which are useful to complete these 4 tasks.

methods:

insertBefore()

remove()

You can use insertBefore() to complete tasks a) & b)

You can use remove() to complete tasks c) & d)

SingleLinkedList.java

public class SingleLinkedList {
    private Node head;
    SingleLinkedList(Node head){
        this.head = head;
    }

    public void insertBefore(String whereTo, String whatTo){
        if(areBothDataEqual(whereTo, head)){
            head = new Node(head, whatTo);
            return;
        }
        Node tempPointer = head;
        tempPointer = tempPointer.next;
        Node beforeTemp = head;
        while (tempPointer != null){
            if (areBothDataEqual(whereTo, tempPointer)){
                beforeTemp.next = new Node(tempPointer, whatTo);
                return;
            }
            beforeTemp = tempPointer;
            tempPointer = tempPointer.next;
        }
    }

    public void remove(String whatTo){
        if(areBothDataEqual(whatTo, head)){
            head = head.next != null? head.next: null;
            return;
        }
        Node tempPointer = head;
        tempPointer = tempPointer.next;
        Node beforeTemp = head;
        while (tempPointer != null){
            if (areBothDataEqual(whatTo, tempPointer)){
                beforeTemp.next = tempPointer.next;
                return;
            }
            beforeTemp = tempPointer;
            tempPointer = tempPointer.next;
        }
    }

    private boolean areBothDataEqual(String whereTo, Node node) {
        return whereTo.equalsIgnoreCase(node.data);
    }
}

Node.java

public class Node {
    Node next;
    String data;
    public Node(Node next, String data){
        this.next = next;
        this.data = data;
    }
}
Add a comment
Know the answer?
Add Answer to:
2. (30 pts) Given the single-linked list below, assume that the variable tail references the last...
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
  • Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that...

    Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that stores integers. Implement a non-recursive function that takes a linked list, searches for an integer, and removes the node with the first occurrence of that integer and also removes the node directly after it regardless of value . This function will return to address of the resulting list. You ca n assume that there will be at least three nodes, and if there is...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not ma...

    Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-»2->3->1-2 Output: 1->2->3 public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3);...

  • Problem Statement This problem provides you with a linked list composed of node objects chained together...

    Problem Statement This problem provides you with a linked list composed of node objects chained together via node pointers. Each node has a next pointer which points to the next node in the chain. The last node is identified by having a NULL (or nullptr) next pointer. The linked lists for this problem store string data. Your task: identify the longest string stored in the linked list. If two strings are of the same length, return the string that occurred...

  • linked list operation /*************************************************************************************** This function creates a new node with the information give as a...

    linked list operation /*************************************************************************************** This function creates a new node with the information give as a parameter and looks for the right place to insert it in order to keep the list organized ****************************************************************************************/ void insertNode(string first_name, string last_name, string phoneNumber) { ContactNode *newNode; ContactNode *nodePtr; ContactNode *previousNode = nullptr; newNode = new ContactNode; /***** assign new contact info to the new node here *****/ if (!head) // head points to nullptr meaning list is empty { head = newNode;...

  • #1. Single linked list - find... Extra info/hint? It's free For this problem, I have a...

    #1. Single linked list - find... Extra info/hint? It's free For this problem, I have a complete linked list program which supports the following commands from the user: insert, display, delete, average, find, insertBefore, insertAfter. insert allows them to insert a number into the current list, display will display the numbers in the current list etc. The program is complete except that I have removed the body of the method for find. Given the following class for the nodes in...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

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