Write Java code (do not use Java libraries) to perform the following on a linked list of integers: prepend a new value
If you have any problem with the code feel free to comment.
Program
/*LinkedList implementation*/
class LinkedList {
Node head;
//node class for holding data and the next node
address
static class Node {
int data;
Node next;
Node(int data) {
this.data =
data;
}
}
//inserting data to the list
public void insert(LinkedList list, int data) {
Node newNode = new Node(data);
newNode.next = null;
if (list.head == null)
list.head =
newNode;
else {
Node last =
list.head;
// traversing
the list to find the last node
while (last.next
!= null) {
last = last.next;
}
// putting the
value after the last node
last.next =
newNode;
}
}
// insert values at the start of the list
public void prepend(LinkedList list, int data) {
Node newNode = new Node(data);
newNode.next = list.head;
list.head = newNode;
}
// delete value from index
public void delete(LinkedList list, int index) {
if (index == 0)
list.head =
list.head.next;
else {
Node node =
list.head;
Node temp =
null;
for (int i =
0; i < index - 1; i++) {
node = node.next;
}
temp =
node.next;
node.next =
temp.next;
}
}
//printing the values of the list
public static void printList(LinkedList list) {
Node currentNode = list.head;
while (currentNode != null) {
System.out.print(currentNode.data + " ");
currentNode =
currentNode.next;
}
System.out.println();
}
}
public class Test {//for testing the linked list
public static void main(String[] args) {
LinkedList list = new
LinkedList();
//inserting values to the
linkedlist
list.insert(list, 1);
list.insert(list, 2);
list.insert(list, 3);
System.out.print("LinkedList after
insert: ");
LinkedList.printList(list);
//deleteing value from the
list
list.delete(list, 2);
System.out.print("LinkedList after
delete: ");
LinkedList.printList(list);
//prepending value to the
list
list.prepend(list, 10);
System.out.print("LinkedList after
prepend: ");
LinkedList.printList(list);
}
}
Output
Write Java code (do not use Java libraries) to perform the following on a linked list...
write the java code to convert a linked list of integers, to a stack of integers. thus your code will traverse the linked listand populate the stsck. linked list 1->2->3. becomes stack 3 First Last 2 1
Java 3) Write the-G-code to convert a linked list of integers, to a stack of integers. Thus, your code will traverse the linked list and populate the stack. [ 15 points ] Linked List 1→ 2→ 3 Stack 3 becomes First Last
C# code Arrays and Linked Lists: Write C++/Java/C#/Python code to declare an array of linked lists of any primitive type you want. (Array of size 2020) (This could be based on MSDN libraries or the lab) – you do not need to instantiate any of the linked lists to contain any actual values. Paste your code for that here (this should only be one line) Based on your code or the lab from 4 or your doubly linked list from...
In Java: Assume the "ferrets" variable points to a linked list of "LLNode." Write code that traverses the list and prints the following. Do not forget to consider the case where the list is empty. The "LLNode" class is given: public class LLNode { protected T info; protected LLNode link; public LLNode(T info) { this.info = info; link = null; } public void setInfo(T info) { this.info = info; } public T getInfo() { return info; } public void setLink(LLNode...
5.8 Sort Linked List Use mergersort to sort a linked list Do not use the following libraries: algorithm, cmath. Do not load the values into a vector and sort them. The sort must be done with a linked list Example Two lists 6->3->1->2->4->5 will return a list with 1->2->3->4->5->6. Input There will be no input read in anymore. Going forward use the hard-coded input on the template and ensure the program works. There will be one compare output test based...
(Java) Linked List. Please explain this with one example or code to understand Linked List: ****Implement the method contains that check if a node is contained in a Linked List.*****
Java - Write an insertBefore method for a List implemented as a Linked List that contains two dummy nodes. - Write an insertAfter method for a List implemented as a Linked List that contains two dummy nodes.
Write a method max() that takes a reference to the first node in a linked list as argument and returns the value of the maximum key in the list. Assume that all keys are positive integers, and return 0 if the list is empty. In java
DO NOT USE ANY EXTERNAL LIBRARIES BESIDES BUILT IN JAVA LIBRARIES! KEEP IT SIMPLE!!! Provided Code: import java.util.Scanner; public class OddAndEven{ /* PART 1: Create a nonstatic method that takes in an int number quantity (n) and returns a returns a String of numbers from 0 to n (inclusive) as the example above demonstrates. Call this quantityToString. In this method you should check that n is between 0(inclusive) and 100(inclusive). If n is outside these boundaries return and empty...
JAVA CODE Write a JAVA program to do the following. Input an integer x. (Should work with “big” numbers.) Create a completely-skewed BST S containing 1, 2, . . . , x. Create a BST R containing x integers without repetitions gen- erated at random. (To minimize the risk of repetitions, you can multiply the value returned by random() by a big number.) Given that the numbers are generated uniformly at random, the tree will likely be balanced. Measure the...