Advertisement
Gilgamesh858

doubleLinkedList.cpp

Jun 7th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename N> class Node{
  6.     private:
  7.         N key;
  8.         Node<N> *next, *prev;
  9.  
  10.     public:
  11.         Node(N key){
  12.             this->key = key;
  13.             next = prev = NULL;
  14.         }
  15.         int getKey() { return key; }
  16.         Node<N>* getNext() { return next; }
  17.         void setNext(Node<N>* next) { this->next = next; }
  18.         Node<N>* getPrev() { return prev; }
  19.         void setPrev(Node<N>* prev) { this->prev = prev; }
  20. };
  21.  
  22. template <typename L> class List{
  23.     private:
  24.         Node<L>* head, *tail;
  25.         int size;
  26.  
  27.         Node<L>* _search(L x){
  28.             Node<L>* temp = head;
  29.  
  30.             while(temp!=NULL)
  31.             {
  32.                 if(temp->getKey() == x)
  33.                     return temp;
  34.                 temp = temp->getNext();
  35.             }
  36.             return NULL;
  37.         }
  38.     public:
  39.         List(){
  40.             head = tail = NULL;
  41.             size = 0;
  42.         }
  43.        
  44.         int isEmpty() {
  45.             if(!head)
  46.                 return 1;
  47.             return 0;
  48.         }
  49.  
  50.         int getSize() { return size; }
  51.  
  52.         int search(L x){
  53.             if(_search(x))
  54.                 return 1;
  55.             return 0;
  56.         }
  57.  
  58.         List<L>* insertInHead(L x){
  59.             Node<L>* temp = new Node<L>(x);
  60.             size++;
  61.             if(!head){
  62.                 head = tail = temp;
  63.                 return this;
  64.             }
  65.             temp->setNext(head);
  66.             head->setPrev(temp);
  67.             head = temp;
  68.             return this;
  69.         }
  70.  
  71.         List<L>* insertInTail(L x){
  72.             if(!head){
  73.                 return insertInHead(x);
  74.             }
  75.             Node<L>* temp = new Node<L>(x);
  76.             size++;
  77.             tail->setNext(temp);
  78.             temp->setPrev(tail);
  79.             tail = temp;
  80.             return this;
  81.         }
  82.  
  83.         void del(L x){
  84.             if(!search(x)){
  85.                 cout << "Elemento Inesistente" << endl;
  86.                 return;
  87.             }
  88.             size--;
  89.             Node<L>* temp = _search(x);
  90.             if(temp == head){
  91.                 head = temp->getNext();
  92.                 delete(temp);
  93.                 return;
  94.             }
  95.             Node<L>* previous = temp->getPrev();
  96.             previous->setNext(temp->getNext());
  97.             Node<L>* temp2 = temp->getNext();
  98.             if(temp2)
  99.                 temp2->setPrev(previous);
  100.             delete(temp);
  101.         }
  102.  
  103.         void print(){
  104.             if(isEmpty()){
  105.                 cout << "La Lista è vuota!" << endl;
  106.                 return;
  107.             }
  108.             Node<L>* temp = head;
  109.             while(temp){
  110.                 cout << temp->getKey() << " ";
  111.                 temp = temp->getNext();
  112.             }
  113.             cout << endl;
  114.         }
  115.  
  116. };
  117.  
  118. int main(){
  119.     List<int>* T = new List<int>();
  120.     T->insertInHead(5)->insertInTail(567)->insertInHead(6)->insertInTail(8)->insertInHead(45);
  121.     T->print();
  122.     T->del(6);
  123.     T->print();
  124.     T->del(8);
  125.     T->print();
  126.     T->del(5);
  127.     T->print();
  128.     T->del(567);
  129.     T->print();
  130.     T->del(45);
  131.     T->print();
  132.  
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement