// CSE 143, Summer 2012 // // Code written in lecture to demonstrate the use of stacks and queues. // Reads in a file of students' exam scores in reverse order. The file // is in the format: // // Yeilding Janet 87 // White Steven 84 // Todd Kim 52 // // First strips the ones who got 100 on the exam using a queue, // then prints the rest in reverse order using a stack. Retains // the reversed list after printing import java.io.*; import java.util.*; public class Exams { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("exams.txt")); // read in lines from text file filtering out all that have exam // scores of 100 Queue q = new LinkedList(); while (input.hasNextLine()) { String line = input.nextLine(); if (!line.endsWith("100")) { q.add(line); } } Stack s = new Stack (); // move all tas to a stack to reverse the order while (!q.isEmpty()) { s.push(q.remove()); } // print out the tas in reverse order and add them back to the queue while (!s.isEmpty()) { String line = s.pop(); System.out.println(line); q.add(line); } } }