static void Main(string[] args) { Song song1 = new Song("bla bla", 7, "22.02.2002", "h"); Node <Song> songs = new Node <Song>(song1); Node <Song> song2 = new Node <Song>(new Song("Elections 2019", 14, "17.09.2019", "q")); songs.SetNext(song2); Disk disk1 = new Disk(songs, "1.1.2002", 10000, "l"); Node <Disk> disks = new Node <Disk>(disk1); Singer singer1 = new Singer(songs, disks); Console.WriteLine("commends are: 1. check_len, then a space, if you want a song, write s and then a name of a song, if you want a disk, write the name of the disk.\n2. buy_disk and the the name of the disk.\n3. search, then a space and words, and the result will be a song that contains these words.\n 4. buy, a space and the name of the disk\n5. search, a space, and words in the song"); string commend = Console.ReadLine(); string[] commend_and_params = commend.Split(); if (commend_and_params[0] == "check_len") { if (commend_and_params[1] == "s") { Node <Song> song = songs; while (song != null) { if (commend_and_params[2] == song.GetValue().GetName()) { Console.WriteLine("len is " + song.GetValue().GetLen()); } song = song.GetNext(); } } else { Node <Disk> disk = disks; while (disk != null) { if (commend_and_params[1] == disk.GetValue().GetName()) { Console.WriteLine("len is " + disk.GetValue().GetLen()); } disk = disk.GetNext(); } } } else if (commend_and_params[0] == "buy") { Console.WriteLine("bought successfully"); } else { Node <Song> song = songs; while (song != null) { if (song.GetValue().GetWords().Contains(commend_and_params[1])) { Console.WriteLine("song is " + song.GetValue().GetName()); } song = song.GetNext(); } } }
// טענת כניסה: הפעולה מקבלת רשימה ממוינת של מספרים שלמים ומספר שלם // טענת יציאה: הפעולה מכניסה באופן ממוין את המספר לרשימה // סיבוכיות זמן ריצה: O(n) public static void InsertSorted(List <int> l, int n) { Node <int> pos = l.GetFirst(); Node <int> before = null; while (pos != null && n > pos.GetInfo()) { before = pos; pos = pos.GetNext(); } l.Insert(before, n); }
// טענת כניסה: הפעולה מקבלת רשימה של מספרים שלמים // טענת יציאה: הפעולה מחזירה את אורך הרשימה - מספר החוליות בה // סיבוכיות זמן ריצה: O(n) public static int LengthList(List <int> l) { int length = 0; Node <int> pos = l.GetFirst(); while (pos != null) { length++; pos = pos.GetNext(); } return(length); }
// טענת כניסה: הפעולה מקבלת רשימה של מספרים שלמים // טענת כניסה: הפעולה מחזירה את סכום כל איברי הרשימה // סיבוכיות זמן ריצה: O(n) public static int SumList(List <int> l) { int sum = 0; Node <int> pos = l.GetFirst(); while (pos != null) { sum += pos.GetInfo(); pos = pos.GetNext(); } return(sum); }
public Disk(Node <Song> songs, string date, int price, string name) { this.songs = songs; this.date = date; this.price = price; this.total_len = 0; this.name = name; while (songs != null) { this.total_len += songs.GetValue().GetLen(); songs = songs.GetNext(); } }
/* הערות: * מחלקת עזר זו כוללת פעולות עזר על רשימה, מחסנית, תור ועץ בינארי * לכל פעולה כתובות מעליה (כהערות) טענת הכניסה, טענת היציאה וסיבוכיות זמן הריצה של הפעולה * הערה עם סימן קריאה משמעה פעולה חיצונית שקיימת במחלקת עזר זו - יש לכתוב את הפעולה החיצונית! * רוב הפעולות מקבלות טיפוס נתונים מסוג מספר שלם, אך יכולות לעבוד עם כל טיפוס אחר * © כל הזכויות שמורות לערן - Saurik * לבקשות, הערות והצעות: http://www.fxp.co.il/showthread.php?t=14343465 * בהצלחה בבחינות! */ /// /// רשימה /// //טענת כניסה: הפעולה מקבלת רשימה של מספרים שלמים ומספר שלם // טענת יציאה: הפעולה מחזירה "אמת" אם המספר נמצא ברשימה, אחרת מחזירה "שקר" // סיבוכיות זמן ריצה: O(n) public static bool IsExistList(List <int> l, int n) { Node <int> pos = l.GetFirst(); while (pos != null) { if (pos.GetInfo() == n) { return(true); } pos = pos.GetNext(); } return(false); }