Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include<iostream>
- #include <queue>
- #include<ctime>
- #include<cstdlib>
- using namespace std;
- class Articol{
- int prioritate;
- public:
- Articol(int prioritate=0){
- this->prioritate = prioritate;
- }
- void afisare(){
- cout<<"Articol (prioritate: "<<prioritate<<")";
- }
- friend bool operator < (const Articol &a1, const Articol &a2);
- };
- bool operator < (const Articol &a1, const Articol &a2){
- return a1.prioritate < a2.prioritate;
- }
- /** Clasa abstracta*/
- class CoadaPrioritati{
- public:
- virtual bool empty() = 0;
- virtual void push(Articol a) = 0; //Adauga in coada un element
- virtual void pop() = 0; //Sterge elementul de prioritate maxima (ultimul element daca sunt ordonate);
- virtual Articol top() = 0; //Intoarce elementul de prioritate maxima;
- };
- class CoadaPrioritatiTablou: public CoadaPrioritati{
- int n; //nr efectiv de elemente din coada
- Articol t[100]; // tabloul unde retin elementele
- public:
- CoadaPrioritatiTablou(){
- n = 0;
- }
- bool empty(){
- if(n==0)
- return true;
- else
- return false;
- //return (n == 0);
- };
- void push(Articol a){
- int i;
- if (n<100){
- //Elementul a se inseraza in tabloul deja ordonat a.i. sa se pastreze ordonarea.
- //Elementul daca avem elementele 7 9 10 si inserez 8 o sa obtinem 7 8 9 10
- for(i=n-1;i>=0 && a<t[i];i--){
- t[i+1] = t[i];
- }
- t[i+1]=a;
- n++;
- }
- }
- void pop(){ //Sterge elementul de prioritate maxima (ultimul element daca sunt ordonate);
- if (n>0){
- n--;
- }
- }
- Articol top() {//Intoarce elementul de prioritate maxima;
- if (n>0){
- return t[n-1];
- } else {
- cout<<"Eroare ";
- exit(1);
- }
- }
- };
- class CoadaPrioritatiLista: public CoadaPrioritati{
- struct Nod{
- Articol info;
- Nod* next;
- Nod(Articol info, Nod* next=NULL){
- this->info = info;
- this -> next = next;
- }
- };
- Nod *prim;
- public:
- CoadaPrioritatiLista(){
- prim = NULL;
- }
- bool empty() {
- return prim == NULL;
- }
- void push(Articol a){
- Nod *p = new Nod(a);
- if (prim == NULL){
- prim = p;
- } else {
- Nod *q = prim;
- Nod *r=NULL;
- while(q != NULL && a<q->info){
- r = q;
- q = q->next;
- }
- p->next = q;
- if (r == NULL){
- prim = p;
- } else {
- r->next = p;
- }
- }
- }
- void pop(){ //Sterge elementul de prioritate maxima (primul element daca sunt ordonate descrescator);
- if (!empty()){
- Nod *p = prim;
- prim = prim ->next;
- delete p;
- }
- }
- Articol top() {//Intoarce elementul de prioritate maxima (primul element);
- if (!empty()){
- return prim->info;
- } else {
- cout<<"Nu am ce sa scot"<<endl;
- exit(1);
- }
- }
- };
- int main()
- { Articol x;
- //priority_queue <Articol> q;
- CoadaPrioritatiTablou q;
- //CoadaPrioritatiLista q;
- q.push(Articol(1));
- q.push(Articol(8));
- q.push(Articol(3));
- q.push(Articol(4));
- q.push(Articol(3));
- cout<<"Extrag articole:"<<endl;
- while (!q.empty())
- {
- x = q.top();
- //cout << x << " ";
- x.afisare();
- cout<<endl;
- q.pop();
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment