Advertisement
bazmikel

OOP | LAB 04. Zadanie 1

Mar 30th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4.  
  5. namespace Lab04
  6. {
  7.  
  8.     class Element
  9.     {
  10.        public object wartosc { get; set; }
  11.        public Element nastepnyElement;
  12.        
  13.  
  14.        
  15.  
  16.        public Element() { wartosc = null; nastepnyElement = null; }
  17.        public Element(object wartosc_, Element nastepnyElement_) {
  18.             wartosc = wartosc_;
  19.             nastepnyElement = nastepnyElement_;
  20.         }
  21.          
  22.        
  23.     };
  24.  
  25.  
  26.     class Kolejka: Element {
  27.         private Element pierwszyElement = null;
  28.         private Element ostatniElement = null;
  29.         public int liczbaElementow;
  30.  
  31.         public void Dodaj(object wartosc_)
  32.         {
  33.             liczbaElementow++;
  34.             if (pierwszyElement == null)
  35.                 pierwszyElement = ostatniElement = new Element() { wartosc = wartosc_ };
  36.             else
  37.                 ostatniElement = ostatniElement.nastepnyElement = new Element() { wartosc = wartosc_ };
  38.            
  39.         }
  40.  
  41.         public object Pobierz() {
  42.             if(pierwszyElement == null)
  43.             {
  44.                 Console.WriteLine("Kolekcja jest pusta");
  45.                 return null;
  46.             }
  47.             else
  48.             {
  49.                 liczbaElementow--;
  50.                 var usuwanyElement = pierwszyElement.wartosc;
  51.                 pierwszyElement = pierwszyElement.nastepnyElement;
  52.                 Console.WriteLine("Usunieto pierwszy element kolejki z wartoscia: " + usuwanyElement.ToString());
  53.                 return usuwanyElement;
  54.                
  55.                
  56.             }
  57.  
  58.         }
  59.         private Element Get(int i)
  60.         {
  61.             var e = pierwszyElement;
  62.             while (i-- > 0 && e != null)
  63.                 e = e.nastepnyElement;
  64.             if (e == null)
  65.                 throw new IndexOutOfRangeException();
  66.             return e;
  67.         }
  68.         public object this[int i] { get => Get(i).wartosc; set => Get(i).wartosc = value; }
  69.  
  70.         public void Wypisz()
  71.         {  
  72.             for(int i = 0; i < liczbaElementow; i++)
  73.             {
  74.                 Console.WriteLine((object)this[i].ToString());
  75.             }
  76.         }
  77.  
  78.     };
  79.    
  80.  
  81.  
  82.     class Program
  83.     {
  84.         static void Main(string[] args)
  85.         {
  86.             Kolejka k = new Kolejka();
  87.             k.Dodaj(1);
  88.             k.Dodaj(2);
  89.             k.Dodaj(3);
  90.             k.Dodaj(8);
  91.             k.Wypisz();
  92.             Console.WriteLine("Liczba elementow: {0}", k.liczbaElementow);    
  93.             int element = (int)k.Pobierz();
  94.             Console.WriteLine("Pobrany element: {0}", element);
  95.             k.Pobierz();
  96.             k.Wypisz();
  97.             Console.WriteLine("Liczba elementow: {0}", k.liczbaElementow);
  98.             k.Dodaj(7);
  99.             k.Dodaj(4);
  100.             k.Wypisz();
  101.             Console.WriteLine("Liczba elementow: {0}", k.liczbaElementow);
  102.             element = (int)k.Pobierz();
  103.             Console.WriteLine("Pobrany element: {0}", element);
  104.             k.Wypisz();
  105.             Console.WriteLine("Liczba elementow: {0}", k.liczbaElementow);
  106.             Console.ReadKey();
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement