Knowee
Questions
Features
Study Tools

Public void function(node node) { if(size == 0) head = node; else { node temp,cur; for(cur = head; (Temp = cur.Getnext())!=Null; cur = temp); cur.Setnext(node); } size++; }

Question

Public void function(node node) { if(size == 0) head = node; else { node temp,cur; for(cur = head; (Temp = cur.Getnext())!=Null; cur = temp); cur.Setnext(node); } size++; }

🧐 Not the exact question you are looking for?Go ask a question

Solution

This is a function in Java that adds a new node to the end of a linked list. Here's a step-by-step explanation:

  1. The function is declared as public and does not return anything (void). It is named "function" and takes a node as an argument.

  2. If the size of the linked list is 0, it means the list is empty. So, the new node is set as the head of the list.

  3. If the size is not 0, it means there are elements in the list. In this case, two node variables, temp and cur, are declared.

  4. A for loop is used to traverse the list. The loop continues until it reaches the last node in the list (where Getnext() returns null).

  5. Inside the loop, the current node (cur) is set to the next node (temp). This is how the function traverses the list.

  6. Once the loop ends, it means we've reached the end of the list. The new node is then added to the end of the list by using Setnext() on the current node (cur).

  7. Finally, the size of the list is incremented by 1, as a new node has been added.

This function assumes that the linked list has a size property, a head property, and that the nodes have Getnext() and Setnext() methods.

This problem has been solved

Similar Questions

What is the functionality of the following code?Public void function(node node) { if(size == 0) head = node; else { node temp,cur; for(cur = head; (Temp = cur.Getnext())!=Null; cur = temp); cur.Setnext(node); } size++; }Select one:a. Inserting a node at the beginning of the list.b. Deleting a node at the beginning of the list.c.Inserting a node at the end of the list.d.Deleting a node at the end of the list.

void myFun(Node* head) {Node* current = head;while (current != nullptr) {if((current->data)%2==0)std::cout << current->data << " "; current = current->next;}std::cout << std::endl; }Output of above code in linked list

public void display() { if(size == 0) System.out.println("underflow"); else { Node current = first; while(current != null) { System.out.println(current.getEle()); current = current.getNext(); } }}

Deletion of the last node:Locate the node before the last node (let it be temp)Keep the address of the node next to the last node in tempDelete the last memoryPut temp at the end

#include<stdio.h>#include<stdlib.h>typedef struct node{ struct node * left; int data; struct node * right;}NODE;… scanf("%d",&num); if( num == -1) break; createBst(&root , num); } printMinimumMaximum(root);return 0;}

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.