Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. class Node{
  2.  
  3. Node next;
  4. Node previous;
  5. int data;
  6.  
  7. public Node(int data){
  8. this.data = data;
  9. }
  10.  
  11. }
  12. public class LinkedList {
  13. Node head;
  14.  
  15.  
  16. public Node push(int data){
  17. Node temp = this.head;
  18. if(head == null){
  19. Node newNode = new Node(data);
  20. newNode.next = head;
  21. return head = newNode;
  22. }
  23. else{
  24. while(temp!=null){
  25. temp.next = new Node(data);
  26. }}
  27. return temp;
  28. }
  29.  
  30. public Node insertAtEnd(int data){
  31. while(head!=null){
  32. head.next = new Node(data);
  33. }
  34. return head.next;
  35. }
  36.  
  37.  
  38.  
  39. public void printList() {
  40. Node temp = this.head;
  41. while (temp != null) {
  42. System.out.print(temp.data + "-->");
  43. temp = temp.next;
  44. }
  45. System.out.println(temp);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement