Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class LinkedList{
- private Node first = null;
- private class Node{
- int data;
- Node next;
- }
- public void insert(int x){
- if(first==null){
- Node first = new Node();
- first.data = x;
- first.next = null;
- }
- else{
- Node old = first;
- first = new Node();
- first.data = x;
- first.next = old;
- }
- }
- public int delete(){
- // if(first==null){ return null; }
- int item = first.data;
- if(first.next != null){
- first = first.next;
- }
- else { first = null;}
- return item;
- }
- public void size(){}
- public static void main(String args[]){
- LinkedList n = new LinkedList();
- n.insert(1);
- n.insert(2);
- n.insert(3);
- n.insert(4);
- n.insert(5);
- // while(n.next!=null){
- int x;
- x = n.delete();
- // }
- }
- }
- Welcome to DrJava. Working directory is C:UsersmohitDr Java
- > run LinkedList
- java.lang.NullPointerException
- at LinkedList.delete(LinkedList.java:26)
- at LinkedList.main(LinkedList.java:43)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
- at java.lang.reflect.Method.invoke(Unknown Source)
- at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
- >
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement