List & SortedList Program

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2784 Views - Last Post: 22 November 2008 - 05:56 PM Rate Topic: -----

#1 deven1974  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-October 08

List & SortedList Program

Post icon  Posted 20 November 2008 - 01:32 PM

Hello,
By the way thanks for the previous help, you're were really, I mean really helpfully. With no one every respond or feed back about my program. I thought that this community is to help people who has diffcult time with his/her program. Based upon individual type from entry-level to advanced. Based up on yesterday, I had no help or respond from this community. I guess no one care about my hard time with program, and matters to try understand about this program. If anyone can I really aprricated, be on this point I'll put the program one more try to someone help, then I'll try do my best with myself with program.

Thanks

The Sorted List ADT
Implement the SortedList class. The SortedList class extends
the List class. Both can be seen here. Your assignment is to
implement all of the abstract methods of the List class. They are:


insert
iterator
remove
retrieve
search
You must also implement an Iterator inner class for the
SortedList class. You must submit a modified SortedList.java
file with your source code. Do not submit and do not modify
the List.java file.


/*
*
* List.java
*
*/

public abstract class List<E> implements Iterable<E> {

protected class Node<T> {

protected Node(T data) {
this.data = data;
}

protected T data;
protected Node<T> next;
}

public abstract void insert(E data);
public abstract void remove(E data);
public abstract E retrieve(int index);
//public abstract boolean search(E data);

protected Node<E> head;
}


/*
*
* SortedList.java
*
*/

public class SortedList<E extends Comparable<? super E>> extends List<E> {
public void insert(E data){ 
if (E data != null)
Node<E> temp = new Node<E>(data);
temp.next = head;
head = temp;
}

   public E remove(E data) {
	 E.temp = null;
	 if (head != null)
	  temp = head.data;
	  head = head.next;
	  return temp;
}
public E retrieve(int index){
	if (index < null || index > E.data) 
	  throw new IndexofBoundException(Integers.toSTring(index));
		 return index;
	}
 public boolean search(E data){
	  if(E.data < 1 || E.data > size())
			return false;
		
		Node<T> = head;
		for(int i = 1; i < index; i++)
		{
			if(Node<T>.getNext() == null)
				return false;
			
			Node<T> = Node<T>.getNext();
		}
		Node<T>.setNext(Node<T>.getNext().getNext());
		E.data--;
		return true;
	}




	public static void main(String[] args) { 
	SortedList theSortedList = new SortedList();
	theSortedList.insert(20); 
	theSortedList.insert(40);

	theSortedList.retrieve(); 

	theSortedList.insert(10); 
	theSortedList.insert(30);
	theSortedList.insert(50);

	theSortedList.E.retrieve(); 

	theSortedList.remove(); 

	theSortedList.E.retrieve(); 
  } 


}


This post has been edited by deven1974: 20 November 2008 - 01:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: List & SortedList Program

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,346
  • Joined: 20-September 08

Re: List & SortedList Program

Posted 20 November 2008 - 01:50 PM

You need to work incrementally. You haven't, and as a result, have built up a massive number of errors. I suggest starting again
Was This Post Helpful? 0
  • +
  • -

#3 deven1974  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-October 08

Re: List & SortedList Program

Posted 20 November 2008 - 02:08 PM

Judging about the program, I only have 19 errors

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: List & SortedList Program

Posted 20 November 2008 - 05:41 PM

In your last topic about this I asked, and will ask again, have you ever written a non-sorted LinkedList? Based on the code you have given you have not.

Last time I gave you this link with a tutorial on linked list, which actually gives you the code for a linked list, if you read and understand how the linked list is operating, implementing a sortedLinkedList like you have to will not be to much harder, since you only need the 5 methods you have specified.
Was This Post Helpful? 0
  • +
  • -

#5 deven1974  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-October 08

Re: List & SortedList Program

Posted 20 November 2008 - 06:00 PM

Hello BigAnt,
Could able help get started at least with first one with insert? Because after reading numerous times the article you mention. I'm still little confused.

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: List & SortedList Program

Posted 20 November 2008 - 07:16 PM

Ok, here is the code for the (unsorted) LinkedList insert, just follow the comments to implement the sorted list.

import java.util.Iterator;


/*
*
* SortedList.java
*
*/

