Add a new L element to the singly linked list after each occurrence of E.
For example, let’s say we have a list of items 2, 3, 4, 7, 23, 2, 4. From the console, we enter two more items. For example L = 55 and E = 4. After that the program should return a new list 2,3,4,55,7,23,2,4,55.
I was only able to implement a list. I tried to create in the insert method and in a separate method, but I just can’t think of it. How do I implement it?
import java.io.*;
import java.util.Scanner;
import java.util.NoSuchElementException;
public class LinkedList {
Node head; // head of list
static class Node {
int data;
Node next;
// Constructor
Node(int d) {
data = d;
next = null;
}
// Method to insert a new node
public static LinkedList insert(LinkedList list, int data) {
// Create a new node with given data
Node new_node = new Node(data);
new_node.next = null;
// If the Linked List is empty,
// then make the new node as head
if (list.head == null) {
list.head = new_node;
} else {
// Else traverse till the last node
// and insert the new_node there
Node last = list.head;
while (last.next != null) {
last = last.next;
}
// Insert the new_node at last node
last.next = new_node;
}
// Return the list by head
return list;
}
// Method to print the LinkedList.
public static void printList(LinkedList list) {
Node currNode = list.head;
System.out.print("LinkedList: ");
// Traverse through the LinkedList
while (currNode != null) {
// Print the data at current node
System.out.print(currNode.data + " ");
// Go to next node
currNode = currNode.next;
}
System.out.println();
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
//
// ******INSERTION******
//
// Insert the values
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);
list = insert(list, 6);
list = insert(list, 7);
list = insert(list, 8);
// Print the LinkedList
printList(list);
}
}
}
Answer 1, authority 100%
Your code, as I have already written, contains weaknesses. The Node
nodes should be aggregated and implemented in a separate class (not internal). Let’s agree, before you sell the bike, first google how it is spelled. On the Internet, men have already implemented much better than you and even me. For the source I took this code and I liked it.
class ListElement {
ListElement next; // pointer to next element
int data; // data
}
class List {
private ListElement head; // pointer to first element
private ListElement tail; // pointer last element
void addFront(int data) //add in front
{
ListElement a = new ListElement(); //creating a new element
a.data = data; //is data.
// pointer to next element TTis isT null
if(head == null) //if the list is empty
{ //then we indicate the start and end links to the new element
head = a; //T.is. Tisis TT isisT
tail = a;
}
else {
a.next = head; //is isisT Tisis isT "former" is
head = a; // pointer to first element Tisis isT isisT
}
}
void addBack(int data) { //isis is
ListElement a = new ListElement(); //creating a new element
a.data = data;
if (tail == null) //if the list is empty
{ //then we indicate the start and end links to the new element
head = a; //T.is. Tisis TT isisT
tail = a;
} else {
tail.next = a; //is "T" is isisT Tisis isT
tail = a; // Tis is isisT is is isisT
}
}
void printList() //isT
{
ListElement t = this.head; //is is isisT
while (t != null) // isisT isTis
{
System.out.print(t.data + " "); //isTis is data
t = t.next; // isisis is
}
System.out.println();
}
public void addAfter(int prevEl, int nextEl){//T T
ListElement t = this.head; //is isisisT
while (t!=null){
if(prevEl==t.data){
ListElement e = new ListElement();
e.data = nextEl;
e.next = t.next;
t.next = e;
t =e;
}
t = t.next;
}
}
void delEl(int data) //isis isisT
{
if(head == null) //if the list is empty -
return; //is is isis
if (head == tail) { //is TT isisT
head = null; //is Tis
tail = null;
return; //and we leave
}
if (head.data == data) { //is is isisT - TT, T is
head = head.next; //isisis Tis T isisT
return; //and we leave
}
ListElement t = head; //is is T
while (t.next != null) { // is isisT isTisT
if (t.next.data == data) { //isis is isisT
if(tail == t.next) //is is
{
tail = t; //T isisis Tis is isisT Tis
}
t.next = t.next.next; //is isisT is
return; //and we leave
}
t = t.next; //is is is
}
}
}
public class ListTest {
public static void main(String[] args) {
List l = new List();
for(int i = 0; i<=5; i++){
l.addBack(i);
}
l.printList();
l.addAfter(4, 4);
l.printList();
}
}
Method public void addAfter(int prevEl, int nextEl)
– I implemented it for you, which is not clear – ask in the comments. The ListTest
class is for testing your list. I tested it – it seems to work.