ed81
12-09-2007, 01:34 AM
Hi all, It has been a long time since I came to the site .. I am taking my last java class at school and i have a final project. As always I would like to thank you for all your help to us the "Java" learners for your cooperation in our path to succees. In this ocassion I have a problem with my last project, They want me to create two lists (DONE), to delete one list(DONE), to re-create the first list(DONE) and then put LIST2 into LIST1 right in the middle..(STUCK HERE), I have tried everything I could possibly think of and i cant get it.. This is what I have so far,, I would appreciate if you could help me with some hints , because its not working the way i want.. did i put something wrong.. (I bet i did) ... Thanks a million once again.. and best regards to Khali :)
public class Link {
public long dData; // data item
public Link next; // next link in list
public Link previous;// previous link in list
// -------------------------------------------------------------
public Link(long d) // constructor
{ dData = d; }
// -------------------------------------------------------------
public void displayLink() // display this link
{ System.out.print(dData + " "); }
// -------------------------------------------------------------
} // end class Link
////////////////////////////////////////////////////////////// //
class FirstLastList
{
private Link first; // ref to first link
private Link last;
private Link middle;
private int listlength;
// -------------------------------------------------------------
public FirstLastList() // constructor
{
first = null; // no links on list yet
last = null;
middle = null;
}
// -------------------------------------------------------------
public boolean isEmpty() // true if no links
{ return first==null; }
// -------------------------------------------------------------
public void insertFirst(long dd) // insert at front of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list,
last = newLink; // newLink <-- last
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
// -------------------------------------------------------------
public void insertLast(long dd) // insert at end of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list,
first = newLink; // first --> newLink
else
last.next = newLink; // old last --> newLink
last = newLink; // newLink <-- last
}
// -------------------------------------------------------------
public long deleteFirst() // delete first link
{ // (assumes non-empty list)
long temp = first.dData;
if(first.next == null) // if only one item
last = null; // null <-- last
first = first.next; // first --> old next
return temp;
}
// -------------------------------------------------------------
public void displayList()
{
System.out.print("List (first-->last): ");
Link current = first; // start at beginning
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}
//---------------------------------------------------------------
public void listLength(){
Link current = first;
int num =0;
while(current != null)
{
current.displayLink();
current = current.next;
num++;
}
System.out.println("The count is"+" "+num);
//displayList();
}
public void deleteList(){
Link current = first;
while(current != null)
{
first = current.next;
current = first;
}
}
private Link getLeftOfMiddle() {
Link left = first;
for ( int i = 0; i <listlength/2 ; i++ ) {
left = left.next;
}
return left;
}
public void insertIntoMiddle(FirstLastList newList) {
Link leftOfMiddle = getLeftOfMiddle();
Link rightOfMiddle = leftOfMiddle.next;
leftOfMiddle.next = newList.first;
newList.first = leftOfMiddle;
rightOfMiddle.previous =newList.last;
newList.last = rightOfMiddle;
}
//-------------------------------------------------------------
} // end class FirstLastList
////////////////////////////////////////////////////////////// //
//THIS IS THE MAIN..
//import java.util.Scanner;
public class FirstLastApp {
public static void main(String[] args)
{ // make a new list
FirstLastList List1 = new FirstLastList();
FirstLastList List2 = new FirstLastList();
FirstLastList List1a = new FirstLastList();
//Scanner input = new Scanner(System.in);
List2.insertFirst(100);
List2.insertFirst(200);
List2.insertFirst(300);
List1.insertFirst(10); // insert at front
List1.insertFirst(20);
List1.insertFirst(30);
List1.insertFirst(40);
List1.insertFirst(50);
List1.insertFirst(60);
System.out.println("THE FIRST LIST");
List1.displayList();
List1.listLength();
List1.deleteList();
System.out.println("Deleting..");
System.out.println("");
System.out.println("this is the FIRST list after deletion:");
List1.displayList();
System.out.println("");
System.out.println("THE SECOND LIST");
List2.displayList();
List2.listLength();
System.out.println("");
System.out.println("Reconstructing List1 Elements:...");
System.out.println("");
List1a.insertFirst(10); // insert at front
List1a.insertFirst(20);
List1a.insertFirst(30);
List1a.insertFirst(40);
List1a.insertFirst(50);
List1a.insertFirst(60);
List1a.displayList();
List1a.listLength();
System.out.println("");
System.out.println("MERGING ITEMS FROM LIST 2 INTO LIST 1 ELEMENTS..");
List1a.insertIntoMiddle(List2);
List1a.displayList();
} // end main()
} // end class FirstLastApp
public class Link {
public long dData; // data item
public Link next; // next link in list
public Link previous;// previous link in list
// -------------------------------------------------------------
public Link(long d) // constructor
{ dData = d; }
// -------------------------------------------------------------
public void displayLink() // display this link
{ System.out.print(dData + " "); }
// -------------------------------------------------------------
} // end class Link
////////////////////////////////////////////////////////////// //
class FirstLastList
{
private Link first; // ref to first link
private Link last;
private Link middle;
private int listlength;
// -------------------------------------------------------------
public FirstLastList() // constructor
{
first = null; // no links on list yet
last = null;
middle = null;
}
// -------------------------------------------------------------
public boolean isEmpty() // true if no links
{ return first==null; }
// -------------------------------------------------------------
public void insertFirst(long dd) // insert at front of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list,
last = newLink; // newLink <-- last
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
// -------------------------------------------------------------
public void insertLast(long dd) // insert at end of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list,
first = newLink; // first --> newLink
else
last.next = newLink; // old last --> newLink
last = newLink; // newLink <-- last
}
// -------------------------------------------------------------
public long deleteFirst() // delete first link
{ // (assumes non-empty list)
long temp = first.dData;
if(first.next == null) // if only one item
last = null; // null <-- last
first = first.next; // first --> old next
return temp;
}
// -------------------------------------------------------------
public void displayList()
{
System.out.print("List (first-->last): ");
Link current = first; // start at beginning
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}
//---------------------------------------------------------------
public void listLength(){
Link current = first;
int num =0;
while(current != null)
{
current.displayLink();
current = current.next;
num++;
}
System.out.println("The count is"+" "+num);
//displayList();
}
public void deleteList(){
Link current = first;
while(current != null)
{
first = current.next;
current = first;
}
}
private Link getLeftOfMiddle() {
Link left = first;
for ( int i = 0; i <listlength/2 ; i++ ) {
left = left.next;
}
return left;
}
public void insertIntoMiddle(FirstLastList newList) {
Link leftOfMiddle = getLeftOfMiddle();
Link rightOfMiddle = leftOfMiddle.next;
leftOfMiddle.next = newList.first;
newList.first = leftOfMiddle;
rightOfMiddle.previous =newList.last;
newList.last = rightOfMiddle;
}
//-------------------------------------------------------------
} // end class FirstLastList
////////////////////////////////////////////////////////////// //
//THIS IS THE MAIN..
//import java.util.Scanner;
public class FirstLastApp {
public static void main(String[] args)
{ // make a new list
FirstLastList List1 = new FirstLastList();
FirstLastList List2 = new FirstLastList();
FirstLastList List1a = new FirstLastList();
//Scanner input = new Scanner(System.in);
List2.insertFirst(100);
List2.insertFirst(200);
List2.insertFirst(300);
List1.insertFirst(10); // insert at front
List1.insertFirst(20);
List1.insertFirst(30);
List1.insertFirst(40);
List1.insertFirst(50);
List1.insertFirst(60);
System.out.println("THE FIRST LIST");
List1.displayList();
List1.listLength();
List1.deleteList();
System.out.println("Deleting..");
System.out.println("");
System.out.println("this is the FIRST list after deletion:");
List1.displayList();
System.out.println("");
System.out.println("THE SECOND LIST");
List2.displayList();
List2.listLength();
System.out.println("");
System.out.println("Reconstructing List1 Elements:...");
System.out.println("");
List1a.insertFirst(10); // insert at front
List1a.insertFirst(20);
List1a.insertFirst(30);
List1a.insertFirst(40);
List1a.insertFirst(50);
List1a.insertFirst(60);
List1a.displayList();
List1a.listLength();
System.out.println("");
System.out.println("MERGING ITEMS FROM LIST 2 INTO LIST 1 ELEMENTS..");
List1a.insertIntoMiddle(List2);
List1a.displayList();
} // end main()
} // end class FirstLastApp