public class SortedList<E> extends List<E> {

	/** size of the List */
	private int size;
	
	public SortedList(){
		head = null;
		size = 0;
	}
	
	public void insert(E data){
		if(head == null){
			head = new Node<E>(data);
			size++;
		} else {
			Node<E> n = head;
			while(n.next != null){
				//Now just edit here to check where the element should go in a sorted list
				//When you find the place, if it isn't at the end,break and just change next reference on previous node
				//to the new Node and the next reference on the new Node to the next reference that was on the previous 
				//node previously
				n = n.next;
			}
			n.next = new Node<E>(data);
			size++;
		}
	}

   public void remove(E data) {
	   /**
	    * Do this by looping through the LinkedList and checking each element, if you come across
	    * the correct element you would remove it by assigning the Node before it's next to the node 
	    * after the node being removed.
	    */
   }
   
   public E retrieve(int index){
	   /**
	    * Check if index<size, if it is Loop through List up until index and 
	    * return that element 
	    */
         return null;
   }
   
   public boolean search(E data){
	   /**
	    * Loop through entire list, if element is found return true, if not return false
	    */
        return true;
   }

	@Override
	public Iterator<E> iterator() {
		// TODO Auto-generated method stub
		/**
		 * Must create an inner class implementing [url="http://java.sun.com/javase/6/docs/api/index.html?java/util/Iterator.html"]http://java.sun.com/javase/6/docs/api/inde...l/Iterator.html[/url]
		 * and return an instance of this class
		 */
		return null;
	}


}


Was This Post Helpful? 0
  • +
  • -

#7 deven1974  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-October 08

Re: List & SortedList Program

Posted 21 November 2008 - 01:04 PM

Hello,
Can anybody help me with this program, I'm get some errors on this program.

Thanks

import java.util.Iterator;  
  
   
 /* 
 * 
 * SortedList.java 
 * 
 */  
   
public class SortedList<E extends Comparable<? super E>> extends List<E> {

 
	 
	 private int size;  
	   
	public SortedList(){  
		 head = null;  
		 size = 0;  
	 }  
	   
	 public void insert(E data){  
		 if(head == null){  
			  head = new Node<E>(data);  
			  size++;  
		   } else {  
			 Node<E> n = head;  
			  while(n.next != null){  
				  
			  }  
			  n.next = new Node<E>(data);  
			  size++;  
		  }  
	   }
	
	   public void remove(E data) {  
	  E.data = null;
	 if (head != null)
	  temp = head.data;
	  head = head.next;
	  return temp;
	 }  
	   
	 public E retrieve(int index){  
		 if (index < size)  
		  return size;
		  else
 
		   return null;  
	 }  
	   
	 public boolean search(E data){  
		 if(E.data < 1 || E.data > size())
			return false;
		
		Node<T> = head;
		for(int i = 1; i < index; i++)
		{
			if(Node<T>.getNext() == null)
				return false;
			
			Node<T> = Node<T>.getNext();
		}
		Node<T>.setNext(Node<T>.getNext().getNext());
		E.data--;
		return true;
   
		   
	 }  
	
	  public Iterator<E> iterator() {  
	   if (index < null || index > E.data)
			throw new IndexofBoundException(Integers.toSTring(index));
			return index;
	   else
			return null;  
	  }  
	
 }

Was This Post Helpful? 0
  • +
  • -

#8 deven1974  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-October 08

Re: List & SortedList Program

Posted 21 November 2008 - 02:08 PM

Hello Big Ant,
Hey thanks for the program, but I still little confused on writing in the comments to make it sortlist program. Could you able help me with this program?

Thanks
Was This Post Helpful? 0
  • +
  • -

#9 deven1974  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-October 08

Re: List & SortedList Program

Posted 22 November 2008 - 01:21 PM

Okay, I'm get 3 error in this message. If someone can help I really appricated.
/*
*
* List.java
*
*/

