EXAMPLE (STACK)
Example of Stack with Object Student
CLASS STUDENT
public class Student
{ ..... }
CLASS NODE
public class Node
{ .... }
CLASS LINKEDLIST
public class LinkedList
{ .... }
CLASS STACK
public class Stack extends LinkedList
{
public Stack() { } // constructor
public void push (Object elem)
{ insertAtFront(elem); }
public Object pop ( )
{ return removeFromFront(); }
public Object peek()
{ return getFirst(); }
} // end Stack
CLASS APPLICATION
import [Link];
public class StackApp2
{
public static void main(String [] args)
{
Stack theStack = new Stack(); // original stack
Stack tempStack = new Stack(); // temporary stack
for (int i=0; i<5; i++) // to input 5 students into the list
{ String sIdStd = [Link]("Enter student id");
String nameStd = [Link]("Enter name");
String sPart = [Link]("Enter part");
String sCgpa = [Link]("Enter cgpa");
int iIdStd = [Link](sIdStd);
int iPart = [Link](sPart);
double dCgpa = [Link](sCgpa);
Student stud = new Student(iIdStd, nameStd, iPart, dCgpa);
[Link](stud); } //insert data
// to display all the students in the stack
Object data;
Student S;
while (![Link]())
{
data = [Link](); //delete first
S = (Student) data; //casting
[Link]([Link]()); //display
[Link](S); // put into temporary stack
}
// restore; transfer all data from temporary stack to original stack
while (![Link]())
{
[Link]([Link]());
}
// to demonstrate some possible operation on data in the stack
double max = -99999.99, min = 9999.99;
int part4 = 0, dList = 0, prob = 0;
Student bestStudent = null;
Student weakStudent = null;
while (![Link]())
{
data = [Link](); // pop from origional stack
S = (Student) data;
if([Link]()>max) // find maximum cgpa
{max = [Link]();
bestStudent = S;}
if ([Link]() < min) // find minimum cgpa
{ min = [Link]();
weakStudent = S;}
if ([Link]() > 3.5) // count dean's list student
dList++;
if ([Link]() == 4) // count part 4 student
part4++;
if ([Link]() < 1.8) // count probation student
prob++;
[Link](S); // store to temporary stack
}
// display result
[Link]("The highest cgpa = " + max);
[Link]("The lowest cgpa = " + min);
[Link]("The number of dean's list student = " + dList);
[Link]("The number of part 4 student = " + part4);
[Link]("The number of probation student = " + prob);
[Link]("BEST STUDENT:");
[Link]([Link]());
// restore; transfer all data from temporary stack to original stack
while (![Link]())
{
[Link]([Link]());
}
} // main
} // StackApp