import cs101.terminal.*; public class StableMarriage { public static void main(String args[]) { Terminal.startTranscript("transcript.txt"); test(); Terminal.stopTranscript(); } public static Relation findMarriages(PersonList eligible) { Relation couples = new Relation(); while (eligible != null) { Man m = (Man) eligible.person; Woman w = m.topPick(); Terminal.println("" + m + " proposes to " + w); if (w.likes(m)) { Terminal.print(" she accepts "); Man oldHusband = (Man) couples.lookup(w); if (oldHusband == null) eligible = eligible.next; else { Terminal.print("(dumping " + oldHusband + ")"); eligible = new PersonList(oldHusband, eligible.next); } couples.map(w,m); w.trimList(m); Terminal.println("\n but she still prefers: " + w.getList()); } m.scratchTop(); } return couples; } public static void test() { Man andy, bob, charles, david, edward; Woman alice, beth, cindy, diane, elaine; andy = new Man("andy"); bob = new Man("bob"); charles = new Man("charles"); david = new Man("david"); edward = new Man("edward"); alice = new Woman("alice"); beth = new Woman("beth"); cindy = new Woman("cindy"); diane = new Woman("diane"); elaine = new Woman("elaine"); andy.prepend(elaine).prepend(diane).prepend(cindy).prepend(beth).prepend(alice); bob.prepend(elaine).prepend(beth).prepend(cindy).prepend(alice).prepend(diane); charles.prepend(cindy).prepend(diane).prepend(beth).prepend(elaine).prepend(alice); david.prepend(diane).prepend(beth).prepend(cindy).prepend(alice).prepend(elaine); edward.prepend(elaine).prepend(cindy).prepend(diane).prepend(beth).prepend(alice); alice.prepend(edward).prepend(bob).prepend(charles).prepend(david).prepend(andy); beth.prepend(edward).prepend(david).prepend(charles).prepend(bob).prepend(andy); cindy.prepend(david).prepend(andy).prepend(charles).prepend(bob).prepend(edward); diane.prepend(david).prepend(charles).prepend(bob).prepend(edward).prepend(andy); elaine.prepend(andy).prepend(david).prepend(edward).prepend(charles).prepend(bob); PersonList eligible = new PersonList(edward, new PersonList(david, new PersonList(charles, new PersonList(bob, new PersonList(andy,null))))); Terminal.println("" + findMarriages(eligible)); } }