public abstract class List<E> implements Iterable<E> {

protected class Node<T> {

protected Node(T data) {
this.data = data;
}

protected T data;
protected Node<T> next;
}

public abstract void insert(E data);
//public abstract void remove(E data);
//public abstract E retrieve(int index);
//public abstract boolean search(E data);

protected Node<E> head;





/*
*
* SortedList.java
*
*/

public class SortedList<E extends Comparable<? super E>> extends List<E> 
{
	 private int size;
		public SortedList()  {
		 head = null;  
		 size = 0;  
	   
	}   
	 public void insert(E data)
	  {  
		 if(head == null){  
			 head = new Node<E>(data);  
			 size++;  
		  elseif   
			 Node<E>n = head;  
			 while(n.next != null)
			  {  
				n = n.next;  
			  }  
			 n.next = new Node<E>(data);  
			 size++;  
		 }  
	 } 
}



Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: List & SortedList Program

Posted 22 November 2008 - 01:35 PM

Aagrawal84 seems to have the same school assignment
He is posting the same questions just a few topics from yours

I suggest you combine your efforts in the same topics... let say this one as it is more completed
Was This Post Helpful? 0
  • +
  • -

#11 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: List & SortedList Program

Posted 22 November 2008 - 01:39 PM

Quote

Okay, I'm get 3 error in this message. If someone can help I really appricated.


SortedList never implements the iterator() method, there should be no if after the else, and there should be a } before the else and a { after the else statement.

Have you tried to get the insert method to work in a sorted manner, as the code you posted is the same as I gave just with some errors.
Was This Post Helpful? 0
  • +
  • -

#12 deven1974  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-October 08

Re: List & SortedList Program

Posted 22 November 2008 - 01:40 PM

View Postpbl, on 22 Nov, 2008 - 12:35 PM, said:

Aagrawal84 seems to have the same school assignment
He is posting the same questions just a few topics from yours

I suggest you combine your efforts in the same topics... let say this one as it is more completed




Hello,
Yeah, but we can't share the program with each other, that I'm starting with first insert method then work other method. But I getting this error message. If someone can help, I really appricated.

Thanks
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,346
  • Joined: 20-September 08

Re: List & SortedList Program

Posted 22 November 2008 - 01:46 PM

Try something like
    class SortedList<E extends Comparable<?super E>> extends List<E> {
	private int size;

	public SortedList() {
	    head = null;
	    size = 0;
	}

	public java.util.Iterator<E> iterator() {
	    return null;
	}

	public void insert(E data) {
	    if (head == null) {
		head = new Node<E>(data);
		size++;
	    } else {
		Node<E> n = head;

		while (n.next != null) {
		    n = n.next;
		}

		n.next = new Node<E>(data);
		size++;
	    }
	}
    }


Was This Post Helpful? 0
  • +
  • -

#14 deven1974  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-October 08

Re: List & SortedList Program

Posted 22 November 2008 - 01:51 PM

Cool, thanks that work, but the old error I'm getting is:


C:\Documents and Settings\Deven\Desktop>javac SortedList.java List.java
SortedList.java:7: SortedList is not abstract and does not override abstract met
hod search(E) in List
public class SortedList<E extends Comparable<? super E>> extends List<E>
	   ^
1 error

C:\Documents and Settings\Deven\Desktop>


Was This Post Helpful? 0
  • +
  • -

#15 deven1974  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-October 08

Re: List & SortedList Program

Posted 22 November 2008 - 02:08 PM

Okay, I have error in the remove method


C:\Documents and Settings\Deven\Desktop>javac SortedList.java List.java
SortedList.java:31: data is already defined in remove(E)
		 Node <E>data = null;
				 ^
SortedList.java:33: cannot find symbol
symbol  : variable temp
location: class SortedList<E>
		 temp = head.data;
		 ^
SortedList.java:35: cannot return a value from method whose result type is void
		 return temp;
				^
3 errors

C:\Documents and Settings\Deven\Desktop>





class SortedList<E extends Comparable<?super E>> extends List<E> {
	private int size;

	public SortedList() {
		head = null;
		size = 0;
			
	}

	public java.util.Iterator<E> iterator() {
		return null;
	}

	public void insert(E data) {
		if (head == null) {
		head = new Node<E>(data);
		size++;
		} else {
		Node<E> n = head;

		while (n.next != null) {
			n = n.next;
		}

		n.next = new Node<E>(data);
		size++;
		}
	}
	
		 public void remove(E data) { 
		 <E>data = null;
		 if (head != null)
		 temp = head.data;
		 head = head.next;
		 return temp;
  
   }
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2