Tuesday, May 1, 2018

LinkedList high level implementation [i]

Problem statement: LinkedList high level implementation in java !
  1. class Node4 {
  2. Node4 next;
  3. int data;

  4. Node4(int data) {
  5. this.data = data;
  6. }
  7. }
  8. public class LinkedListHighLevelExe1 {
  9. Node4 head;

  10. public static void main(String[] args) {
  11. LinkedListHighLevelExe1 l = new LinkedListHighLevelExe1();
  12. l.add(3);
  13. l.add(5);
  14. l.add(8);
  15. l.add(9);
  16. l.print(l.head);
  17. }

  18. // add the element in LinkedList
  19. public void add(int data) {
  20. Node4 next = new Node4(data);
  21. if (head == null) {
  22. head = next;
  23. return;
  24. }
  25. Node4 last = head;
  26. while (last.next != null) {
  27. last = last.next;
  28. }
  29. last.next = next;
  30. }
  31. // print the LinkedList
  32. void print(Node4 next) {
  33. Node4 temp = next;
  34. while (temp != null) {
  35. System.out.print(temp.data + "-->");
  36. temp = temp.next;
  37. }
  38. }
  39. }
Output:
3-->5-->8-->9-->

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...