Linked lists, FIFO & LIFO


/** */

1. The principale class


import java.io.*;

class TheNext {

private int Value;
private TheNext Next;

public TheNext (int TheValue){
Value = TheValue;
}

public int getValue() {
return Value;
} 
public TheNext getNext() {
return Next;
}
public void setValue (int TheValue) {
Value = TheValue;
}
public void setNext (TheNext ThisNext){
Next = ThisNext;
}

}//End of the class TheNext

2. Linked LIFO order class


public class LifoList {
 
static void Display (TheNext ThisList){
if (ThisList != null){
System.out.println ("\n\tIn the LIFO order, the list contains :");
int rank  = 0;
while(ThisList !=null){
System.out.println ("\n\t" + ++rank + ")\t" + ThisList.getValue());
ThisList = ThisList.getNext();
}
System.out.println ("\n");
}
else
System.out.println ("The list is empty \n");
}


static int nbElements (TheNext TheList, int nbMax)
{
int n = 0;
while(TheList !=null){
	if (TheList.getValue() > 
	nbMax) n++;
	TheList = TheList.getNext();
	}
return n;
}


static void PrintThis (TheNext ThisList){
int nbLimit = 17;
Display (ThisList);
System.out.println ("\n\tThe number of elements  with the value 
greater than 7 is: " + nbElements(ThisList, nbLimit));
}


public static void main (String [] args) throws IOException {
TheNext ThisList = ReadNumbers();
PrintThis (ThisList);
}


static TheNext ReadNumbers()throws IOException {   
char TheResponse;
TheNext Head = null;
TheNext tempo = null;

int Value;
do {Value = ReadUtilities.ReadInteger("\n\tEnter an integer: --> ");
tempo = new TheNext(Value);
tempo.setNext(Head);
Head = tempo;
TheResponse = ReadUtilities.ReadCharacter ("\n\tDo you want to continue ? y/n: --> ");
TheResponse = (char) ( TheResponse + 'A' - 'a');
} while(TheResponse == 'Y');
return Head;
}


}//End of LifoList

3. Linked FIFO order class


import java.io.*;
import java.util.*;
import java.text.DecimalFormat;


 class TheNext {

private int Value;
private TheNext Next;

public TheNext (int TheValue){
Value = TheValue;
}

public int getValue() {
return Value;
} 
public TheNext getNext() {
return Next;
}
public void setValue (int TheValue) {
Value = TheValue;
}
public void setNext (TheNext ThisNext){
Next = ThisNext;
}

}//End of the class TheNext


 public class FifoList {
 
static void PrintThis (TheNext ThisList){
Display (ThisList);
}

static void Display (TheNext ThisList){
if (ThisList != null){
System.out.println ("\n\tThe list contains in the FIFO order:");
int rank  = 0;
while(ThisList !=null){
System.out.println ("\n\t"+ ++rank + ")\t" + ThisList.getValue());
ThisList = ThisList.getNext();
}
System.out.println ("\n");
}
else
System.out.println ("The list is empty \n");
}


static TheNext ReadTheFile (String TheFile)throws IOException {

DataInputStream ReadFile = new DataInputStream(new FileInputStream (TheFile));
int rank = 0;
int Value = 0;//Any value

TheNext TheHead = null; 
TheNext tempo = null;
TheNext TheCurrent = null;
boolean TheEndOfFile = false;

while (!TheEndOfFile)
{
try {
  Value = ReadFile.readInt();
  }
catch (EOFException e)
		{
		TheEndOfFile = true;
		if (TheHead !=null)
		TheCurrent.setNext(null);
		}	
				if (!TheEndOfFile) {
				tempo = new TheNext(Value);
				if (TheHead == null) 
				TheHead = tempo;
				else 
				TheCurrent.setNext(tempo);
			
				TheCurrent = tempo;
			
		}

	}
	ReadFile.close();
	return TheHead;
}

public static void main (String [] args) throws IOException {
TheNext ThisList = ReadTheFile ("C:/(Directories to a binary file/YourFile.dat");
PrintThis (ThisList);
}

}//End of FifoList



4. Binary Output File


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

/**  This program will write a primitive type data array to a binary file. The mainly Java two methods to use is 
   FileOutputStream OutPutFile = new FileOutputStream (InputFile);
   DataOutputStream data_out = new DataOutputStream (OutputFile);
This example gives the binary file related to the sum of the first itegers according to the formula: 
1 + 2 + 3 + 3 + ... + n = n (n+1)/2.
The out put file calledSumFirstNumbers.dat will be used to be read in the example of FIFO order.
	 
*/
public class BinaryOutputFile
{
  public static void main (String arg[]) {
// Create an integer array Table
    int    [] Table = new int[10];
// Fill in the Table
    for  (int i= 0; i < Table.length; i++) {
      Table[i] = i*(i + 1)/2;
    }

//First, create,  the file TheFile
    File Thefile = null;
// Get the output file name from the argument line.
    if (arg.length > 0) Thefile = new File (arg[0]);
    
    if (Thefile == null) {
	        Thefile = new File ("SumFirstNumbers.dat");
// or by using a default name for the file
        System.out.println ("\n\tDefault: SumFirstNumbers.dat");

    }

// Next,  write the data array to the file.
    try {
      // Create an output stream to the file.
      FileOutputStream OutPutFile = new FileOutputStream (Thefile);
      // Wrap the FileOutputStream with a DataOutputStream
      DataOutputStream OutPutData = new DataOutputStream (OutPutFile);

      // Write the data to the file 
      for (int i= 0; i < Table.length; i++) {
          OutPutData.writeInt (Table[i]);
      }
// Close the file..
      OutPutFile.close ();
    }
    catch (IOException e) {
       System.out.println ("IO exception = " + e );
    }
  } // main

} // class BinaryFile




5. Utilities for reading


import java.io.*;
public class ReadUtilities {

static String ReadString (String info)
	throws IOException{
	BufferedReader TheInput = new BufferedReader ( new InputStreamReader(System.in));
	System.out.print(info);
	return TheInput.readLine();					
	}
	
	
static char ReadCharacter(String info) throws IOException 
	{                       
	return ReadString(info).charAt(0);           
	}	
	
	
static double ReadDouble(String info) throws IOException {
	 Double z = new Double (ReadString(info));
	 return z.doubleValue();
	}
	
	
static int ReadInteger(String info) throws IOException {
	return Integer.parseInt(ReadString(info));  
	}
}