Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. Hi Dear,
  2.  
  3. Question is long.
  4.  
  5. I have answered Q1. Please post others in separate post.
  6.  
  7. I have tested , its working fine.
  8.  
  9. /**
  10. * Created by pravesh on 12/04/18.
  11. */
  12.  
  13. public class LList<T extends Comparable<T>>
  14. {
  15. Node<T> head; // head of list
  16.  
  17. /* Linked list Node*/
  18. class Node<T>
  19. {
  20. T data;
  21. Node<T> next;
  22. Node(T d) {data = d; next = null; }
  23. }
  24. /* Appends a new node at the end. This method is
  25. defined inside LinkedList class shown above */
  26. public void append(T new_data)
  27. {
  28. /* 1. Allocate the Node &
  29. 2. Put in the data
  30. 3. Set next as null */
  31. Node new_node = new Node(new_data);
  32.  
  33. /* 4. If the Linked List is empty, then make the
  34. new node as head */
  35. if (head == null)
  36. {
  37. head = new Node(new_data);
  38. return;
  39. }
  40.  
  41. /* 4. This new node is going to be the last node, so
  42. make next of it as null */
  43. new_node.next = null;
  44.  
  45. /* 5. Else traverse till the last node */
  46. Node last = head;
  47. while (last.next != null)
  48. last = last.next;
  49.  
  50. /* 6. Change the next of last node */
  51. last.next = new_node;
  52. return;
  53. }
  54.  
  55. public int finds(T item) {
  56.  
  57. if(head == null)
  58. return 0;
  59.  
  60. Node<T> temp = head;
  61.  
  62. int i = 0;
  63. while(temp != null) {
  64. i++;
  65. if(temp.data.compareTo(item) == 0)
  66. return i;
  67. temp = temp.next;
  68. }
  69.  
  70. return 0; // not found
  71. }
  72.  
  73. /* This function prints contents of linked list starting from
  74. the given node */
  75. public void printList()
  76. {
  77. Node tnode = head;
  78. while (tnode != null)
  79. {
  80. System.out.print(tnode.data+" ");
  81. tnode = tnode.next;
  82. }
  83. }
  84.  
  85. /* Driver program to test above functions. Ideally this function
  86. should be in a separate user class. It is kept here to keep
  87. code compact */
  88. public static void main(String[] args)
  89. {
  90. /* Start with the empty list */
  91. LList<Integer> llist = new LList<>();
  92.  
  93. llist.append(6);
  94. llist.append(7);
  95.  
  96. llist.append(1);
  97.  
  98. llist.append(4);
  99.  
  100. llist.append( 8);
  101.  
  102. System.out.println("\nCreated Linked list is: ");
  103. llist.printList();
  104.  
  105. System.out.println();
  106. System.out.println("Searching 9");
  107. System.out.println(llist.finds(9));
  108. }
  109. }
  110. Output:
  111.  
  112. Created Linked list is:
  113. 6 7 1 4 8
  114. Searching 9
  115. 0
  116.  
  117. Please DONT forgot to rate my answer. We are working hard for you Guys!!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement