Add a new element L to a one-connected list of each entry of E. element
For example, let’s say there is a list of elements 2, 3, 4, 7, 23, 2, 4. With console, we enter two more items. For example, L = 55 and E = 4. After this, the program should issue a new list of 2,3,4,55,7,23,2,4,55.
I was able to implement only the list. I tried to create in the INSERT method and in a separate method, but I can’t think. How do I implement?
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 wrote contains weaknesses. The vertices of Node
should be aggregated, and implemented by a separate class (not internal). Let’s agree, you before selling a bike, first thug how it is written. 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 the next element
INT DATA; // data
}
Class List {
Private Listelement Head; // pointer to first element
Private Listelement Tail; // pointer the last element
Void AddFront (int data) // Add a front
{
Listelement A = New Listelement (); // Create a new element
a.data = DATA; // Initialize the data.
// pointer to the next element is automatically initialized as NULL
if (Head == NULL) // if the list is empty
{// then indicate the references of the beginning and end to the new element
Head = A; //those. The list now consists of one item
Tail = A;
}
ELSE {
a.next = head; // otherwise the new element now refers to the "former" first
Head = A; // A pointer to the first element now refers to a new element
}
}
Void Addback (int data) {// Adding to the end of the list
Listelement A = New Listelement (); // Create a new element
a.data = DATA;
if (tail == null) // if the list is empty
{// then indicate the references of the beginning and end to the new element
Head = A; //those. The list now consists of one item
Tail = A;
} else {
tail.next = a; // otherwise "old" the last element now refers to the new
Tail = A; // And in the pointer to the last element, write the address of the new element
}
}
Void PrintList () // Print List
{
Listelement T = this.head; // Get a reference to the first element
While (T! = NULL) // While the element is the existence
{
System.Out.print (T.Data + "); // Print its data
T = T.NEXT; // and switch to the next
}
System.Out.printLN ();
}
Public void AddAfter (int prevel, int nextel) {// Mstod that asked you
Listelement T = this.head; // We get a reference to the first element
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) // Removing an item
{
if (Head == null) // if the list is empty -
Return; // do not do anything
if (Head == tail) {// if the list consists of one element
HEAD = NULL; // Clean the start and end indicators
Tail = NULL;
Return; // and come out
}
if (head.data == data) {// If the first element is the one that we need
Head = head.next; // Switch the start pointer to the second element
Return; // and come out
}
Listelement T = Head; // otherwise we start to look
while (t.next! = NULL) {// While the next element exists
if (t.next.data == DATA) {// Check the following element
if (tail == t.next) // if it is last
{
Tail = T; // then switch the pointer to the last item to the current
}
T.NEXT = T.NEXT.NEXT; // found element throw out
Return; // and come out
}
T = T.NEXT; // otherwise looking for further
}
}
}
Public Class Listtest {
Public Static Void Main (String [] Args) {
List L = new list ();
For (int i = 0; i & lt; = 5; i ++) {
L.ADDBACK (I);
}
L.PrintList ();
l.addafter (4, 4);
L.PrintList ();
}
}
Public Void AddAfter (int prevel, int nextel)
– I implemented for you that it is not clear – ask in the comments. Class Listtest
– to test your list. I tested – it seems to work.