/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include "list_.h"
#include "sznur.h"
using namespace std;
int main() {
for (size_t i=0;i<3;i++)
{ cout<<"\n___________START_______________________________________\n";
sznur sz;
sz.dodaj();
sz.print();
sz.usun();
cout<<"\n___________ROZWIAZANIE___START_______________________________________\n";
sz.print();
cout<<"\n___________ROZWIAZANIE___END_______________________________________\n";
}
}
#include "list_.h"
list_::~list_() {
while(first) {
std::cout<<first->data<<"->";
element* temp=first;
first=first->next;
delete temp;
}
std::cout<<std::endl;
}
int list_:: size()const {
int n=0;
element *temp=first;
while(temp) {
temp=temp->next;
n++;
}
return n;
}
void list_::print() {
element* current=first;
while(current) {
std::cout<<current->data<<"->";
current=current->next;
}
std::cout<<"nullptr"<<std::endl;
}
#include "sznur.h"
sznur::sznur() {
//ctor
}
sznur::~sznur() {
//dtor
}
void sznur::dodaj() {
int data;
do {
std::cin>>data;
if(std::cin.fail())
break;
l.push(data);
} while(std::cin.peek()!='\n');
}
void sznur::usun() {
using Node = element;
// using Node->nast=Node->next;
/// Node*zaszuk=nullptr;
Node*a=l.getFirst();
Node* last = a;
int stop=10;
while(a!=nullptr && a->next!=nullptr && a->next->next!=nullptr&&stop--) {
Node*jeden=a;
Node*dwa=a->next;
Node*trzy=a->next->next;
std::cout<<"jeden dwa trzy "<<jeden->data<<' '<<dwa->data<<' '<<trzy->data<<std::endl;
float srd=(jeden->data+trzy->data)/2.0;
float wartosc=dwa->data-srd;
wartosc=wartosc<0?wartosc*(-1):wartosc;
bool flaga = wartosc<0.000000001;
std::cout<<"\n wartosc="<<wartosc<<", flaga="<<flaga<<std::endl;
if(flaga /**dwa->date==srd*/) {
/// jeden->next = trzy->next;
jeden->next=trzy;
delete dwa;
/// zaszuk=jeden;
/// a=zaszuk->next;
if(trzy->next!=nullptr) {
last=trzy;
a=last->next;
} else {
last=trzy;
a=last->next=nullptr;
}
} else {
/// if(zaszuk!=nullptr) {
/// zaszuk->next=trzy->next;
/// } else {
/// pocz=trzy->next;
/// }
/// jeden->next=trzy->next;
if(last==l.getFirst()) {
for(int i=0; i<3; i++)
l.pop();
last=a=l.getFirst();
} else if(trzy->next!=nullptr) {
delete jeden;
delete dwa;
last->next=trzy->next;
a=last->next;
delete trzy;
} else {
delete jeden;
delete dwa;
delete trzy;
a=last->next=nullptr;
break;
}
/// ileJest=ileJest-3;
/// zaszuk=trzy;
/// a=zaszuk->next;
}
}
}
#ifndef LIST__H
#define LIST__H
#include <iostream>
class list_ {
protected:
struct element;
public:
virtual ~list_();
list_()=default; // last=nullptr;
list_(list_ & l)=delete;
list_ & operator = (list_ & l)=delete;
inline void push(int data);
inline void pop();
int size()const;
bool empty()const {return first?0:1;}
void print();
element* getFirst() {
return first;
}
void setFirst(element * node) {
first=node;
}
//////////////////////////////////////////////private/
protected:
int length=0;
// c++ 11
element* first=nullptr;
element* last=nullptr; //c++ 11
/////////////////////////////////////////////private::implementation/
struct element {
element(int set_data):data(set_data) {}
int data;
element* next=nullptr;
~element() {
std::cout<<"(del "<<data<<") ";
}
};
};
void list_:: push(int a) {
if(last) {
last->next=new element(a);
last=last->next;
} else {
last=new element(a);
first=last;
}
length++;
}
void list_:: pop() {
if(first!=last) {
element* temp=first;
first = first ->next;
delete temp;
length--;
} else if(first) {
last=nullptr;
delete first;
first=nullptr;
length--;
} else {
std::cout<<"lista jest pusta"<<std::endl;
}
}
#endif // LIST__H
#ifndef SZNUR_H
#define SZNUR_H
#include "list_.h"
class sznur : list_ {
public:
sznur();
virtual ~sznur();
void usun();
void dodaj();
void print() {l.print();}
element * first = l.getFirst();
element * curent = first;
// protected:
private:
list_ l;
};
#endif // SZNUR_H