I have done almost all things in my project. I think file SlList.java in my project has something wrong( in insert function). When I insert something in the middle of link list, I rest disappear although I link the rest to the next of the node I want to insert. Please help my find out my problem. By the way, please correct every mistake in my project . Thanks ^^.
This is link of my project: http://www.mediafire.com/?gybzun3xca8zg86

Recommended Answers

All 4 Replies

By the way, please correct every mistake in my project . Thanks ^^.

made my day :D

No way I'm downloading a RAR from an unknown source.
Publish your code in this thread, correctly indented, in code tags if you want anyone to read ut.

Node.java

public class Node <E>{
     protected E data;
     //double coefficient;
    // int degree;
     
    //public Node( double coefficient, int degree )
    //{
    //   this.coefficient = coefficient;
    //   this.degree = degree;
    //}

    public Node(E data) {
        this.data = data;
    }
    
    public void print() {
        System.out.println(data);
    }

    public E getData() {
        return data;
    }
}

///////////////////////////////////////////////////////////////////////////////////
SlNode.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DSA.Data;

/**
 *
 * @author Nguyen Doan Tung
 */
public class SlNode <E> extends Node<E>{
   private SlNode<E> nextNode;

    public SlNode(E data) {
        super(data);
    }
    
    public SlNode(SlNode nextNode, E data) {
        super(data);
        this.nextNode = nextNode;
    }
    
    public void setNext(SlNode<E> next) {
        this.nextNode = next;
    }
    
    public SlNode<E> getNext() {
        return this.nextNode;
    }

    @Override
    public void print() {
        System.out.print("["+this.data + ", "+ this.nextNode +"]");
    }
}

///////////////////////////////////////////////////////////////////////////////////////
SlList.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DSA.W02_List;

import DSA.Data.SlNode;

/**
 *
 * @param <E> 
 * @author Nguyen Doan Tung
 */
public class SlList<E> implements dsa.W02_List.List<E> {
    
    SlNode<E> head, tail;
    private SlNode<E> SlNode;

    @Override
    public boolean isEmpty() {
        return (getHead() == null && tail == null);

    }

    @Override
    public boolean isFull() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int size() {
        int count = 0;
        SlNode< E > current = getHead();
        while ( current != null)
        {
            count ++;
            current = current.getNext();
        }
        return count;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void add(E slNode) {
        if ( isEmpty() ) {
            setHead((SlNode< E >) slNode);
            tail = getHead();
        }
        else
        {
            tail.setNext((SlNode < E >) slNode);
            tail = (SlNode<E>) slNode;
        }
    }

    @Override
    
    public void insert(int pos, E slNode) {
     int count = 0;int i = 0;
        SlNode< E > tmp = getHead();
        while ( tmp != null)
        {
            count ++;
            tmp = tmp.getNext();
        }
        SlNode< E > current = getHead();
        SlNode< E > previous = getHead();
        if(pos > count)
        {
            throw new UnsupportedOperationException("Out of bound");
        }
        else
            while(i != pos)
            {
                previous = current;
                current = current.getNext();
                i++;
            }

            if(pos >= i)
            {
                current.setNext((SlNode<E>) slNode);
            }
            else
            {
                SlNode.setNext(current.getNext());
                current.setNext(SlNode);
             }
        
    }


    
    @Override
    public void remove(int pos) {
        int count = 0;int i = 0;
        SlNode< E > tmp = getHead();
        while ( tmp != null)
        {
            count ++;
            tmp = tmp.getNext();
        }
        SlNode< E > current = getHead();
        SlNode< E > previous = getHead();
        if(pos > count)
        {
            throw new UnsupportedOperationException("Out of bound");
        }
        else {
            while(i != pos)
            {
                previous = current;
                current = current.getNext();
                i++;
            }
        }
        if(current == getHead())
        {
            setHead(getHead().getNext());
        }
        else
        {
           previous.nextNode = current.nextNode;
        }
    }

    @Override
    public void removeAll() {
        /*
         * TODO:
         */
//        throw new UnsupportedOperationException("Not supported yet.");

        setHead(null);
        tail = null;
    }

    @Override
    public void printAll() {
        /*
         * TODO:
         */
//        throw new UnsupportedOperationException("Not supported yet.");
        System.out.println("Print the list: ");
        SlNode< E > current = getHead();
        while (current != null) {
            current.print();
            current = current.getNext();
        }
    }

    /**
     * @return the head
     */
    public SlNode<E> getHead() {
        return head;
    }

    /**
     * @param head the head to set
     */
    public void setHead(SlNode<E> head) {
        this.head = head;
    }
}

///////////////////////////////////////////////////////////////////////////////////////
TestList.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DSA.W02_List;

import DSA.Data.SlNode;

/**
 *
 * @author Nguyen Doan Tung
 */
public class TestList {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        SlList slList = new SlList();
        
        SlNode slNode1 = new SlNode(1);
        SlNode slNode3 = new SlNode(3);
        SlNode slNode2 = new SlNode(2);
        SlNode<Integer> slNode4 = new SlNode<Integer>(4);
        slList.add(slNode1);
        System.out.println("Size of the List is: " + slList.size());
        slList.add(slNode3);
        slList.add(slNode2);
        //slList.remove(1);
        slList.insert(0,slNode4);
       
        System.out.println("Size of the List is: " + slList.size());
        slList.printAll();
    }
}
if(pos > count)
        {
            throw new UnsupportedOperationException("Out of bound");
        }
        else
            while(i != pos)
            {
                previous = current;
                current = current.getNext();
                i++;
            }
 
            if(pos >= i)
            {
                current.setNext((SlNode<E>) slNode);
            }
            else
            {
                SlNode.setNext(current.getNext());
                current.setNext(SlNode);
             }

here you are checking if pos is over the current size of your chained list, which is fine, but once that is checked, there is no need to recheck it before inserting

if(pos >= i)

you do this if RIGHT AFTER a while that exited because it reached : "i==pos" so you KNOW that your current node IS "i" , no if else monkey buisness needed here...

-save the current node in a tempNode.
-go to that node's previous node and save it as current.
-set the current node's NEXT to "slNode" the argument being inserted.
-set "slNode" 's PREVIOUS to the current node.
-set "slNode" 's NEXT to the tempNode.

voila. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.