Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Here I tried to implement singly Linked list in Java. Here I implemented adding element to the list,removing element from the list etc. Please tell me whether implementation is correct or not? If it's correct if there's any other good way to implement singly linked list in java??

import java.lang.*;
import java.util.*;

public class Node
{
   Node data;
   node next;
}

public class SinglyLinkeList
{
   Node start;
   int size;
}

 public SinnglyLinkedList()
{
  start=null;
  size=0;
}



public void add(Node data)
{
   if(size=0)
   {
      start=new Node();
      start.next=null;
      start.data=data;
   }
   else
   {
      Node currentnode=getnode(size-1);
      Node newnode=new Node();
      newnode.data=data;
      newnode.next=null;
      currentnode.next=newnode;
   }
   size++;
  }

  public void insertfront(Node data)
   {
     if(size==0)
    {
      Node newnode=new Node();
      start.next=null;
      start.data=data;
    }
    else        
    {
        Node newnode=new Node();
         newnode.data=data;
        newnode.next=start;
   }
   size++;
  }

   public void insertAt(int position,Node data)
   {
      if(position==0)
      {
        insertatfront(Node data);
       }
       else if(position==size-1)
      {
        insertatlast(data);
      }
       else
      {
         Node tempnode=getNodeAt(position-1);
         Node newnode= new Node();
         newnode.data=data;
         newnode.next=tempnode.next;
         size++;
      }
   }

 public Node getFirst()
 {
    return getNodeAt(0);
 }
  public Node getLast()
  {
     return getNodeAt(size-1);
  } 
  public Node removeAtFirst()
  {
     if(size==0)
     {
        System.out.println("Empty List ");
     } 
     else
     {
         Node tempnode=getNodeAt(position-1);
         Node data=tempnode.next.data;
         tempnode.next=tempnode.next.next;
         size--;
           return data;
     }
 }

      Node data=start.data;
      start=start.next;
      size--;
      return data;
  }
 }
 public Node removeAtLast()
 {
   if(size==0)
   {
      System.out.println("Empty List ");
   }
   else
   {
       Node data=getNodeAt(size-1);
       Node data=tempnode.next.data;
       size--;
       return data;
   }
}

   public string void main(string[] args)
  {

    LinkedList l1=new LinkedList();
    BufferReader bf=new BufferReader(new InputStreamReader(Sysyem.in));
    System.out.println("1->Add Element 2->Remove Last 3->Insert Front 4->Insert at        position 5->REmove Front 6-> Remove At Last 7->Exit ");
string choice;
choice.readline();
int choiceNum= Integer.parseInt(choice);
string str;
switch(choiceNum)
{
    case 1: System.out.println("Enter the element to be inserted ");
        str =bf.readline();
        l1.add(str);
        break;

    case 2: System.out.println("Linked List before removing element : "+l1);
        System.out.println("Linked List after removing element  : ");
        l1.removeLast();
        break;

    case 3: System.out.println("Linked List before inserting element at first: "+l1);
        System.out.println("Enter the element to be inserted at first: ");
        str.readline(); 
        l1.insertfront(str);
        System.out.println("Linked List after inserting at first : "+l1);
        break;

    case 4: System.out.println("Linked List before inserting element at particular position: "+l1);
        System.out.println("Enter the element position & element : ");
        str.readline();
        string str1;
        str1.readline();
        l1.insertAt(str,str1);
        System.out.println("Linked List after inserting at particular Position :"+l1);
        break;

    case 5: System.out.println("Linked LIst before removing front element: "+l1);
        System.out.println("Linked List after removing front element : ");
        l1.removeAtFirst();
        break;

    case 6: System.out.println("Linked List before removing last element: "+l1);
        System.out.println("Linked List after removing last element :");
        l1.removeAtLast();

    default:break;
    }
   }
   }
share|improve this question

closed as off topic by Jeff Mercado, Landei, palacsint, sepp2k Dec 28 '11 at 14:12

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

8  
There is a whole slew of problems in that code. It wouldn't even compile. You should focus on getting a working implementation first. –  Jeff Mercado Dec 23 '11 at 11:08
4  
Also consider indenting consistently; most IDEs will do this for you. It makes looking at, and thinking about, code much easier. –  Dave Newton Dec 24 '11 at 18:17
    
Try to weed out your many errors and repost your code. If it doesn't compile it's not a very good implementation... If you need help with the errors please edit and post the relevant info. –  Bill.Caffery Dec 27 '11 at 10:53

1 Answer 1

up vote 6 down vote accepted

This is not a good implementation of a singly linked list in Java, while it does not compile, there are a few issues with it which I will hopefully correct and explain.

The definition of a single Node is not quite correct. A Node in a singly linked list typically stores a piece of information, data of type Object and a link to the next Node. To build a successful linked list, we must first have a working implementation of a Node:

public class Node
{
    public Object data; //the data stored in this node
    public Node next; //store a reference to the next node in this singlylinkedlist
    public Node(Object data,Node next){
        this.data =data;
        this.next =next;
    }
}

In order to create a SinglyLinkedList, which is a linear structure, all we need is a reference to the first Node in the list. Each consecutive Node will point to the next node (see http://en.wikipedia.org/wiki/Linked_list for reference).

public class SinglyLinkeList
{
    Node start;
    public SinnglyLinkedList()
    {
      this.start=null;
    }

    public void addFront(Object newData)
    {
        Node cache = this.start; //store a reference to the current start node
        this.start = new Node(newData,cache); //assign our start to a new node that has newData and points to our old start
    }
    public addRear(Object newData)
    {
        Node cache = start; 
        Node current = null;

        while((current = cache.next) != null) //find the last Node
            cache = cache.next;

        cache.next = new Node(newData,null); //create a new node that has newData and points to null
    }

    public Object getFront()
    {
        return this.start.data; // return the front object's data
    }
}

As your code combined the two class definitions in a single file, the the can be found below:

import java.lang.*;
public class SinglyLinkeList
{
    Node start;
    public SinnglyLinkedList()
    {
      this.start=null;
    }

    public void addFront(Object newData)
    {
        Node cache = this.start; //store a reference to the current start node
        this.start = new Node(newData,cache); //assign our start to a new node that has newData and points to our old start
    }
    public addRear(Object newData)
    {
        Node cache = start; 
        Node current = null;

        while((current = cache.next) != null) //find the last Node
            cache = cache.next;

        cache.next = new Node(newData,null); //create a new node that has newData and points to null
    }

    public Object getFront()
    {
        return this.start.data; // return the front object's data
    }

    public class Node
    {
        public Object data; //the data stored in this node
        public Node next; //store a reference to the next node in this singlylinkedlist
        public Node(Object data,Node next){
            this.data =data;
            this.next =next;
        }
    }
}

Note that it is difficult to tell what sort of an answer is appropriate here on codereview for an implementation that is incorrect or incomplete, my inclination would be to move this thread to StackOverflow and rephrase the question to "how do I implement...", which when searched, would reveal a great many solutions.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.