Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include"List.h"
- #include"Zad2.h"
- #define x 3
- template<class T>
- void print_list(List<T> *p)
- {
- unsigned int i = 1;
- cout << endl;
- while (p)
- {
- cout << i << ". " << p->data << endl;
- p = p->next;
- ++i;
- }
- }
- template<class T>
- unsigned int count(List<T> *p)
- {
- unsigned int counter = 0;
- while (p)
- {
- ++counter;
- p = p->next;
- }
- return counter;
- }
- template<class T>
- void add_to_list(List<T> *&head, List<T> *&tail, T element)
- {
- List<T> *p = new List<T>;
- p->data = element;
- if (head)
- {
- tail->next = p;
- tail = tail->next;
- }
- else head = tail = p;
- }
- template<class T>
- void delete_from_list(List<T> *&head, List<T> *&tail)
- {
- cout << "\nKtory element chcesz usunac?\n";
- print_list(head);
- List<T> *p = head;
- unsigned int n, ilosc = count(head);
- cin >> n;
- if (n > ilosc)
- return;
- else
- {
- if (n == 1)
- {
- head = head->next;
- delete p;
- }
- else
- {
- while (n > 2)
- {
- p = p->next;
- --n;
- }
- if (p->next == tail)
- {
- delete tail;
- tail = p;
- p->next = NULL;
- }
- else
- {
- List<T> *cleaner = p->next;
- p->next = p->next->next;
- delete cleaner;
- }
- }
- }
- cout << "\nPo usunieciu:\n";
- print_list(head);
- }
- void printmenu()
- {
- cout << "\n(1)Wyswietl liste";
- cout << "\n(2)Dodaj do listy";
- cout << "\n(3)Usun z listy";
- cout << "\n(4)Zapisz liste do pliku";
- cout << "\n(5)Odczytaj liste z pliku";
- cout << "\n(6)Wyszukaj studenta";
- cout << "\n(0)Powrot\n";
- }
- template<class T>
- void delete_list(List<T> *&head)
- {
- List<T> *p;
- while (head)
- {
- p = head;
- head = head->next;
- delete p;
- }
- }
- template<class T>
- void add_from_file(List<T> *&head, List<T> *&tail)
- {
- }
- void save(List<Student> *p)
- {
- const char *txt = "St.bin";
- ofstream file1(txt, ios::out | ios::binary);
- int ilosc = count(p);
- Student *tab = new Student[ilosc];
- int i = 0;
- while (p)
- {
- tab[i] = p->data;
- p = p->next;
- ++i;
- }
- file1.write((char*)tab, ilosc*sizeof(Student));
- file1.close();
- delete[] tab;
- }
- template<class T>
- void load(List<T> *&head, List<T> *&tail)
- {
- const char *txt = "St.bin";
- ifstream file1(txt, ios::in | ios::binary);
- Student *tab = new Student[sizeof(Student)];
- file1.read((char*)tab, sizeof(Student));
- while (!(file1.eof()))
- {
- add_to_list(head, tail, *tab);
- file1.read((char*)tab, sizeof(Student));
- }
- delete[] tab;
- }
- void find_id(List<Student> *p)
- {
- int id, counter = 0;
- cout << "\nWprowadz indeks: ";
- cin >> id;
- while (p)
- {
- counter++;
- if (p->data.nr_indeksu == id)
- {
- cout << "\nOdnaleziono element. Jest on na pozycji nr " << counter;
- cout << ".\nDane studenta:\n" << p->data;
- }
- p = p->next;
- }
- }
- void zad3()
- {
- List<Student> *head = NULL, *tail = NULL;
- Student s1, s2, s3;
- s1 = { "Sebastian", "Jena", 23041995, 122045 };
- s2 = { "Jan", "Nowak", 01012000, 123456 };
- s3 = { "Ktos", "Ktosik", 30042000, 334234 };
- add_to_list(head, tail, s1);
- add_to_list(head, tail, s2);
- add_to_list(head, tail, s3);
- char podmenu;
- do
- {
- printmenu();
- podmenu = _getch();
- switch (podmenu)
- {
- case '1':
- print_list(head);
- break;
- case '2':
- {
- cout << "Wpisz odpowiednio imie, nazwisko, date urodzenia i indeks,\n i zatwierdzajac wszystko Enterem:\n";
- Student dodaj;
- cin >> dodaj;
- add_to_list(head, tail, dodaj);
- }break;
- case '3':
- delete_from_list(head, tail);
- break;
- case '4':
- save(head);
- break;
- case '5':
- load(head, tail);
- break;
- case '6':
- find_id(head);
- break;
- default:
- break;
- }
- } while (podmenu != '0');
- delete_list(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement