You are on page 1of 2

C:\Users\magshimim\Desktop\Summary.

txt Sunday, May 12, 2019 9:16 PM


using System;
//using System.Collections.Generic;
using Unit4.CollectionsLib;

namespace ConsoleApplication1
{
class UsefulMethods
{
/// ‫רשימה‬
///
//‫ הפעולה מקבלת רשימה של מספרים שלמים ומספר שלם‬:‫טענת כניסה‬
// ‫ אחרת מחזירה "שקר‬,‫ הפעולה מחזירה "אמת" אם המספר נמצא ברשימה‬:‫"טענת יציאה‬
// ‫סיבוכיות זמן ריצה‬: O(n)
public static bool IsExistList(Node<int> l, int n)
{
Node<int> pos = l;
while (pos != null)
{
if (pos.GetValue() == n)
return true;
pos = pos.GetNext();
}
return false;
}
// ‫ הפעולה מקבלת רשימה של מספרים שלמים‬:‫טענת כניסה‬
// ‫ הפעולה מחזירה את סכום כל איברי הרשימה‬:‫טענת כניסה‬
// ‫סיבוכיות זמן ריצה‬: O(n)
public static int SumList(Node<int> l)
{
int sum = 0;
Node<int> pos = l;
while (pos != null)
{
sum += pos.GetValue();
pos = pos.GetNext();
}
return sum;
}
// ‫ הפעולה מקבלת רשימה של מספרים שלמים‬:‫טענת כניסה‬
// ‫ מספר החוליות בה‬- ‫ הפעולה מחזירה את אורך הרשימה‬:‫טענת יציאה‬
// ‫סיבוכיות זמן ריצה‬: O(n)
public static int LengthList(Node<int> l)
{
int length = 0;
Node<int> pos = l;
while (pos != null)
{
length++;
pos = pos.GetNext();
}
return length;
}
// ‫ הפעולה מקבלת רשימה של מספרים שלמים ומספר שלם‬:‫טענת כניסה‬
// ‫ הפעולה מחזירה את מספר הפעמים שהמספר מופיע ברשימה‬:‫טענת יציאה‬
// ‫סיבוכיות זמן ריצה‬: O(n)
public static int HowManyList(Node<int> l, int n)
{
int count = 0;
Node<int> pos = l;
while (pos != null)
{
if (pos.GetValue() == n)
count++;
pos = pos.GetNext();
}
return count;
}

-1-
C:\Users\magshimim\Desktop\Summary.txt Sunday, May 12, 2019 9:16 PM
// ‫ הפעולה מקבלת רשימה של מספרים שלמים ומספר שלם‬:‫טענת כניסה‬
// ‫ הפעולה מוחקת את כל מופעי המספר שהתקבל מהרשימה‬:‫טענת יציאה‬
// ‫סיבוכיות זמן ריצה‬: O(n)
public static void DebugList(Node<int> l, int n)
{
Node<int> pos = l;
while (pos != null)
if (pos.GetValue() == n)
pos = l.Remove(pos);
else pos = pos.GetNext();
}
// ‫ הפעולה מקבלת רשימה ממוינת של מספרים שלמים ומספר שלם‬:‫טענת כניסה‬
// ‫ הפעולה מכניסה באופן ממוין את המספר לרשימה‬:‫טענת יציאה‬
// ‫סיבוכיות זמן ריצה‬: O(n)
public static void InsertSorted(Node<int> l, int n)
{
Node<int> pos = l;
Node<int> before = null;
while (pos != null && n > pos.GetValue())
{
before = pos;
pos = pos.GetNext();
}
before.SetNext(new Node<int>(n,pos));
}
// ‫ הפעולה מקבלת רשימה של מספרים שלמים‬:‫טענת כניסה‬
// ‫ אחרת מחזירה "שקר‬,‫ הפעולה מחזירה "אמת" אם הרשימה ממוינת בסדר עולה‬:‫"טענת יציאה‬
// ‫סיבוכיות זמן ריצה‬: O(n)
public static bool IsSorted(Node<int> l)
{
Node<int> pos = l;
while (pos.GetNext() != null)
{
if (pos.GetValue() > pos.GetNext().GetValue())
return false;
pos = pos.GetNext();
}
return true;
}
// ‫ הפעולה מקבלת רשימה לא ממוינת של מספרים שלמים‬:‫טענת כניסה‬
// ‫ הפעולה מחזירה רשימה המכילה את כל ערכי הרשימה שהתקבלה בסדר ממוין עולה‬:‫טענת יציאה‬
// ‫סיבוכיות זמן ריצה‬: O(n²)
public static Node<int> Sort(Node<int> l)
{
Node<int> newL = new Node<int>();
Node<int> pos = l;
while (pos != null)
{
InsertSorted(newL, pos.GetValue()); //! //O(n)
pos = pos.GetNext();
}
return newL;
}

-2-

You might also like