Advertisement
Danielos168

Lista Posortowana

Jan 19th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ListaPosortowana
  8. {
  9.  
  10.     class ListaPosortowana
  11.     {
  12.         List<int> L = new List<int>();
  13.  
  14.         public void Dodaj(int temp)
  15.         {
  16.             if(L.Count==0) L.Add(temp);
  17.             else if (L.Contains(temp))
  18.             {
  19.                 int i = L.IndexOf(temp);
  20.                 L.Insert(i,temp);
  21.             }
  22.             else if(L.Count==1 && temp < L[0]) L.Insert(0,temp);
  23.             else if (L.Count == 1 && temp > L[0]) L.Insert(1, temp);
  24.             else if (temp < L[0]) L.Insert(0,temp);
  25.             else if (temp> L[L.Count-1]) L.Add(temp);
  26.             else
  27.             {
  28.                 for (int i = 1; i < L.Count; i++)
  29.                 {
  30.                     if (L[i-1] < temp && L[i] > temp)
  31.                     {
  32.                         L.Insert(i,temp);
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.  
  38.         public void Wyswietl()
  39.         {
  40.             foreach (var i in L)
  41.             {
  42.                 Console.Write(i + ",");
  43.             }
  44.         }
  45.     }
  46.  
  47.     class Program
  48.     {
  49.         static void Main(string[] args)
  50.         {
  51.             ListaPosortowana List = new ListaPosortowana();
  52.  
  53.             for (int i = 0; i < 20; i++)
  54.             {
  55.                 Console.Write("Dodaj liczbę: ");
  56.                 List.Dodaj((Convert.ToInt32(Console.ReadLine())));
  57.                 List.Wyswietl();
  58.                 Console.WriteLine();
  59.  
  60.             }
  61.  
  62.  
  63.             Console.ReadKey();
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement