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).
Write an algorithm in pseudo code to find one element and delete it in a doubly...
Write an algorithm in pseudo code to find one element and delete it in a doubly linked list. Your algorithm will print the original list, request the user to put in an element to be deleted, then print the final list after the deletion is done. If the element doesn’t exist in the list, print "XXX is not in the list" where "XXX" should be the one you received from the user. Use the following as your test cases to...
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...
(2)   In the United States, more than 50% of banks have less than $100 million in...
(2)   In the United States, more than 50% of banks have less than $100 million in assets: T or F? (3)   In 1980, there were 14,404 commercial banks in the United States, but now there are 5,116 in 2020: T or F? (4)   The U.S. banking industry is highly concentrated compared with other industries in the United States or compared with the banking industry in other countries: T or F? (5)   The U.S. banking industry is one of the most...
With over 50% of adults spending more than an hour a day on the Internet, the...
With over 50% of adults spending more than an hour a day on the Internet, the number experiencing computer- or Internet-based crime continues to rise. A survey in 2010 of a random sample of 1020 adults, aged 18 and older, reached by random digit dialing found 122 adults in the sample who said that they or a household member was a victim of a computer or Internet crime on their home computer in the past year. What is a 90...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT