hi, I'm working on an exercise, i did a lot of work already and i just can't figure where I'm going wrong, this is what I'm trying to achieve
Sample IO
*************** *************** *************** *************** *************** ****
*************** *************** *************** *************** ***
and this is my code so far, that won't work....
*************** *************** *************** *************** *************** *************** *
[code=java]import java.util.NoSuc hElementExcepti on;
import java.util.Scann er;
public class LinkedList2
{ private class Node
{
private String name;
private String stuName;
private Node link;
public Node()
{
link = null;
name = null;
stuName = null;
}
public Node(String newStuName, String newName, Node linkValue)
{
setData(newStuN ame, newName);
link = linkValue;
}
public void setData(String newStuName, String newName)
{
name = newName;
stuName = newStuName;
}
public void setLink(Node newLink)
{
link = newLink;
}
public String getStuName()
{
return stuName;
}
public String getName()
{
return name;
}
public Node getLink()
{
return link;
}
}//end of node inner class
public class List2Iterator//inner class for iterators for linkedlist2
{
private Node position;
private Node previous;
public List2Iterator()
{
position = head;
previous = null;
}
public void restart()
{
position = head;
previous = null;
}
public String next()
{
if(!hasNext())
throw new NoSuchElementEx ception();
String toReturn = position.stuNam e;
previous = position;
position = position.link;
return toReturn;
}
public boolean hasNext()
{
return(position != null);
}
public String peek()
{
if(!hasNext())
throw new IllegalStateExc eption();
return position.stuNam e;
}
public void addHare(String newData)
{
if(position == null && previous != null)//if at end o list
previous.link = new Node(newData, null);
else if(position == null || previous == null)
LinkedList2.thi s.addToStart(ne wData);
else{
Node temp = new Node(newData, position);
previous.link = temp;
previous = temp;
}
}
public void changeHare(Stri ng newData)
{
if(position == null)
throw new IllegalStateExc eption();
else
position.stuNam e = newData;
}
public void delete()
{
if(position == null)
throw new IllegalStateExc eption();
else if(previous == null)
{
head = head.link;
position = head;
}
else
{
previous.link = position.link;
position = position.link;
}
}
}
private Node head;
public List2Iterator iterator()
{
return new List2Iterator() ;
}
public LinkedList2()
{
head = null;//adds node at the stat of the list with the specified data
}
public void addToStart(Stri ng itemStuName, String itemName)
{
head = new Node1(itemStuNa me, itemName, head);
}
}
public boolean deleteHeadNode( )
{
if(head != null){
head = head.link;
return true;
}
else
return false;
}
/*public int size()//return number of nodes in the list
{
int count = 0;
Node position = head;
while(position != null)
{
count++;
position = position.link;
}
return count;
}*/
public boolean contains(String stuName, String name)
{
return (find(stuName, name) != null);
}
private Node find(String target)
{
Node position = head;
String itemAtPosition;
while (position != null)
{
itemAtPosition = position.stuNam e;
if(itemAtPositi on.equals(targe t))
return position;
position = position.link;
}
return null; //target wan not found
}
public void outPutList()
{
Node position = head;
while(position != null);
{
System.out.prin tln(position.st uName);
position = position.link;
}
}
public boolean isEmpty()
{
return(head == null);
}
public void clear()
{
head = null;
}
public boolean equals(Object otherObject)
{
if(otherObject == null)
return false;
else if(getClass() != otherObject.get Class())
return false;
else{
LinkedList2 otherList = (LinkedList2)ot herObject;
if(size() != otherList.size( ))
return false;
Node position = head;
Node otherPosition = otherList.head;
while(position != null)
{
if((!(position. item.equals(oth erPosition.stuN ame))))
return false;
position = position.link;
otherPosition = otherPosition.l ink;
}
return true;//a mis match was not found
}
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System. in);
LinkedList2 list = new LinkedList2();
LinkedList2.Lis t2Iterator i = list.iterator() ;
System.out.prin tln("Welcome to PeopleSoft 2");
int choice = keyboard.nextIn t();
while(choice !== 5)
{
System.out.prin tln("MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it");
switch(choice)
{
case 1:
System.out.prin tln("Enter the student number");
stuNum = keyboard.next() ;
System.out.prin tln("Enter the Student name");
name = keyboard.next() ;
list.addToStart (studentNum,nam e);
System.out.prin tln("Done");
break;
case 2:
System.out.prin tln("Enter student number");
stuName = keyboard.next() ;
i.restart();
i.next();
System.out.prin tln("Will delete the node for; " + i.peek());
i.delete();
System.out.prin tln("Done");
break;
case 3:
System.out.prin tln("list now contains:");
i.restart();
while(i.hasNext ())
System.out.prin tln("Student: " + i.next());
System.out.prin tln();
System.out.prin tln("Done");
break;
case 5:
System.exit(0);
break;
default:
System.out.prin tln("Error,plea se check your input again");
break;
}
}
}
}[/code]
*************** *************** *************** *************** *************** *************** **
please help me see where I'm going wrong.
Sample IO
*************** *************** *************** *************** *************** ****
Code:
Welcome to PeopleSoft 2 MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it a Enter the student number MSXMIC001 Enter the name Mickey Mouse Done MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it a Enter the student number CHPALV001 Enter the name Alvin the Chipmunk Done MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it a Enter the student number DCKDON002 Enter the name Donald Duck Done MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it l Student: DCKDON002, Donald Duck Student: CHPALV001, Alvin the Chipmunk Student: MSXMIC001, Mickey Mouse Done MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it s Done MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it l Student: CHPALV001, Alvin the Chipmunk Student: DCKDON002, Donald Duck Student: MSXMIC001, Mickey Mouse Done MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it d Enter the student number DCKDON002 Done MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it l Student: CHPALV001, Alvin the Chipmunk Student: MSXMIC001, Mickey Mouse Done MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it x
and this is my code so far, that won't work....
*************** *************** *************** *************** *************** *************** *
[code=java]import java.util.NoSuc hElementExcepti on;
import java.util.Scann er;
public class LinkedList2
{ private class Node
{
private String name;
private String stuName;
private Node link;
public Node()
{
link = null;
name = null;
stuName = null;
}
public Node(String newStuName, String newName, Node linkValue)
{
setData(newStuN ame, newName);
link = linkValue;
}
public void setData(String newStuName, String newName)
{
name = newName;
stuName = newStuName;
}
public void setLink(Node newLink)
{
link = newLink;
}
public String getStuName()
{
return stuName;
}
public String getName()
{
return name;
}
public Node getLink()
{
return link;
}
}//end of node inner class
public class List2Iterator//inner class for iterators for linkedlist2
{
private Node position;
private Node previous;
public List2Iterator()
{
position = head;
previous = null;
}
public void restart()
{
position = head;
previous = null;
}
public String next()
{
if(!hasNext())
throw new NoSuchElementEx ception();
String toReturn = position.stuNam e;
previous = position;
position = position.link;
return toReturn;
}
public boolean hasNext()
{
return(position != null);
}
public String peek()
{
if(!hasNext())
throw new IllegalStateExc eption();
return position.stuNam e;
}
public void addHare(String newData)
{
if(position == null && previous != null)//if at end o list
previous.link = new Node(newData, null);
else if(position == null || previous == null)
LinkedList2.thi s.addToStart(ne wData);
else{
Node temp = new Node(newData, position);
previous.link = temp;
previous = temp;
}
}
public void changeHare(Stri ng newData)
{
if(position == null)
throw new IllegalStateExc eption();
else
position.stuNam e = newData;
}
public void delete()
{
if(position == null)
throw new IllegalStateExc eption();
else if(previous == null)
{
head = head.link;
position = head;
}
else
{
previous.link = position.link;
position = position.link;
}
}
}
private Node head;
public List2Iterator iterator()
{
return new List2Iterator() ;
}
public LinkedList2()
{
head = null;//adds node at the stat of the list with the specified data
}
public void addToStart(Stri ng itemStuName, String itemName)
{
head = new Node1(itemStuNa me, itemName, head);
}
}
public boolean deleteHeadNode( )
{
if(head != null){
head = head.link;
return true;
}
else
return false;
}
/*public int size()//return number of nodes in the list
{
int count = 0;
Node position = head;
while(position != null)
{
count++;
position = position.link;
}
return count;
}*/
public boolean contains(String stuName, String name)
{
return (find(stuName, name) != null);
}
private Node find(String target)
{
Node position = head;
String itemAtPosition;
while (position != null)
{
itemAtPosition = position.stuNam e;
if(itemAtPositi on.equals(targe t))
return position;
position = position.link;
}
return null; //target wan not found
}
public void outPutList()
{
Node position = head;
while(position != null);
{
System.out.prin tln(position.st uName);
position = position.link;
}
}
public boolean isEmpty()
{
return(head == null);
}
public void clear()
{
head = null;
}
public boolean equals(Object otherObject)
{
if(otherObject == null)
return false;
else if(getClass() != otherObject.get Class())
return false;
else{
LinkedList2 otherList = (LinkedList2)ot herObject;
if(size() != otherList.size( ))
return false;
Node position = head;
Node otherPosition = otherList.head;
while(position != null)
{
if((!(position. item.equals(oth erPosition.stuN ame))))
return false;
position = position.link;
otherPosition = otherPosition.l ink;
}
return true;//a mis match was not found
}
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System. in);
LinkedList2 list = new LinkedList2();
LinkedList2.Lis t2Iterator i = list.iterator() ;
System.out.prin tln("Welcome to PeopleSoft 2");
int choice = keyboard.nextIn t();
while(choice !== 5)
{
System.out.prin tln("MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it");
switch(choice)
{
case 1:
System.out.prin tln("Enter the student number");
stuNum = keyboard.next() ;
System.out.prin tln("Enter the Student name");
name = keyboard.next() ;
list.addToStart (studentNum,nam e);
System.out.prin tln("Done");
break;
case 2:
System.out.prin tln("Enter student number");
stuName = keyboard.next() ;
i.restart();
i.next();
System.out.prin tln("Will delete the node for; " + i.peek());
i.delete();
System.out.prin tln("Done");
break;
case 3:
System.out.prin tln("list now contains:");
i.restart();
while(i.hasNext ())
System.out.prin tln("Student: " + i.next());
System.out.prin tln();
System.out.prin tln("Done");
break;
case 5:
System.exit(0);
break;
default:
System.out.prin tln("Error,plea se check your input again");
break;
}
}
}
}[/code]
*************** *************** *************** *************** *************** *************** **
please help me see where I'm going wrong.
Comment