Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Lab04
- {
- class Osoba
- {
- private string imie, nazwisko;
- public string Imie
- {
- get => imie;
- set => imie = value;
- }
- public string Nazwisko
- {
- get => nazwisko;
- set => nazwisko = value;
- }
- public Osoba(string imie_, string nazwisko_) { imie = imie_; nazwisko = nazwisko_; }
- public string Wypiszinfo()
- {
- return imie + " " + nazwisko;
- }
- }
- class Element
- {
- public Osoba wartosc { get; set; }
- public Element nastepnyElement;
- public override string ToString()
- {
- return wartosc.Wypiszinfo();
- }
- public Element() { nastepnyElement = null; }
- public Element(Osoba wartosc_) {
- nastepnyElement = null;
- wartosc = wartosc_;
- }
- public Element(Osoba wartosc_, Element nastepnyElement_) {
- nastepnyElement = nastepnyElement_;
- wartosc = wartosc_;
- }
- };
- class Lista
- {
- private Element pierwszyElement = null;
- public int liczbaElementow = 0;
- public void Dodaj(Osoba wartosc_)
- {
- liczbaElementow++;
- if (pierwszyElement == null)
- {
- pierwszyElement = new Element(wartosc_);
- }
- else
- {
- Element temp = pierwszyElement;
- while (temp.nastepnyElement !=null)
- temp = temp.nastepnyElement;
- temp.nastepnyElement = new Element(wartosc_);
- }
- }
- public void Wstaw(Osoba wartosc_, int i)
- {
- if (i >= liczbaElementow) throw new IndexOutOfRangeException(); // czy index nie jest poza zakresem
- if(i == 0) // jesli element jest pierwszym
- {
- var tempNastepny = pierwszyElement.nastepnyElement;
- pierwszyElement = new Element(wartosc_, tempNastepny);
- //pierwszyElement.nastepnyElement = tempNastepny;
- }
- if(i >= 1)
- {
- var temp = pierwszyElement;
- int j = 0;
- while(temp != null) // szukam element on podanym indeksie
- {
- if (j == i)
- {
- break; // jesli j == i, znalazlem element
- }
- temp = temp.nastepnyElement;
- j++;
- }
- var tempReferencja = temp.nastepnyElement;
- temp.wartosc = wartosc_;
- temp.nastepnyElement = tempReferencja;
- }
- }
- public Element Pobierz(int i)
- {
- Element elementDoZwrocenia = null;
- if (i >= liczbaElementow) {
- throw new IndexOutOfRangeException();
- }
- if (i == 0)
- {
- elementDoZwrocenia = pierwszyElement;
- Console.WriteLine("Udalo sie pobrac element z indeksem [" + i + "]. Byla to persona: " + pierwszyElement.ToString());
- pierwszyElement = pierwszyElement.nastepnyElement;
- liczbaElementow--;
- }
- else if (i >= 1)
- {
- int j = 0;
- var temp = pierwszyElement;
- while(temp != null)
- {
- if ((j + 1) == i) break; // jesli udalo mi sie znalesc poprzedni element, wychodze z petli
- temp = temp.nastepnyElement;
- j++;
- }
- // jezeli usuwamy ostatni element listy
- if (temp.nastepnyElement.nastepnyElement == null)
- {
- elementDoZwrocenia = temp.nastepnyElement;
- Console.WriteLine("Udalo sie pobrac element z indeksem [" + i + "]. Byla to persona: " + temp.nastepnyElement.ToString());
- temp.nastepnyElement = null;
- }
- // jezeli usuwamy srodkowy element
- else
- {
- elementDoZwrocenia = temp.nastepnyElement;
- Console.WriteLine("Udalo sie pobrac element z indeksem [" + i + "]. Byla to persona: " + temp.nastepnyElement.ToString());
- var usuwana = temp.nastepnyElement;
- temp.nastepnyElement = temp.nastepnyElement.nastepnyElement;
- }
- liczbaElementow--;
- }
- return elementDoZwrocenia;
- }
- public void WypiszLiczbeElementow()
- {
- Console.WriteLine(liczbaElementow);
- }
- public void Wypisz()
- {
- Element temp = pierwszyElement;
- Console.WriteLine("*Lista wszystkich osob w liscie:");
- while(temp != null)
- {
- Console.WriteLine("*" +temp.ToString());
- temp = temp.nastepnyElement;
- }
- Console.WriteLine("*");
- }
- };
- class Program
- {
- static void Main(string[] args)
- {
- // indeksy sie zaczynaja od zera
- Osoba o = new Osoba("Alicja", "Nowak");
- Osoba o2 = new Osoba("Karolina", "Kowalska");
- Osoba o3 = new Osoba("Michal", "Jablonski");
- Osoba o4 = new Osoba("Karol", "Wisniewski");
- Lista lista = new Lista();
- lista.Dodaj(o);
- lista.Dodaj(o2);
- lista.Dodaj(o3);
- lista.Dodaj(o4);
- lista.Wypisz();
- lista.Pobierz(2);
- lista.Pobierz(0);
- lista.Pobierz(1);
- lista.Wypisz();
- lista.Dodaj(o);
- lista.Dodaj(o3);
- lista.Dodaj(o2);
- lista.Wstaw(o3, 0);
- lista.Wstaw(o4, 1);
- lista.Wstaw(o, 2);
- lista.Wypisz();
- // W podanym przez Pana kodzie testujacym wywolanie metody Wstaw nie jest mozwile, o ile indeks przekracza rozmiar kolekcji
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement