answer them all please from 59-67 LTE 40, \"il 29% 6:37 PM X Question firstNode
ID: 3910174 • Letter: A
Question
answer them all please from 59-67
LTE 40, "il 29% 6:37 PM X Question firstNode 59. Repe at question 12, but instead write the dass within a package an implementation of the ADT bag. Set and get me 60. List the ation of the ADT bag. Set and get methods will be necessary ps necessary to add a node to the beginning of the doubly linked chain. 61. List th e steps necessary to remove the first node from the beginning of the doubly linked chain. 62. If currentNode in the contains method becomes nu What value does the method retun when the bag is not empty public boolean contains(T anEntry) E boolean found- false; Node currentNode firstNode while (Cfound && (currentNode :e nul) ( if (anEntry.equals(current Node.data) al. found true; else currentNode currentN return fou 63. Trace the execution of the method contains when the bag is empty. What is the re 64. The code that we developed to add a node to an empty chain is Node newNode new Node(newEntry); firstNode newNode; code that we just developed to add to the beginning of a chain is de newNode new Node(newEntry); newNode.next- firstNode; firstNode a newNode; Why do these three statements also work correctly when the chain is empty? 65. What steps are necessary to remove the first node in a chain of five nod 66. What steps are 67, Instead of calling the method getReferenceTo, could es? necessary to remove the third node in a chain of five nodes? method baca have called the original definition of contains ExplainExplanation / Answer
q59- Its says repeat question 12, which is not mentioned so skipping it.
q60.
// Adding a node at the front of the list
public void push(int new_data)
{
/* 1. allocate node
* 2. put in the data */
Node new_Node = new Node(new_data);
/* 3. Make next of new node as head and previous as NULL */
new_Node.next = head;
new_Node.prev = null;
/* 4. change prev of head node to new node */
if (head != null)
head.prev = new_Node;
/* 5. move the head to point to the new node */
head = new_Node;
}
q61.
// Java program to delete a node from doubly linked list
class LinkedList {
static Node head = null;
class Node {
int data;
Node next, prev;
Node(int d) {
data = d;
next = prev = null;
}
}
/*Function to delete a node in a Doubly Linked List.
head_ref --> pointer to head node pointer.
del --> pointer to node to be deleted. */
void deleteNode(Node head_ref, Node del) {
/* base case */
if (head == null || del == null) {
return;
}
/* If node to be deleted is head node */
if (head == del) {
head = del.next;
}
/* Change next only if node to be deleted is NOT the last node */
if (del.next != null) {
del.next.prev = del.prev;
}
/* Change prev only if node to be deleted is NOT the first node */
if (del.prev != null) {
del.prev.next = del.next;
}
/* Finally, free the memory occupied by del*/
return;
}
/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginning of the Doubly Linked List */
void push(Node head_ref, int new_data) {
/* allocate node */
Node new_node = new Node(new_data);
/* since we are adding at the begining,
prev is always NULL */
new_node.prev = null;
/* link the old list off the new node */
new_node.next = (head);
/* change prev of head node to new node */
if ((head) != null) {
(head).prev = new_node;
}
/* move the head to point to the new node */
(head) = new_node;
}
/*Function to print nodes in a given doubly linked list
This function is same as printList() of singly linked lsit */
void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
/* Let us create the doubly linked list 10<->8<->4<->2 */
list.push(head, 2);
list.push(head, 4);
list.push(head, 8);
list.push(head, 10);
System.out.println("Original Linked list ");
list.printList(head);
/* delete nodes from the doubly linked list */
list.deleteNode(head, head); /*delete first node*/
list.deleteNode(head, head.next); /*delete middle node*/
list.deleteNode(head, head.next); /*delete last node*/
System.out.println("");
/* Modified linked list will be NULL<-8->NULL */
System.out.println("Modified Linked List");
list.printList(head);
}
}
q62.
Two scenario if item is present in entry then found = true
else if item is present but not matching with given one then found = false
q63.
if bag is empty the code will return false , it will not able to enter to loop.
q64.
suppose if chain is empty ,new node start filling the chain with new entry ,
q65.to remove the node first node ,
just shift the head pointer to next node automatically first node is removed
Step 1 : create a function which takes a linked list and node that had to be deleted as arguments and delete the node.
Step 2 : If you want to delete a head node.
a) Change the head pointer to next of current node (head here).
b) Change the previous pointer of next node to current node previous.
Step 3 : If you want to delete middle node.
a) Change the previous pointer of next node to current node previous
b) Change the previous node next pointer to next of current node.
c) Delete the node. (current node)
d) If previous or next is NULL you need not delete them. (for deleting last node)
Step 4 : On the given linked list, call the function and give which node you want to delete.
q66.
for everything steps will be same
number doesnt change the rule
here is the generalized rule
Step 1 : create a function which takes a linked list and node that had to be deleted as arguments and delete the node.
Step 2 : If you want to delete a head node.
a) Change the head pointer to next of current node (head here).
b) Change the previous pointer of next node to current node previous.
Step 3 : If you want to delete middle node.
a) Change the previous pointer of next node to current node previous
b) Change the previous node next pointer to next of current node.
c) Delete the node. (current node)
d) If previous or next is NULL you need not delete them. (for deleting last node)
Step 4 : On the given linked list, call the function and give which node you want to delete.
q67.
Can you please upload the snapshot of method GETREFERECETO(), Then only answer could be done.
// Adding a node at the front of the list
public void push(int new_data)
{
/* 1. allocate node
* 2. put in the data */
Node new_Node = new Node(new_data);
/* 3. Make next of new node as head and previous as NULL */
new_Node.next = head;
new_Node.prev = null;
/* 4. change prev of head node to new node */
if (head != null)
head.prev = new_Node;
/* 5. move the head to point to the new node */
head = new_Node;
}
q61.
// Java program to delete a node from doubly linked list
class LinkedList {
static Node head = null;
class Node {
int data;
Node next, prev;
Node(int d) {
data = d;
next = prev = null;
}
}
/*Function to delete a node in a Doubly Linked List.
head_ref --> pointer to head node pointer.
del --> pointer to node to be deleted. */
void deleteNode(Node head_ref, Node del) {
/* base case */
if (head == null || del == null) {
return;
}
/* If node to be deleted is head node */
if (head == del) {
head = del.next;
}
/* Change next only if node to be deleted is NOT the last node */
if (del.next != null) {
del.next.prev = del.prev;
}
/* Change prev only if node to be deleted is NOT the first node */
if (del.prev != null) {
del.prev.next = del.next;
}
/* Finally, free the memory occupied by del*/
return;
}
/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginning of the Doubly Linked List */
void push(Node head_ref, int new_data) {
/* allocate node */
Node new_node = new Node(new_data);
/* since we are adding at the begining,
prev is always NULL */
new_node.prev = null;
/* link the old list off the new node */
new_node.next = (head);
/* change prev of head node to new node */
if ((head) != null) {
(head).prev = new_node;
}
/* move the head to point to the new node */
(head) = new_node;
}
/*Function to print nodes in a given doubly linked list
This function is same as printList() of singly linked lsit */
void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
/* Let us create the doubly linked list 10<->8<->4<->2 */
list.push(head, 2);
list.push(head, 4);
list.push(head, 8);
list.push(head, 10);
System.out.println("Original Linked list ");
list.printList(head);
/* delete nodes from the doubly linked list */
list.deleteNode(head, head); /*delete first node*/
list.deleteNode(head, head.next); /*delete middle node*/
list.deleteNode(head, head.next); /*delete last node*/
System.out.println("");
/* Modified linked list will be NULL<-8->NULL */
System.out.println("Modified Linked List");
list.printList(head);
}
}
q62.
Two scenario if item is present in entry then found = true
else if item is present but not matching with given one then found = false
q63.
if bag is empty the code will return false , it will not able to enter to loop.
q64.
suppose if chain is empty ,new node start filling the chain with new entry ,
q65.to remove the node first node ,
just shift the head pointer to next node automatically first node is removed
Step 1 : create a function which takes a linked list and node that had to be deleted as arguments and delete the node.
Step 2 : If you want to delete a head node.
a) Change the head pointer to next of current node (head here).
b) Change the previous pointer of next node to current node previous.
Step 3 : If you want to delete middle node.
a) Change the previous pointer of next node to current node previous
b) Change the previous node next pointer to next of current node.
c) Delete the node. (current node)
d) If previous or next is NULL you need not delete them. (for deleting last node)
Step 4 : On the given linked list, call the function and give which node you want to delete.
q66.
for everything steps will be same
number doesnt change the rule
here is the generalized rule
Step 1 : create a function which takes a linked list and node that had to be deleted as arguments and delete the node.
Step 2 : If you want to delete a head node.
a) Change the head pointer to next of current node (head here).
b) Change the previous pointer of next node to current node previous.
Step 3 : If you want to delete middle node.
a) Change the previous pointer of next node to current node previous
b) Change the previous node next pointer to next of current node.
c) Delete the node. (current node)
d) If previous or next is NULL you need not delete them. (for deleting last node)
Step 4 : On the given linked list, call the function and give which node you want to delete.
q67.
Can you please upload the snapshot of method GETREFERECETO(), Then only answer could be done.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.