Advertisement
Gilgamesh858

primaLista.cpp

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