Results 1 to 2 of 2
  1. #1
    mamoonrizwan is offline Member
    Join Date
    Apr 2011
    Posts
    19
    Rep Power
    0

    Default program compilation problem

    public class SingleLinkedListTest {



    SingleLinkedList<String> names = new SingleLinkedList<String>();
    names.addFirst("Sam");

    }


    public class SingleLinkedList<E>{

    private Node<E> head;
    private int size;

    //add element at first node of the list
    public void addFirst(E dataItem){
    head = new Node<E>(dataItem, head);
    size++;


    }

    // adds element after the specified node of the list
    private void addAfter(Node<E> node, E dataItem){

    node.next = new Node<E>(dataItem, node.next);
    size++;
    }

    // removes the first node from the list
    @SuppressWarnings("unused")
    private E removeFirst(){
    Node<E> temp = head;

    if (temp != null)
    head = head.next;
    if (head!= null){
    size--;
    return temp.data;

    }
    else
    return null;

    }

    //removes an element after a specified node of the list
    @SuppressWarnings("unused")
    private E removeAfter(Node<E> node){

    Node<E> temp = node.next;
    if (temp != null){

    node.next = temp.next;
    size--;
    return temp.data;

    }
    else
    return null;
    }

    // gets a string representation of the elements present in a list
    public String toString(){

    @SuppressWarnings("unchecked")
    Node<String> nodeRef = (Node<String>) head;

    String result = "";

    while(nodeRef != null){

    result.concat(nodeRef.data);
    if (nodeRef.next != null)
    result.concat("==>>");
    nodeRef = nodeRef.next;


    }
    return result;
    }


    private Node<E> getNode(int index){
    Node<E> node = head;
    for(int i= 0; i < index && node != null; i++){
    node = node.next;

    }
    return node;
    }

    // gets an element at the specified index
    public E get (int index){

    if(index < 0 || index >= size )
    throw new IndexOutOfBoundsException(Integer.toString(index)) ;


    Node<E> node = getNode(index);
    return node.data;
    }
    // sets an element at the specified index
    public E set(int index, E dataItem){
    if (index < 0 || index >= size )
    throw new IndexOutOfBoundsException(Integer.toString(index)) ;


    Node<E> node = getNode(index);
    E result = node.data;
    node.data = dataItem;
    return result;
    }

    // adds an element at the specified index
    public void add(int index, E dataItem){
    if (index < 0 || index > size)
    throw new IndexOutOfBoundsException(Integer.toString(index)) ;

    if (index== 0){
    addFirst(dataItem);
    }

    else
    {
    Node<E> node = getNode(index-1);
    addAfter(node,dataItem);
    }
    }

    // adds an element at the end of the list
    public boolean add(E dataItem){

    add(size,dataItem);
    return true;

    }














    private static class Node<E>{

    private E data;
    private Node<E> next;

    private Node(E dataItem){
    data = dataItem;
    next = null;

    }

    private Node(E dataItem, Node<E> nodeRef){
    data = dataItem;
    next = nodeRef;

    }
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,299
    Blog Entries
    7
    Rep Power
    24

    Default Re: program compilation problem

    What are we supposed to do now? Guess the syntax error? What do we win?

    Jos

    ps. I moved this thread to the New To Java section; there is nothing advanced about it.
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Image databae compilation problem
    By commissioner in forum Java Servlet
    Replies: 3
    Last Post: 04-09-2011, 05:32 AM
  2. Unresolved compilation problem
    By ls7897 in forum New To Java
    Replies: 9
    Last Post: 03-09-2011, 02:42 AM
  3. Compilation Problem
    By whosadork in forum New To Java
    Replies: 6
    Last Post: 09-10-2008, 02:50 AM
  4. Unresolved compilation problem
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-30-2007, 08:17 PM
  5. Unresolved compilation problem
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 12:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •