Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ListaPosortowana
- {
- class ListaPosortowana
- {
- List<int> L = new List<int>();
- public void Dodaj(int temp)
- {
- if(L.Count==0) L.Add(temp);
- else if (L.Contains(temp))
- {
- int i = L.IndexOf(temp);
- L.Insert(i,temp);
- }
- else if(L.Count==1 && temp < L[0]) L.Insert(0,temp);
- else if (L.Count == 1 && temp > L[0]) L.Insert(1, temp);
- else if (temp < L[0]) L.Insert(0,temp);
- else if (temp> L[L.Count-1]) L.Add(temp);
- else
- {
- for (int i = 1; i < L.Count; i++)
- {
- if (L[i-1] < temp && L[i] > temp)
- {
- L.Insert(i,temp);
- }
- }
- }
- }
- public void Wyswietl()
- {
- foreach (var i in L)
- {
- Console.Write(i + ",");
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- ListaPosortowana List = new ListaPosortowana();
- for (int i = 0; i < 20; i++)
- {
- Console.Write("Dodaj liczbę: ");
- List.Dodaj((Convert.ToInt32(Console.ReadLine())));
- List.Wyswietl();
- Console.WriteLine();
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement