Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class LinkedList{
  5. private Node first = null;
  6. private class Node{
  7. int data;
  8. Node next;
  9. }
  10. public void insert(int x){
  11. if(first==null){
  12. Node first = new Node();
  13. first.data = x;
  14. first.next = null;
  15. }
  16. else{
  17. Node old = first;
  18. first = new Node();
  19. first.data = x;
  20. first.next = old;
  21. }
  22. }
  23.  
  24. public int delete(){
  25. // if(first==null){ return null; }
  26. int item = first.data;
  27. if(first.next != null){
  28. first = first.next;
  29. }
  30. else { first = null;}
  31. return item;
  32. }
  33. public void size(){}
  34. public static void main(String args[]){
  35. LinkedList n = new LinkedList();
  36. n.insert(1);
  37. n.insert(2);
  38. n.insert(3);
  39. n.insert(4);
  40. n.insert(5);
  41. // while(n.next!=null){
  42. int x;
  43. x = n.delete();
  44.  
  45. // }
  46. }
  47. }
  48.  
  49. Welcome to DrJava. Working directory is C:UsersmohitDr Java
  50. > run LinkedList
  51. java.lang.NullPointerException
  52. at LinkedList.delete(LinkedList.java:26)
  53. at LinkedList.main(LinkedList.java:43)
  54. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  55. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  56. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  57. at java.lang.reflect.Method.invoke(Unknown Source)
  58. at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
  59. >
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement