need help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satan
    New Member
    • Oct 2006
    • 28

    #1

    need help

    I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinked List.

    These are my classes:



    public class UnorderedLinked List extends LinkedListClass
    {

    public UnorderedLinked List()
    {
    super();
    }

    public UnorderedLinked List(UnorderedL inkedList otherList)
    {
    super(otherList );
    }

    //Method to determine whether searchItem is in
    //the list.
    //Postcondition: Returns true if searchItem is found
    // in the list; false otherwise.
    public boolean search(DataElem ent searchItem)
    {
    LinkedListNode current; //variable to traverse the list
    boolean found;

    current = first; //set current to point to the first
    //node in the list

    found = false; //set found to false

    while(current != null && !found) //search the list
    if(current.info .equals(searchI tem)) //item is found
    found = true;
    else
    current = current.link; //make current point to
    //the next node
    return found;
    }


    //Method to delete deleteItem from the list.
    //Postcondition: If found, the node containing
    // deleteItem is deleted from the
    // list. Also first points to the first
    // node, last points to the last
    // node of the updated list, and count
    // is decremented by 1.
    public void deleteNode(Data Element deleteItem)
    {
    LinkedListNode current; //variable to traverse the list
    LinkedListNode trailCurrent; //variable just before current
    boolean found;

    if(first == null) //Case 1; the list is empty
    System.err.prin tln("Cannot delete from an empty list.");
    else
    {
    if(first.info.e quals(deleteIte m)) //Case 2
    {
    first = first.link;

    if(first == null) //the list had only one node
    last = null;
    count--;
    }
    else //search the list for the node with the given info
    {
    found = false;
    trailCurrent = first; //set trailCurrent to point to
    //the first node
    current = first.link; //set current to point to the
    //second node

    while(current != null && !found)
    {
    if(current.info .equals(deleteI tem))
    found = true;
    else
    {
    trailCurrent = current;
    current = current.link;
    }
    }//end while

    if(found) //Case 3; if found, delete the node
    {
    count--;
    trailCurrent.li nk = current.link;

    if(last == current) //node to be deleted was
    //the last node
    last = trailCurrent; //update the value of last
    }
    else
    System.out.prin tln("Item to be deleted is "
    + "not in the list.");
    }//end else
    }//end else
    }//end deleteNode
    }




    public abstract class LinkedListClass
    {
    protected class LinkedListNode
    {
    DataElement info;
    LinkedListNode link;
    }

    protected LinkedListNode first; //variable to store the
    //address of the first
    //node of the list
    protected LinkedListNode last; //variable to store the
    //address of the last
    //node of the list
    protected int count; //variable to store the number of nodes
    //in the list


    //default constructor
    //Initializes the list to an empty state.
    //Postcondition: first = null, last = null,
    // count = 0
    public LinkedListClass ()
    {
    first = null;
    last = null;
    count = 0;
    }

    //Method to determine whether the list is empty.
    //Postcondition: Returns true if the list is empty;
    // false otherwise.
    public boolean isEmptyList()
    {
    return (first == null);
    }

    //Method to initialize the list to an empty state.
    //Postcondition: first = null, last = null,
    // count = 0
    public void initializeList( )
    {
    first = null;
    last = null;
    count = 0;
    }

    //Method to output the data contained in each node.
    public void print()
    {
    LinkedListNode current; //variable to traverse the list

    current = first; //set current so that it points to
    //the first node
    while(current != null) //while more data to print
    {
    System.out.prin t(current.info + " ");
    current = current.link;
    }
    }//end print

    //Method to return the number of nodes in the list.
    //Postcondition: The value of count is returned.
    public int length()
    {
    return count;
    }


    //Method to return a reference of the object containing
    //the data of the first node of the list.
    //Precondition: The list must exist and must not be empty.
    //Postcondition: The reference of the object that
    // contains the info of the first node
    // is returned.
    public DataElement front()
    {
    DataElement temp = first.info.getC opy();
    return temp;
    }

    //Method to return a reference of object containing
    //the data of the last node of the list.
    //Precondition: The list must exist and must not be empty.
    //Postcondition: The reference of the object that
    // contains the info of the last node
    // is returned.
    public DataElement back()
    {
    DataElement temp = last.info.getCo py();
    return temp;
    }

    //Method to determine whether searchItem is in the list.
    //Postcondition: Returns true if searchItem is found
    // in the list; false otherwise.
    public abstract boolean search(DataElem ent searchItem);


    //Method to insert newItem in the list.
    //Postcondition: first points to the new list
    // and newItem is inserted at the
    // beginning of the list. Also,
    // last points to the last node and
    // count is incremented by 1.
    public void insertFirst(Dat aElement newItem)
    {
    LinkedListNode newNode; //variable to create the
    //new node

    newNode = new LinkedListNode( ); //create the new node
    newNode.info = newItem.getCopy (); //assign a copy of
    //newItem to the node
    newNode.link = first; //insert newNode before first
    first = newNode; //make first point to the
    //actual first node

    if(last == null) //if the list was empty, newNode is
    //also the last node in the list
    last = newNode;

    count++;
    }

    //Method to insert newItem at the end of the list.
    //Postcondition: first points to the new list and
    // newItem is inserted at the end
    // of the list. Also, last points to
    // the last node and
    // count is incremented by 1.
    public void insertLast(Data Element newItem)
    {
    LinkedListNode newNode; //variable to create the new node

    newNode = new LinkedListNode( ); //create the new node
    newNode.info = newItem.getCopy (); //assign a copy of
    //newItem to the node
    newNode.link = null; //set the link field of
    //newNode to null

    if(first == null) //if the list is empty, newNode is
    //both the first and last node
    {
    first = newNode;
    last = newNode;
    }
    else //if the list is not empty, insert newNode after last
    {
    last.link = newNode; //insert newNode after last
    last = newNode; //set last to point to the actual last node
    }

    count++;
    }//end insertLast



    //Method to delete deleteItem from the list.
    //Postcondition: If found, the node containing
    // deleteItem is deleted from the
    // list. Also first points to the first
    // node, last points to the last
    // node of the updated list, and count
    // is decremented by 1.
    public abstract void deleteNode(Data Element deleteItem);

    //Method to return a reference of the copy of otherList.
    private void copy(LinkedList Class otherList)
    {
    LinkedListNode newNode; //variable to create a node
    LinkedListNode current; //variable to traverse the list

    first = null; //make this list empty

    if(otherList.fi rst == null) //otherList is empty
    {
    first = null;
    last = null;
    count = 0;
    }
    else
    {
    count = otherList.count ;
    current = otherList.first ; //current points to the
    //list to be copied

    //copy the first element
    first = new LinkedListNode( ); //create the node
    first.info = current.info.ge tCopy(); //copy the info
    first.link = null; //set the link field of
    //the node to null
    last = first; //make last point to the first node
    current = current.link; //make current point to the next
    //node of the list being copied

    //copy the remaining list
    while(current != null)
    {
    newNode = new LinkedListNode( );
    newNode.info = current.info.ge tCopy();
    newNode.link = null;
    last.link = newNode;
    last = newNode;
    current = current.link;
    }//end while
    }//end else
    }//end copy


    //Method to return a reference of the copy of otherList.
    public void copyList(Linked ListClass otherList)
    {
    if(this != otherList) //avoid self-copy
    copy(otherList) ;
    }

    //copy constructor
    public LinkedListClass (LinkedListClas s otherList)
    {
    copy(otherList) ;
    }//end copy constructor
    }



    import java.io.*;
    import java.util.*;

    public class TestProgUnorder edLinkedList
    {
    static BufferedReader keyboard = new
    BufferedReader( new InputStreamRead er(System.in));

    public static void main(String[] args) throws IOException
    {
    UnorderedLinked List list
    = new UnorderedLinked List();
    UnorderedLinked List tempList =
    new UnorderedLinked List();
    IntElement num = new IntElement();

    StringTokenizer tokenizer;

    int position;

    System.out.prin t(" Enter 8 integers on the " + "same line: ");
    System.out.flus h();

    tokenizer = new
    StringTokenizer (keyboard.readL ine());

    num.setNum(Inte ger.parseInt(to kenizer.nextTok en()));

    while (tokenizer.hasM oreTokens())
    {
    num.setNum(Inte ger.parseInt(to kenizer.nextTok en()));
    list.insertLast (num);
    }

    System.out.prin tln();
    System.out.prin t("The list you " + "entered is: ");
    list.print();
    System.out.prin tln();
    System.out.prin tln("Length of list: " + list.length());

    tempList.copyLi st(list);

    System.out.prin t("Enter the num to " + "be deleted: ");
    System.out.flus h();
    num.setNum(Inte ger.parseInt(ke yboard.readLine ()));
    System.out.prin tln();

    tempList.delete Node(num);
    System.out.prin tln("After deleting " + num + " the node, tempList:");
    tempList.print( );
    System.out.prin tln();
    System.out.prin tln("Length of list: " + tempList.length ());

    System.out.prin t("Enter the search " + "item: ");
    System.out.flus h();

    num.setNum(Inte ger.parseInt(ke yboard.readLine ()));
    System.out.prin tln();

    if(tempList.sea rch(num) != -1)
    System.out.prin tln("Item found in " + "the list");
    else
    System.out.prin tln("Item not found");

    System.out.prin t("The list temp: ");
    tempList.print( );
    System.out.prin tln();
    System.out.prin tln("Length of list: " + tempList.length ());
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You know with code tags, the code becomes more readable.
    In java LinkedLists the trick is the design of the node. Here is how I would do it:
    Code:
    class LinkedListNode {
        private Object data;
        private Listnode next;
    
        public LinkedListNode(Object d) {
    	this(d, null);
        }
    
        public LinkedListNode(Object d, LinkedListNode n) {
    	data = d;
    	next = n;
        }
    
        public Object getData() {
            return data;
        }
    
        public LinkedListNode getNext() {
            return next;
        }
    
    
        public void setData(Object ob) {
            data = ob;
        }
    
        public void setNext(LinkedListNode n) {
            next = n;
        }
    }
    public class TheList {
    	LinkedListNode head;
    	LinkedListNode tail;
    
    	public static void main(String[] args) {
    		TheList linkedList = new TheList();
    	}
    	public TheList() {
    		head = null;
    		tail = null;
    	}
    	public TheList(LinkedListNode head, LinkedListNode tail) {
    		this.head head;
    		this.tail = tail;
    	}
    
    	public void addLast(LinkedListNode node) {
    		if(head == null) {
    			head = node;
    		}
    		else if(tail == null) {
    			tail = node;
    		}
    		else {
    			tail.setNext(node);
    		}
    	}
    
    	public boolean contains(LinkedListNode node) {
    		boolean found = false;
    		LinkedListNode current = head;
    		if(current == null) {
    			found = false;
    		}
    		else {
    			while(current != null && !found ) {
    				if(current.getData().equals(node.getData())) {
    					found = true;
    				}
    				current = current.getNext();
    			}
    		}
    		return found;
    	}
    	
    	public void addAfter(LinkedListNode nodeInList, LinkedListNode nodeToAdd) {
    		if(!contains(nodeToAdd)) {
    			System.out.println("Failed to add..");
    		}
    		else {
    			LinkedListNode current = head;
    			boolean positionReached = false;
    			while(!positionReached) {
    				if(current.getData().equals(nodeInList)) {
    					positionReached = true;
    				}
    			}
    			LinkedListNode temp = current.getNext();
    			current.setNext(nodeToAdd);
    			nodeToAdd.setNext(temp);
    			
    		}
    	}
    	// the rest of the methods should now be easy
    
    }
    perhaps better approaches can be found here
    http://www.cs.jhu.edu/~pari/600.107/Horstmann/slides/Ch19/ch19.html
    http://www.javacommerc e.com/displaypage.jsp ?name=javadata2 .sql&id=18214#L inked_Lists

    Comment

    Working...