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;
}
}
2. (30 pts) Given the single-linked list below, assume that the variable tail references the last...
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 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, 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...
In C++, Modify the following code of a single linked list, so insted of doing sort insert in descending order, to do it in ascending order: template <class T> void IntSLList<T>::sortInsert(T val) { //if empty the list if (head == 0) { //add as node to head with data as val addToHead(val); } //if not empty else { //create to pointers to the list IntSLLNode<T>...
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 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);...
In C++, change this code to a circular linked list #include <iostream> using namespace std; struct node { string data; node *next; }; class linked_list { private: node *head,*tail; public: linked_list() { head = NULL; tail = NULL; } void add_node(string n) { node *tmp = new node; tmp->data = n; tmp->next = NULL; if(head == NULL) { head = tmp; tail = tmp; } else { tail->next = tmp; tail = tail->next; } } void display() { while (tmp...
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 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;...
c++ only 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 Node { int number; Node * nextNode;...