import java.util.Scanner; /* * מחלקת שרות המספקת פעולות שונות לרשימה * List של מספרים שלמים * * size (lst) אורך רשימה * howMany (lst, x) מספר מופעים * exist (lst,x) נמצא ברשימה * place (lst,x) מקום ברשימה * maxElement (lst) מקסימום * maxPlace (lst) מקום המקסימום * getPrev (lst,pos) קודם ברשימה * getLast (lst) אחרון ברשימה */ public class ListAlgorithmPatterns { static Scanner input = new Scanner(System.in); // --- return a List of Int --- public static List listBuild() { List list = new List(); Node pos = list.getFirst(); System.out.print("type a number (-1 to finish) --> "); int x = input.nextInt(); while (x != -1) { pos = list.insert(pos, x); System.out.print("type a number (-1 to finish) --> "); x = input.nextInt(); } return list; } //--- הפעולה מחזירה את מספר האיברים ברשימה --- public static int size(Listlst) { } //--- x הפעולה מחזירה את מספר המופעים של האיבר --- public static int howMany(Listlst , int x) { } //--- lst קיים ברשימה x הפעולה מחזירה "אמת" אם --- //--- ו-"שקר" אחרת --- public static boolean exist(List lst, int x) { } //--- x הפעולה מחזירה הפניה לאיבר --- //--- null אם הוא קיים ברשימה. אם לא, יוחזר הערך --- public static Node place(List lst, int x) { } //--- פעולה המחזירה את הערך המקסימלי ברשימה --- //--- הנחה: הרשימה אינה ריקה --- public static int maxElement(List lst) { } //--- פעולה המחזירה את מקומו של הערך המקסימלי ברשימה --- //--- הנחה: הרשימה אינה ריקה --- public static Node maxPlace(List lst) { } //--- pos -פעולה המחזירה הפניה לחוליה הקודמת ל --- //--- null מפנה לראשון ברשימה, יוחזר הערך pos אם --- //-- הינו הפניה למקום ברשימה pos :הנחה --- //--- List הערה: שימו לב שהפעולה יכולה להיות גם פעולה פנימית של המחלקה public static Node getPrev(List lst,Nodepos) { } //--- פעולה המחזירה הפניה לחוליה האחרונה ברשימה --- //--- null אם הרשימה ריקה יוחזר הערך --- //--- List הערה: שימו לב שהפעולה יכולה להיות גם פעולה פנימית של המחלקה public static Node getLast(List lst) { } public static void main(String[] args) { List lst = listBuild(); System.out.println(lst.toString()); int n = size(lst); System.out.println("size = "+n); System.out.print("type a num to find --> "); int x = input.nextInt(); if (exist(lst,x)) System.out.println(x + " קיים ברשימה " ); else System.out.println(x + " לא קיים ברשימה "); int max = maxElement(lst); System.out.println(max + " : האיבר המקסימלי ברשימה"); System.out.println(howMany(lst,max) + " : מספר המופעים של המקסימום"); if (maxPlace (lst) != lst.getFirst()) { x = getPrev(lst, maxPlace (lst)).getInfo(); System.out.println(x + " : האיבר הקודם לאיבר המקסימלי"); } else System.out.println("האיבר המקסימלי נמצא בתחילת הרשימה"); System.out.println(getLast(lst) + " : האיבר האחרון ברשימה"); } }