Question

In: Computer Science

Suppose a linklist consists of more than 50 node, write a code to delete the tail...

Suppose a linklist consists of more than 50 node, write a code to delete the tail node

(IN JAVA)

Solutions

Expert Solution

Tail node means the last node of linked list.

Java Code:

public class Delete_last_node {

   // this class represent a node in single linked list.
   static class Node {
       int data;
       Node next;
   };
// this function used to insert a node at the beginning at the linked list
   static Node add_Node_At_Beginning(Node head, int new_data)
   {
       Node NewNode = new Node();
       NewNode.data = new_data;
       NewNode.next = head;
       head = NewNode;
       return head;
   }
// this funcion will delete the tail node
   static Node Delete_tail_node(Node head)
   {
   //if head==null , that means no node exists ,then we just return null
       if (head == null)
           return null;
// if only one node exists
       if (head.next == null) {
           return null;
       }
//temp is a variable which is used to find second last(tail) node
       Node temp = head;
       while (temp.next.next != null)
       {
           temp = temp.next;
       }
//NOW(after while loop) temp is holding second last node.
//now we will make temp.next=null to delete the tail node
       temp.next = null;

       return head;
   }


   public static void main(String args[])
   {
  
// here we create a sample linked iist to test our program

Node head = null;
       head = add_Node_At_Beginning(head, 50);
       head = add_Node_At_Beginning(head, 40);
       head = add_Node_At_Beginning(head, 30);
       head = add_Node_At_Beginning(head, 20);
       head = add_Node_At_Beginning(head, 10);
       Node temp=head;
System.out.print("linked list is: ");
  
// this while loop will print the linked list
while(temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();

// now using Delete_tail_node() method we delete the tail node .
// then again we will print the linked list
       head = Delete_tail_node(head);
System.out.println("Deleting the tail node ");
   temp=head;
System.out.print("Now the linked list is: ");   
while(temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
  
   }
}

Code In my complier:

Output:


Related Solutions

Suppose a linklist consists of more than 50 node, write a code to delete the tail...
Suppose a linklist consists of more than 50 node, write a code to delete the tail node   in java
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list....
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list. So if the item is 7 and the list is [1,3,7,4,7,3,7,2], the result is [1,3,7,4,7,3,2] 2- Use LinkList. Write removeAll(int n). Deletes all occurrences of an item n from a linked list. So if the item is 7 and the list1 is [1,3,7,4,7,3,2] , then list1.removeAll(7) then list1 becomes [1,3,4,3,2]. Demonstrate by displaying the list contents before and after calling the above methods. Eg:...
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list....
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list. So if the item is 7 and the list is [1,3,7,4,7,3,7,2], the result is [1,3,7,4,7,3,2] 2- Use LinkList. Write removeAll(int n). Deletes all occurrences of an item n from a linked list. So if the item is 7 and the list1 is [1,3,7,4,7,3,2] , then list1.removeAll(7) then list1 becomes [1,3,4,3,2]. Demonstrate by displaying the list contents before and after calling the above methods. Eg:...
In C++, write a member method delete() that deletes a node from a linked list at...
In C++, write a member method delete() that deletes a node from a linked list at a random position. (It should first randomly generate that position. and then delete that node).
please write simple python code Write a program to replicate the behavior of UNIX utility “tail”....
please write simple python code Write a program to replicate the behavior of UNIX utility “tail”. It takes one or more files and displays requested number of lines from the ending. If number is not specified, then it print 10 lines by default.
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
Write a ministerial brief of no more than 200 words containing no more than three graphs...
Write a ministerial brief of no more than 200 words containing no more than three graphs on whether you can confidently recommend investing in the Bitcoin, compared with ONE of three shares: BHP, CBA, and TLS.   Weekly returns Date CBA.AX TLS.AX BTC BHP 27/9/15 4/10/15 2.92% 0.36% 3.91% 14.22% 11/10/15 1.35% -3.55% 5.66% -4.50% 18/10/15 1.05% 2.57% 10.01% -0.86% 25/10/15 -0.80% -3.23% 12.19% -8.00% 1/11/15 -0.18% -1.67% 15.05% -6.51% 8/11/15 -1.08% -3.20% -14.56% -7.61% 15/11/15 5.37% 5.25% 1.68% 2.43% 22/11/15...
Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string.
Programming CWrite the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include:• Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node.• Find a node by integer value: This function takes in two parameters: the root...
Python 3 Fix the code so if the user enter the same bar code more than...
Python 3 Fix the code so if the user enter the same bar code more than three times, it shows a warning message indicating that the product was already tested 3 times and it reached the limits Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox from datetime import datetime window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1, weight=1) window.grid_rowconfigure(1, weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20,...
Suppose that the security code for a particular automobile’s locks consists of 4 bytes of data...
Suppose that the security code for a particular automobile’s locks consists of 4 bytes of data transmitted via a low power radio. A byte is either sent correctly or is distorted by interference. The car locks are only opened if all 4 bytes are received correctly. Let C be the event that a byte is received correctly and F be the event that the byte is distorted in transmission. Then CCCC would mean that all four bytes were received correctly...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT