Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "node.h"
- template <typename typo>
- class list {
- protected:
- node <typo> *head; // wskaznik na pierwszy element listy
- public:
- list() {
- head = nullptr;
- }
- ~list() {}
- /* sprawdza czy lista jest pusta*/
- bool empty() const {
- if (head == nullptr) {
- return true;
- }
- else {
- return false;
- }
- }
- /* zwraca pierwszy element z listy*/
- const typo & front() const {
- if(empty()==true){
- error();
- return 0;
- }
- else{
- return head->element;
- }
- }
- /* dodaje element na poczatek listy*/
- virtual void add(const typo& element) {
- node<typo> *newNode = new node <typo>;
- newNode->element = element;
- newNode->next = head;
- head = newNode;
- //cout << " Dodano element" << endl;
- }
- /* usuwa i zwraca pierwszy element z listy*/
- typo removeFront() {
- typo returnValue = 0;
- if (empty()==true) {
- error();
- }
- else {
- node<typo> *oldHead = head;
- returnValue = oldHead->element;
- head = oldHead->next;
- delete oldHead;
- // cout << " Usunieto element" << endl;
- }
- return returnValue;
- }
- /* wypisuje liste od poczatku*/
- void print() {
- if (empty() == true) {
- cout << " Struktura jest pusta!" << endl;
- }
- else {
- node<typo> *temp = head;
- while (temp != nullptr) {
- cout << temp->element << " ";
- temp = temp->next;
- }
- cout << endl;
- }
- }
- /* usuwa wszystkie elementy z listy*/
- void remove() {
- if (empty()==true) {
- error();
- }
- else {
- node<typo> *oldHead;
- while (head != nullptr) {
- oldHead = head;
- head = oldHead->next;
- delete oldHead;
- }
- cout << " Usunieto wszystkie elementy" << endl;
- }
- }
- /* wypisuje komunikat o niepoprawnej operacji*/
- void error() {
- cout << " Nie mozna wykonac tej operacji" << endl;
- }
- /* menu operacji*/
- virtual void menu() {
- cout << " 1. Dodaj element na poczatek listy" << endl;
- cout << " 2. Usun element z pocztku listy" << endl;
- cout << " 3. Wyswietl zawartosc listy" << endl;
- cout << " 4. Usun zawartosc listy" << endl;
- cout << " 5. Wroc" << endl;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement