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

java code

class Node
{
int data;
Node next;
public Node(int data)
{
this.data=data;
}
  
public static void deletetail(Node head)
{
Node current = head;
while(current!=null)
{
if(current.next.next==null)
{
current.next=null;
break;
}
current=current.next;
}
}
  
public static void display(Node head)
{
Node current=head;
while(current!=null)
{
System.out.print(current.data+" ");
current=current.next;
}
System.out.println("");
}
}

public class Main
{
   public static void main(String[] args) {
  
   //Test for 3 nodes
   Node n1=new Node(45);
   Node n2=new Node(40);
   Node n3=new Node(50);
   n1.next=n2;
   n1.next.next=n3;
   n1.display(n1);
   n1.deletetail(n1);
   n1.display(n1);
  
   //Test for more than 50 nodes
   Node head1=new Node(1);
   Node current=head1;
   for(int i=0; i<55; i++)
   {
   current.next = new Node(i);
   current=current.next;
   }
  
   head1.display(head1);
   head1.deletetail(head1);
   head1.display(head1);
   }
}
Sample output

45 40 50 
45 40 
1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 
1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 

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...
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,...
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...
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