Below is the syntax highlighted version of PList.java.
/****************************************************************************** * Name: * NetID: * Precept: * * PList - Popularity List: Stack Version * list where the most recently accessed or added entry * is always on top. * * Dependencies: Entry, Stack, StdOut *****************************************************************************/ public class PList { // instance variables private Stack<Entry> list; // stack of entries holds the whole list // constructor just initializes the empty list public PList() { list = new Stack<Entry>(); } // add entry to top of list public void add(String name, String num) { Entry en = new Entry(name, num); list.push(en); } // output the list public void show() { if (list.isEmpty()) { StdOut.println("empty PList"); return; } // not empty, output the list for (Entry en : list) { StdOut.println(en); } } // move entry with name to top of list, return number // return null if not there public String find(String name) { // need a temp stack to hold onto other entries Stack<Entry> temp = new Stack<Entry>(); Entry found = null; // move everybody over to temp except your entry while (!list.isEmpty()) { Entry en = list.pop(); if (en.equals(name)) { // found it! remember it. found = en; } // not right name. save on temp and keep going else temp.push(en); } // replace list while (!temp.isEmpty()) { list.push(temp.pop()); } // Did we find it? if (found == null) return null; // put found on top list.push(found); return found.getNum(); } //remove and return Entry from list public Entry cut(String name) { // use find() to move Entry to top of list String found = find(name); if (found == null) throw new RuntimeException(name + "not found"); // return Entry and remove it from list return list.pop(); } // update num or add entry to list public void update(String name, String num) { // Is it already on list? String found = find(name); if (found != null) { // if there, cut it cut(name); } // either way, add new version add(name, num); return; } // test main public static void main(String[] args) { PList list = new PList(); list.add("Doug", "6092586314"); list.add("Donna", "6092581978"); list.add("Christopher", "6092585388"); list.add("Maia", "6092586484"); list.add("Chris", "6092582038"); StdOut.println("Entire list:"); list.show(); StdOut.println("==========================="); list.update("Maia", "6092588888"); list.cut("Chris"); StdOut.println("Doug " + list.find("Doug")); StdOut.println("Maria " + list.find("Maria")); StdOut.println("==========================="); StdOut.println("Updated list:"); list.show(); } }