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

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation