Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program.cs
- using System;
- using Unit4.CollectionsLib;
- using System.Linq;
- using System.Text;
- namespace Q4
- {
- class Program
- {
- //טענת כניסה: הפעולה מקבלת רשימה
- //טענת יציאה: הפעולה מחזירה את אורך הרשימה
- //סיבוכיות O(n)
- public static int Length(Node<int> l)
- {
- int count = 0;
- Node<int> n = l;
- while (n != null)
- {
- count++;
- n = n.GetNext();
- }
- return count;
- }
- //טענת כניסה: הפעולה מקבלת רשימה ואינדקס (הערה - המספור מתחיל מ-0!!!) המציין מיקום של איבר ברשימה
- //טענת יציאה: הפעולה מחזירה מצביע לאיבר הנמצא במקום זה ברשימה
- //סיבוכיות O(n)
- public static Node<int> GetPosAt(Node<int> l, int index)
- {
- Node<int> pos = l;
- for (int i = 0; i < index; i++)
- pos = pos.GetNext();
- return pos;
- }
- public static int MiddleInNode(Node<int> l)
- {
- int length = Length(l);
- if (length % 2 == 0)
- return 0;
- return GetPosAt(l, length / 2).GetInfo();
- }
- public static Queue<int> CreateQueue(Stack<Node<int>> s)
- {
- Queue<int> q = new Queue<int>();
- while (!s.IsEmpty())
- {
- q.Insert(MiddleInNode(s.Pop()));
- Console.WriteLine(q);
- }
- return q;
- }
- //returns the smallest value stored in a stack
- public static int Smallest(Queue<int> q)
- {
- int x = 0, min = int.MaxValue, n = 0;
- if (!q.IsEmpty())
- {
- x = q.Remove();
- n = Smallest(q);
- if (min == int.MaxValue)
- min = x;
- if (n < min)
- min = n;
- }
- if (x != 0)
- q.Insert(x);
- return min;
- }
- //removes all instances of a given value from a stack
- public static void RemoveAllX(Queue<int> q, int x)
- {
- int y = 0;
- if (!q.IsEmpty())
- {
- y = q.Remove();
- RemoveAllX(q, x);
- }
- if (y != x && y != 0)
- q.Insert(y);
- }
- //Sorts a stack in a descending order
- public static Queue<int> Sort(Queue<int> q)
- {
- int next;
- Queue<int> sorted = new Queue<int>();
- while (!q.IsEmpty())
- {
- next = Smallest(q);
- sorted.Insert(next);
- RemoveAllX(q, next);
- Console.WriteLine(sorted);
- }
- return sorted;
- }
- //******
- public bool IsFound(Node<Person> l, Person k)
- {
- Node<Person> tmp = l;
- while (tmp != null)
- {
- if (tmp.GetInfo().Equelsper(k))
- return true;
- tmp = tmp.GetNext();
- }
- return false;
- }
- //טענת כניסה: הפעולה מקבלת רשימה
- //טענת פלט: הפעולה מדפיסה את איברי הרשימה
- //סיבוכיות O(n)
- public static void PrintNode(Node<int> n)
- {
- Node<int> l = n;
- while (l != null)
- {
- if (l.GetNext() != null)
- Console.Write(l.GetInfo() + " --> ");
- else
- Console.WriteLine(l.GetInfo() + ".");
- l = l.GetNext();
- }
- }
- static void Main(string[] args)
- {
- Stack<Node<int>> s = new Stack<Node<int>>();
- Node<int> tmp = new Node<int>(22, new Node<int>(7, new Node<int>(8, new Node<int>(5, new Node<int>(3, new Node<int>(12, new Node<int>(2)))))));
- PrintNode(tmp);
- s.Push(tmp);
- tmp = new Node<int>(2, new Node<int>(7, new Node<int>(11, new Node<int>(3, new Node<int>(23, new Node<int>(14, new Node<int>(2, new Node<int>(66))))))));
- PrintNode(tmp);
- s.Push(tmp);
- tmp = new Node<int>(8, new Node<int>(12, new Node<int>(3, new Node<int>(5, new Node<int>(6)))));
- PrintNode(tmp);
- s.Push(tmp);
- tmp = new Node<int>(21, new Node<int>(9, new Node<int>(7, new Node<int>(6, new Node<int>(25, new Node<int>(22, new Node<int>(1, new Node<int>(4, new Node<int>(17)))))))));
- PrintNode(tmp);
- s.Push(tmp);
- Queue<int> q = new Queue<int>();
- q = CreateQueue(s);
- q = Sort(q);
- Console.WriteLine(q);
- }
- }
- }
- ***********************
- Person.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Q4
- {
- public class Person
- {
- private string name;
- private string surname;
- private int age;
- public Person(string name, string surname, int age)
- {
- this.name = name;
- this.surname = surname;
- this.age = age;
- }
- public bool Equelsper(Person p)
- {
- return this.name.Equals(p.Name) && this.surname.Equals(p.Surname) && this.age.Equals(p.Age);
- }
- public string Name
- {
- get { return this.name; }
- set { this.name = value; }
- }
- public string Surname
- {
- get { return this.surname; }
- set { this.surname = value; }
- }
- public int Age
- {
- get { return this.age; }
- set { this.age = value; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement