// CSE 143, Winter 2010, Marty Stepp // // This program demonstrates the use of stacks and queues. // It reads in a file of students' exam scores in reverse order, such as: // // Yeilding Janet 87 // White Steven 84 // Todd Kim 52 // // And first strips the ones who got 100 on the exam using a queue, // then prints the rest in reverse order using a stack. import java.io.*; import java.util.*; public class GradeExams { public static void main(String[] args) throws FileNotFoundException { Stack students = new Stack(); Scanner input = new Scanner(new File("exams.txt")); while (input.hasNextLine()) { String line = input.nextLine(); // "Todd Kim 52" students.push(line); } // print everybody Queue backup = new LinkedList(); while (!students.isEmpty()) { String line = students.pop(); backup.add(line); System.out.println(line); } // filter out people who got 100 // Q --> S while (!backup.isEmpty()) { String line = backup.remove(); if (!line.endsWith("100")) { students.push(line); } } // print all people who didn't get 100, in reverse ABC order while (!students.isEmpty()) { String line = students.pop(); backup.add(line); System.out.println(line); } } }