List.java
import java.io.*;
public class List {
private InfoNode front;
private InfoNode back;
public List() {
front = new InfoNode(777, null);
back = new InfoNode(888,null);
}
public static void main(String[] arg) throws Exception{
List list = new List();
/* I will add 5 nodes in the front and then 6 nodes in the back
using a loop and an array. You may try to do this accepting
input from the user to practice. You may attempt to remove nodes
in either end as well. Make sure that you are consistent in the ends
that you select as front and back. Maybe what I see as front may be
what in your standards is back, please check that out and change
method names at your convenience. Draw your linked list so you can
understand it better.
*/
// adding the first node. This is most of the time a special process
InfoNode firstNode = new InfoNode(0, null); //created a new InfoNode
list.front.setNext(firstNode); //the new InfoNode is the front InfoNode
list.back.setNext(firstNode); //the new InfoNode is also the backNode
/* notice that I arbitrarily put a 0 as information, in your queue program you must ask
the user for the content and the content will be the whatever you get from the TextField
0 is the node that I add first. From there I add 100 in front of it
100, 0
after that I add 101
101, 100, 0
and so on 'till 104, so the list looks like
104, 103, 102, 101, 100, 0
After this I start adding in the back. First number added in the back is 200
so the list looks like this:
104, 103, 102, 101, 100, 0, 200
I add in the back also the numbers from 201 to 205 in that order so the list
looks as this
104, 103, 102, 101, 100, 0, 200, 201, 202, 203, 204, 205
*/
int i = 0;
for(i = 100; i<105; i++){
list.addNodeInFront(i);
}
for(i = 200; i<206; i++){
list.addNodeInBack(i);
}
list.printList();
System.in.read();
} //main method finishes.
public void addNodeInFront(int info){
// adds a node to the front side of the list
InfoNode in = new InfoNode(info, front.getNext());
this.setFront(in);
}
public InfoNode getFront(){
//returns the node in the front of the list
return this.front;
}
public void setFront(InfoNode node){
// sets a node to be the front of the list
front.setNext(node);
}
public void addNodeInBack(int info){
//adds a node in the back of the list
InfoNode in = new InfoNode(info, null);
back.getNext().setNext(in); // this is the part that keeps front and back as one single linked list
this.setBack(in);
}
public InfoNode getBack(){
// returns the node in the back of the list
return this.back;
}
public void setBack(InfoNode node){
// sets a node to be the back of the list
back.setNext(node);
}
/*
the following is an extra method that can be useful for you to print to the TextArea
i'm using it to print the linked list to the screen
*/
public void printList(){
/*
this method starts printing from the front to the back.
I will use cursor to traverse the list in order starting from the Front
For this matter I make an InfoNode cursor that will point to the same
place that front is pointing to.
*/
InfoNode cursor = new InfoNode(0, front.getNext());
while(cursor.getNext()!= null){
cursor = cursor.getNext();
System.out.println("---> " + cursor.getIngformation());
}
}
} // closes class List
InfoNode.java
public class InfoNode {
private int information = 0;
private InfoNode next = null;
public InfoNode(int information, InfoNode next) {
this.information = information;
this.next = next;
}
public void setInformation (int newInformation){
this.information = newInformation;
}
public void setNext(InfoNode nextNode){
this.next = nextNode;
}
public int getIngformation(){
return this.information;
}
public InfoNode getNext(){
return this.next;
}
}