Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <assert.h>
- using namespace std;
- template <class T> class link;
- template <class T> class Iterator;
- class Coords {
- std::string name;
- double x,y,sum_of_xy;
- public:
- Coords(string name_,double x_,double y_): x(x_), y(y_), name(name_) {
- sum_of_xy = sum_of_coords();
- }
- Coords(){
- name = "name1";
- x = 0.0;
- y = 0.0;
- }
- double sum_of_coords(){
- return x+y;
- }
- double get_sum_of_coords(){
- return sum_of_xy;
- }
- int get_first_name_letter(){
- return name[0];
- }
- friend std::ostream& operator << (std::ostream& out, const Coords &pkt);
- };
- std::ostream& operator<<(std::ostream& os,const Coords &pkt){
- os << pkt.name << " " << pkt.x << " " << pkt.y << " ";
- return os;
- }
- // ======================================================================
- // Class Template list
- // ======================================================================
- template <class T>
- class list {
- protected:
- link <T> *First; // data field
- public:
- list() : First(nullptr) {} // default constructor
- //list(const list &source); // copy constructor
- virtual ~list() {}; // destructor
- virtual void add(T value); // insert a new item
- virtual void delete_all();
- T first_element() const; // access the first item
- //virtual bool includes(T value) const; // inclusion test
- bool is_empty() const;
- virtual void remove_first();
- friend class Iterator <T>;
- };
- // ======================================================================
- // Class Template link
- // ======================================================================
- template <class T>
- class link {
- private:
- T Value;
- link <T> *Next;
- link(T val, link *ptr) : Value(val), Next(ptr) { }
- public:
- link <T> *insert(T value); // after current link
- friend class list <T>;
- friend class Iterator <T>;
- };
- // ======================================================================
- // Class Template Iterator
- // ======================================================================
- template <class T> class Iterator {
- public:
- Iterator(list <T> &aList);
- virtual bool init();
- virtual T operator()();
- virtual bool operator !();
- virtual bool operator++(); // for prefix, for postfix add dummy int
- virtual bool operator++(int);
- virtual void operator=(T value);
- void remove_current();
- void add_before(T new_value);
- void add_after(T new_value);
- protected:
- list <T> &my_list; // data fields
- link <T> *previous;
- link <T> *current;
- };
- // ======================================================================
- // Class Template link - attributes
- // ======================================================================
- template <class T> link <T> * link <T> :: insert(T value)
- {
- Next = new link <T>(value, Next);
- return Next;
- }
- // ======================================================================
- // Class Template list - attributes
- // ======================================================================
- template <class T> void list <T> :: add(T value)
- {
- First = new link <T>(value, First);
- }
- template <class T> T list <T> :: first_element() const
- {
- assert(First != nullptr);
- return First->Value;
- }
- template <class T> bool list <T> :: is_empty() const
- {
- return First == nullptr;
- }
- /*
- template <class T> bool list <T> :: includes(T value) const
- {
- for (link <T> *p = First; p; p = p->Next)
- if (value == p->Value) return true;
- return false;
- }
- */
- template <class T> void list <T> :: remove_first() {
- assert(First != nullptr);
- link <T> *ptr = First; // save pointer to the first item
- First = ptr->Next; // reassign the First node
- delete ptr;
- }
- template <class T> void list <T> :: delete_all() {
- link <T> *next;
- for (link <T> *p = First; p; p = next){
- next = p->Next;
- delete p;
- }
- First = nullptr;
- }
- // ======================================================================
- // Class Template Iterator - attributes
- // ======================================================================
- template <class T> Iterator <T> ::
- Iterator(list <T> &aList) : my_list(aList){
- init();
- }
- template <class T> bool Iterator <T> :: init(){
- previous = nullptr;
- current = my_list.First;
- return current != nullptr;
- }
- template <class T> T Iterator <T> :: operator()(){
- assert(current != nullptr);
- return current->Value;
- }
- template <class T> void Iterator <T> :: operator=(T val){
- assert(current != nullptr);
- current->Value = val;
- }
- template <class T> void Iterator <T> :: remove_current()
- {
- assert(current != nullptr);
- if (previous == nullptr)
- my_list.First = current->Next;
- else
- previous->Next = current->Next;
- delete current;
- current = nullptr;
- }
- template <class T> bool Iterator <T> :: operator++(){
- if (current == nullptr){ // move current pointer
- if (previous == nullptr) // to next element
- current = my_list.First;
- else
- current = previous->Next;
- }
- else {
- previous = current;
- current = current->Next;
- }
- return current != nullptr;
- } // valid for prefix operator only!
- template <class T> bool Iterator <T> :: operator++(int){
- return operator++();
- }
- template <class T> bool Iterator <T> :: operator !(){
- if (current == nullptr and previous != nullptr)
- current = previous->Next;
- return current != nullptr; // termination of iterator
- }
- template <class T> void Iterator <T> :: add_before(T val)
- {
- if (previous)previous = previous->insert(val);
- else {
- my_list.list<T>::add(val); // to avoid subclass
- previous = my_list.First;
- current = previous->Next; // if current is NULL
- }
- }
- template <class T> void Iterator <T> :: add_after(T val)
- {
- if (current){current->insert(val); return;} // not shown
- if (previous)current = previous->insert(val);
- else my_list.list<T>::add(val);
- }
- int insert_index(){
- return 0;
- }
- // ======================================================================
- // main
- // ======================================================================
- int main() {
- int size_of_list = 3;
- double x,y;
- std::string name;
- list <Coords> myList;
- Iterator<Coords> myIterator(myList);
- for(int iter = 0; iter < size_of_list; iter++){
- cin>>name;
- cin>>x;
- cin>>y;
- if(iter == 0) {
- myList.add(Coords(name, x, y));
- myIterator.init();
- continue;
- }
- if(iter > 0) {
- for (int iter2 = 1; iter2 < size_of_list+1; iter2++) {
- if (myIterator().get_sum_of_coords() > x + y) {
- cout << ">" << endl;
- myIterator.add_before( Coords(name, x, y) );
- }
- if ( myIterator().get_sum_of_coords() < x + y ) {
- cout << "<" << endl;
- myIterator++;
- myIterator.add_after( Coords(name, x, y) );
- break;
- }
- if (myIterator().get_sum_of_coords() == x + y) {
- if (myIterator().get_first_name_letter() < name[0]) {
- cout << " == " << endl;
- myIterator.add_before( Coords(name, x, y) );
- } else {
- myIterator.add_after( Coords(name, x, y) );
- }
- break;
- }
- }
- }
- }
- for(myIterator.init(); !myIterator; myIterator++) {
- std::cout << myIterator();
- }
- // delete
- int number_to_delete;
- cout<<endl<<"Num to del"<<endl;
- cin>>number_to_delete;
- myIterator.init();
- for(int delete_iter = 0; delete_iter < size_of_list; delete_iter++) {
- if(delete_iter != number_to_delete) myIterator++;
- else myIterator.remove_current();
- }
- for(myIterator.init(); !myIterator; myIterator++) {
- std::cout << myIterator();
- }
- /*
- std::cout<< "=============== 1. Position of myIterator! ===============" << std::endl;
- std::cout<< myIterator() << std::endl;
- std::cout << "=============== 2. Showing content of the list! ===============" << std::endl;
- for(myIterator.init(); !myIterator; myIterator++) {
- std::cout << myIterator() << std::endl;
- }
- std::cout << "=============== 3. Position of myIterator! ===============" << std::endl;
- if(!myIterator) {
- cout << myIterator() << endl;
- }
- myIterator.init();
- ++myIterator;
- myIterator++;
- myIterator.add_before(4.0);
- cout << "=============== 4. Position of myIterator! ===============" << endl;
- cout << myIterator() << endl;
- cout << "=============== 5. Showing content of the list! ===============" << endl;
- for(myIterator.init(); !myIterator; myIterator++) {
- cout << myIterator() << endl;
- }
- cout << "Bye, bye!" << endl;
- return 0;
- */
- }
- // ===
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement