Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- 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 <utility>
- #include <algorithm>
- using namespace std;
- struct Node
- {
- Node* prev;
- Node* next;
- double value;
- Node();
- Node(double n)
- {
- value=n;
- prev=nullptr;
- next=nullptr;
- };
- Node(const Node& n)
- {
- value=n.value;
- next=n.next;
- prev=n.prev;
- };
- // prilikom čišćenja ispisujemo adresu
- ~Node()
- {
- cout<<"Delete node at: "<<this<<endl;
- };
- //swap premješta sadržaj između dva čvora
- void swap(Node& n)
- {
- double x=n.value;
- n.value=value;
- value=x;
- };
- // ispisujemo adresu, adresu prethodnog, adresu sljedećeg te vrijednost
- void print() const
- {
- cout<<"Node at: "<<this<<", "<<"prev at: "<<&prev<<", "<<"next at: "<<&next<<", "<<"value: "<<value<<endl;
- };
- };
- class CDLL {
- protected:
- Node* head{};
- Node* tail{};
- public:
- CDLL(){head=nullptr; tail=nullptr;};
- CDLL(const CDLL& c)
- {
- CDLL l;
- Node* temp=c.head;
- while(temp!=c.tail)
- {
- if(head==nullptr and tail==nullptr)
- {
- Node *n= new Node(temp->value);
- n->next=head;
- n->prev=tail;
- head=n;
- tail=n;
- }
- else
- {
- Node *n=new Node(temp->value);
- head->prev=n;
- tail->next=n;
- n->prev=tail;
- n->next=head;
- tail=n;
- }
- temp=temp->next;
- }
- if(head==nullptr and tail==nullptr)
- {
- Node *n= new Node(temp->value);
- n->next=head;
- n->prev=tail;
- head=n;
- tail=n;
- }
- else
- {
- Node *n=new Node(temp->value);
- head->prev=n;
- tail->next=n;
- n->prev=tail;
- n->next=head;
- tail=n;
- }
- };
- // ~CDLL();
- bool empty() const
- {
- if(head==tail){return true;}
- else{return false;}
- };
- // postavi vrijednost na početak
- void prepend(double value)
- {
- if(head==nullptr and tail==nullptr)
- {
- Node *n= new Node(value);
- n->next=head;
- n->prev=tail;
- head=n;
- tail=n;
- }
- else
- {
- Node *n=new Node(value);
- head->prev=n;
- tail->next=n;
- n->prev=tail;
- n->next=head;
- head=n;
- }
- };
- // postavi vrijednost na kraj
- void append(double value)
- { if(head==nullptr and tail==nullptr)
- {
- Node *n= new Node(value);
- n->next=head;
- n->prev=tail;
- head=n;
- tail=n;
- }
- else
- {
- Node *n=new Node(value);
- head->prev=n;
- tail->next=n;
- n->prev=tail;
- n->next=head;
- tail=n;
- };
- };
- // ukloni čvor s početka
- // i vrati njegovu vrijednost
- double removeFromHead()
- {
- double v=head->value;
- Node* p=head;
- head=head->next;
- tail->next=head;
- delete p;
- return v;
- };
- // ukloni čvor s kraja
- // i vrati njegovu vrijednost
- double removeFromTail()
- {
- double v=tail->value;
- Node* p=tail;
- tail=tail->prev;
- head->prev=tail;
- delete p;
- };
- // ispisujemo adresu head-a, tail-a te sve čvorove
- void print() const
- {
- Node* temp=head;
- while(temp!=tail)
- {
- cout<<"prev at: "<<temp->prev<<", "<<"current at: "<<temp<<", next at: "<<temp->next<<", "<<"value: "<<temp->value<<endl;
- temp=temp->next;
- }
- cout<<"prev at: "<<temp->prev<<", "<<"current at: "<<temp<<", next at: "<<temp->next<<", "<<"value: "<<temp->value<<endl;
- };
- void sort()
- {
- Node* temp=head;
- int i=0;
- while(temp!=tail)
- {
- i++;
- temp=temp->next;
- }
- i++;
- for(int j=0;j<i;j++)
- {Node* temp1=head;
- while (temp1!=tail)
- {
- Node* temp2=temp1->next;
- if((temp1->value)<(temp2->value))
- {
- double v1=temp1->value;
- temp1->value=temp2->value;
- temp2->value=v1;
- }
- temp1=temp1->next;
- }
- }
- };
- };
- int main()
- {
- CDLL l;
- //cout<<l.empty()<<endl;
- l.append(5);
- l.append(2);
- l.append(8);
- l.append(4);
- l.sort();
- l.removeFromHead();
- l.print();
- //cout<<l.empty()<<endl;
- CDLL l1(l);
- l1.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement