Please help with the following in JAVA
static class Node {
String element;
public Node next;
public Node prev;
public Node(String element){
this.element = element;
}
}
public static void main(String[] args){
Node firstNode=new Node("one"); //create the first node referenced by head
Node second=new Node("two");
head.next=second; second.prev=firstNode; //create the second node and attach it to the first
Node third=new Node("three");
second.next=third; third.prev=second; //create the third node and attach it to the second
Node fourth=new Node("four");
third.next=fourth; fourth.prev=third; //create the fourth node and attach it to the third
Node fifth=new Node("five");
four.next= fifth; fifth.prev= four; //create the fifth node and attach it to the fourth
Node lastNode= fifth;
System.out.println("\n ---The original doubly linked list - forward");
Node<String> current=firstNode;
while(current!=null) {System.out.print(current.element+" "); current=current.next;}
System.out.println("\n ---The original doubly linked list - backward");
current=lastNode;
while(current!=null) {System.out.print(current.element+" ");current=current.prev;}
/* Remove node index 0, node index 2 and index 4 from the list*/
//Add your code here
System.out.println("\n ---The list after removing even-index nodes - forward");
current=firstNode;
while(current!=null) {System.out.print(current.element+" "); current=current.next;}
System.out.println("\n ---The list after removing even-index nodes - backward");
current=lastNode;
while(current!=null) {System.out.print(current.element+" "); current=current.prev;}
System.out.println();
}
}
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps
- Assume you have a class SLNode representing a node in a singly-linked list and a variable called list referencing the first element on a list of integers, as shown below: public class SLNode { private E data; private SLNode next; public SLNode( E e){ data = e; next = null; } public SLNode getNext() { return next; } public void setNext( SLNoden){ next = n; } } SLNode list; Write a fragment of Java code that would append a new node with data value 21 at the end of the list. Assume that you don't know if the list has any elements in it or not (i.e., it may be empty). Do not write a complete method, but just show a necessary fragment of code.arrow_forward#include <iostream> usingnamespace std; class Queue { int size; int* queue; public: Queue(){ size = 0; queue = new int[100]; } void add(int data){ queue[size]= data; size++; } void remove(){ if(size ==0){ cout <<"Queue is empty"<<endl; return; } else{ for(int i =0; i < size -1; i++){ queue[i]= queue[i +1]; } size--; } } void print(){ if(size ==0){ cout <<"Queue is empty"<<endl; return; } for(int i =0; i < size; i++){ cout<<queue[i]<<" <- "; } cout << endl; } //your code goes here }; int main(){ Queue q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); Queue q2; q2.add(3); q2.add(66); q2.add(128); q2.add(5); Queue q3 = q1+q2; q3.print();…arrow_forward#include <iostream>#include <cstdlib>using namespace std; class IntNode {public: IntNode(int dataInit = 0, IntNode* nextLoc = nullptr); void InsertAfter(IntNode* nodeLoc); IntNode* GetNext(); void PrintNodeData(); int GetDataVal();private: int dataVal; IntNode* nextNodePtr;}; // ConstructorIntNode::IntNode(int dataInit, IntNode* nextLoc) { this->dataVal = dataInit; this->nextNodePtr = nextLoc;} /* Insert node after this node. * Before: this -- next * After: this -- node -- next */void IntNode::InsertAfter(IntNode* nodeLoc) { IntNode* tmpNext = nullptr; tmpNext = this->nextNodePtr; // Remember next this->nextNodePtr = nodeLoc; // this -- node -- ? nodeLoc->nextNodePtr = tmpNext; // this -- node -- next} // Print dataValvoid IntNode::PrintNodeData() { cout << this->dataVal << endl;} // Grab location pointed by nextNodePtrIntNode* IntNode::GetNext() { return this->nextNodePtr;} int IntNode::GetDataVal() {…arrow_forward
- class BSTNode { int key; BSTNode left, right; public BSTNode(int item) { key = item; left = right = null; } } class BST_Tree { BSTNode root; BST_Tree() { // Constructor root = null; } boolean search(int key){ return (searchRec(root, key) != null); } public BSTNode searchRec(BSTNode root, int key) { if (root==null || root.key==key) return root; if (root.key > key) return searchRec(root.left, key); return searchRec (root.right, key); } void deleteKey(int key) { root = deleteRec(root, key); } /* A recursive function to insert a new key in BST */ BSTNode deleteRec(BSTNode root, int key) { /* Base Case: If the tree is empty */ if (root == null) return root; /* Otherwise, recur down the tree */ if (key < root.key)…arrow_forwardWhat happens when a programmer attempts to access a node's data fields when the node variable refers to None? How do you guard against it? *PYTHONarrow_forward1 Assume some Node class with info & link fields. Complete this method in class List that returns a reference to the node containing the data item in the argument find This, Assume that find this is in the list public class List { protected Node head; protected int size; fublic Public Node find (char find This)arrow_forward
- The numbers on the left margin denote line numbers. line#01 class LL_node {02 int val;03 LL_node next;04 public LL_node (int n) {05 val = n;06 next = null;07 }08 public void set_next (LL_node nextNode) {09 next = nextNode;10 }11 public LL_node get_next () {12 return next;13 }14 public void set_value (int input) {15 val = input;16 }17 public int get_value () {18 return val;19 }20 }21 public class LL {22 protected LL_node head;23 protected LL_node tail;24 public LL () {25 head = null;26 tail = null;27 }28 public int append (int n) {29 if (head == null) {30 head = new LL_node(n);31 tail = head;32 } else {33 LL_node new_node;34 new_node = new LL_node(n);35 tail.set_next (new_node);36 tail = new_node;37 }38 return n;39 }40 } which the following statement…arrow_forwardclass BSTNode { int key; BSTNode left, right; public BSTNode(int item) { key = item; left = right = null; } } class BST_Tree { BSTNode root; BST_Tree() { // Constructor root = null; } boolean search(int key){ return (searchRec(root, key) != null); } public BSTNode searchRec(BSTNode root, int key) { if (root==null || root.key==key) return root; if (root.key > key) return searchRec(root.left, key); return searchRec (root.right, key); } void deleteKey(int key) { root = deleteRec(root, key); } /* A recursive function to insert a new key in BST */ BSTNode deleteRec(BSTNode root, int key) { /* Base Case: If the tree is empty */ if (root == null) return root; /* Otherwise, recur down the tree */ if (key < root.key)…arrow_forwardclass BSTNode { int key; BSTNode left, right; public BSTNode(int item) { key = item; left = right = null; } } class BST_Tree { BSTNode root; BST_Tree() { // Constructor root = null; } boolean search(int key){ return (searchRec(root, key) != null); } public BSTNode searchRec(BSTNode root, int key) { if (root==null || root.key==key) return root; if (root.key > key) return searchRec(root.left, key); return searchRec (root.right, key); } void deleteKey(int key) { root = deleteRec(root, key); } /* A recursive function to insert a new key in BST */ BSTNode deleteRec(BSTNode root, int key) { /* Base Case: If the tree is empty */ if (root == null) return root; /* Otherwise, recur down the tree */ if (key < root.key)…arrow_forward
- Java data strucarrow_forward1.Assume some Node class with info link fields . Complete this method in class List that returns a reference to the node containing the data item in the argument findThis, Assume that findThis is in the list public class list { protected Node head ; Protected int size ; Public Node find (char find this) { Node curr = head; while(curr != null) { if(curr.info == findThis) return curr; curr = curr.link; } return -1; } }arrow_forwardComputer science JAVA programming question i need help with this entire question pleasearrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY