Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //lifo
- #include<iostream>
- using namespace std;
- typedef struct element {
- int info;
- struct element *next;
- struct element *prev;
- };
- class doublelist {
- private :
- int length;
- element *start,*current,*finalist;
- public :
- doublelist() { length=0; }
- int showlenght() {cout<<endl<<length;}
- int addLIFO(int thisvalue) {
- if(length == 0)
- {
- element * nou = new element;
- nou->info = thisvalue;
- start = nou;
- start->prev = NULL;
- start->next = NULL;
- finalist = start;
- length++;
- return 0;
- }
- element * nou = new element;
- nou->info = thisvalue;
- finalist->next = nou;
- finalist->next->prev = finalist;
- finalist = finalist->next;
- finalist->next = NULL;
- length++;
- return 0;
- }
- int showlist(bool direction)
- {
- if(length == 0)
- {
- return 0;
- }
- if(direction)
- {
- current = start;
- while(current != finalist->next)
- {
- cout<<current->info<<" ";
- current = current->next;
- }
- return 0;
- }
- current = finalist;
- while(current != start->prev)
- {
- cout<<current->info<<" ";
- current = current->prev;
- }
- }
- int delLIFO()
- {
- if(length == 0)
- {
- return 0;
- }
- else if(length == 1)
- {
- start->next = NULL;
- start->info = NULL;
- length --;
- return 0;
- }
- finalist->info = NULL;
- finalist = finalist->prev;
- finalist->next = NULL;
- length--;
- }
- protected :
- } somelist;
- int main()
- {
- for(int i=1;i<=10;i++)
- {
- somelist.addLIFO(i);
- somelist.showlist(1);
- cout<<endl;
- }
- for(int i = 1; i <= 10; i++)
- {
- somelist.delLIFO();
- somelist.showlist(1);
- cout<<endl;
- }
- for(int i=1;i<=10;i++)
- {
- somelist.addLIFO(i);
- somelist.showlist(0);
- cout<<endl;
- }
- for(int i = 1; i <= 10; i++)
- {
- somelist.delLIFO();
- somelist.showlist(0);
- cout<<endl;
- }
- }
- ====================================================================
- //fifo
- #include<iostream>
- using namespace std;
- typedef struct element {
- int info;
- struct element *next;
- struct element *prev;
- };
- class doublelist {
- private :
- int length;
- element *start,*current,*finalist;
- public :
- doublelist() { length=0; start=NULL;current=NULL;finalist=NULL;}
- int addFIFO(int thisvalue)
- {
- if(length == 0)
- {
- element * nou = new element;
- nou->info = thisvalue;
- finalist = nou;
- finalist->next = NULL;
- finalist->prev = NULL;
- start = current = finalist;
- length++;
- return 0;
- }
- element * nou = new element;
- nou->info = thisvalue;
- start->prev = nou;
- start->prev->next = start;
- start = start->prev;
- start->prev = NULL;
- length++;
- }
- int delFIFO()
- {
- if(length == 0)
- {
- return 0;
- }
- else if(length == 1)
- {
- start->next = NULL;
- start->prev = NULL;
- start->info = NULL;
- start = current = finalist = NULL;
- length--;
- return 0;
- }
- start->info = NULL;
- start = start->next;
- //start->prev->next = NULL;
- start->prev = NULL;
- length--;
- }
- int printLIST(bool direction)
- {
- if(length == 0)
- {
- return 0;
- }
- if(direction)
- {
- current = start;
- while(current != finalist->next)
- {
- cout<<current->info<<" ";
- current = current->next;
- }
- return 0;
- }
- current = finalist;
- while(current != start->prev)
- {
- cout<<current->info<<" ";
- current = current->prev;
- }
- }
- protected :
- };
- doublelist somelist;
- int main()
- {
- for(int i=1;i<=10;i++)
- {
- somelist.addFIFO(i);
- somelist.printLIST(1);
- cout<<endl;
- }
- for(int i = 1; i <= 10; i++)
- {
- somelist.delFIFO();
- somelist.printLIST(1);
- cout<<endl;
- }
- for(int i=1;i<=10;i++)
- {
- somelist.addFIFO(i);
- somelist.printLIST(0);
- cout<<endl;
- }
- for(int i = 1; i <= 10; i++)
- {
- somelist.delFIFO();
- somelist.printLIST(0);
- cout<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement