In this activity, you will work with a sorted linked list of integers, performing the following activity: Code the insert and delete methods to insert and delete nodes in a sorted linked list of ints. The framework will animate your code to give you feedback on the correctness of your code. It will display the state of the sorted linked list at all times.
In this task, you will fill in the code inside the insert and delete methods for a sorted linked list of integers. Example 14.17 shows the section of the LinkedList source code where you will add your code. This example is different from the one presented earlier in the chapter. The nodes of the linked list contain ints, not Players. The delete method returns a boolean value to indicate whether the deletion was successful. Because the client has already provided the int value to delete, there is no reason to return the value to the client.
You can first code the insert method and run the application. Once the insert method works properly, you can code the delete method and run the application again. We have provided a dummy returnstatement in the delete method so that the LinkedList.java file will compile if only the insert method is coded. When you write the delete method, modify the dummy return statement to return the appropriate value.
public void insert( int value )
{
// ***** Student writes the body of this method *****
// code the insert method of a linked list of ints
// the int to insert in the linked list is value
//
// Student code starts here
//
//
// End of student code, part 1
//
}
public boolean delete( int value )
{
// ***** Student writes the body of this method *****
// code the delete method of a linked list of ints
// the int to delete in the linked list is value
// if deletion is successful, return true
// otherwise, return false
//
// Student code starts here
//
return true; // replace this return statement
//
// End of student code, part 2
//
}Please find the answer below.
I have created a Node structure as below:
class Node {
// instance data should be private...
public int data;
public Node next;
public Node(int i){
data = i;
next = null;
}
}
Based on that I have created below class with your required answer, please extract the code of your interest. I have created the main method for the testing purpose.
LinkedListOp.java
import java.util.LinkedList;
public class LinkedListOp {
Node head;
LinkedListOp(){
head = null;
}
public void insert( int value )
{
// ***** Student writes the body of this method *****
// code the insert method of a linked list of ints
// the int to insert in the linked list is value
//
// Student code starts here
//
//create a node to traverse the list
Node temp;
//new node to be inserted into the list
Node new_node = new Node(value);
//check if list is empty of node to be inserted is at the 1st place
if(head == null || head.data >= new_node.data){
new_node.next = null;
head = new_node;
}
//else traverse till the correct position
else{
temp = head;
while(temp.next!=null && temp.next.data < new_node.data){
temp = temp.next;
}
new_node.next = temp.next;
temp.next = new_node;
}
//
// End of student code, part 1
//
}
public boolean delete( int value )
{
// ***** Student writes the body of this method *****
// code the delete method of a linked list of ints
// the int to delete in the linked list is value
// if deletion is successful, return true
// otherwise, return false
//
// Student code starts here
//
//pointers to point to current and prev locations
Node temp = head;
Node prev = head;
//traverse till the desired value
while(temp!=null && temp.data!=value){
prev = temp;
temp = temp.next;
}
//if value is not found then return false
if(temp == null){
return false;
}
//if value is foudn then update the links to delete the node
else{
if(temp==head)
head = temp.next;
else{
prev.next = temp.next;
}
}
return true; // replace this return statement
//
// End of student code, part 2
//
}
void display()
{
Node temp = head;
while (temp != null){
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}
public static void main(String args[]){
LinkedListOp obj = new LinkedListOp();
obj.insert(2);
obj.insert(5);
obj.insert(7);
obj.insert(10);
obj.insert(15);
obj.display();
obj.insert(9);
obj.insert(16);
obj.display();
System.out.println(obj.delete(2));
obj.display();
System.out.println(obj.delete(16));
obj.display();
}
}
Output:

In this activity, you will work with a sorted linked list of integers, performing the following...
Implement a simple Doubly Linked List using nodes. Functions to include: an insert in a sorted list, and a delete function, including constructor. Please include comments to make the code traceable and understandable
Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...
Please use C++
CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student records that should not be sorted Create a liked list of 15 student record nodes. Each node is a node of one student record from the above unsorted array. The list of student records should be sorted by student ID. (Insert function without sort function to create a linked list.) (If you insert correctly, the...
#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...
Using Python.
3) Create a single-linked list (StudentList) where the data in the link is an object of type pointer to Student as defined below. Note that you need to store and pass a pointer of type Student so that you do not create duplicate objects. Test your list with the provided program. You will find example code for a single-linked list of integers in Moodle that you can base your solution on. For your find methods, you can either...
Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...
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...
Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...
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...
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...