Advertisement
nex036ara

Lista

Dec 9th, 2011
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.11 KB | None | 0 0
  1. //lista.hpp
  2.  
  3.  
  4.  
  5. #ifndef LISTA_DEF
  6. #define LISTA_DEF
  7. #include <stdlib.h>
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. template <class T>
  12.  
  13. class List{
  14.     private:
  15.         struct listEl{
  16.  
  17.         T content;
  18.         struct listEl *next;
  19.         };
  20.         int noEl;
  21.         listEl *tail;
  22.         listEl *head;
  23.     public:
  24.         List() {tail=NULL; head=NULL; noEl=0;}
  25.         List(const List<T>&);
  26.         virtual ~List();
  27.  
  28.         List<T>& operator=(const List<T>&);
  29.         int getSize()const {return noEl;}
  30.         void clear();
  31.  
  32.         bool empty()const{return head==NULL? 1:0;}
  33.         bool add(int, const T&);
  34.         bool remove(int);
  35.         bool read(int, T&)const;
  36.     };
  37.  
  38.  
  39. template <class T>
  40.    ostream& operator<<(ostream &out, const List<T> &ref){
  41.  
  42.        out<<"-------"<<endl;
  43.        for(int i=1; i<=ref.getSize(); i++){
  44.        out<<" ";
  45.        T res; //npr int rezultat
  46.        ref.read(i,res);//sa i-te pozicije ocitaj sadrzaj i upisi u res
  47.        out<<res;
  48.        } out<<"------";
  49.        return out;
  50.  
  51.  
  52.    }
  53.        template <class T>
  54.        List<T>:: List(const List<T> &ref){
  55.            tail=NULL;
  56.            head=NULL;
  57.            noEl=0;
  58.             for(int i=1; i<=ref.noEl; i++)
  59.                {
  60.  
  61.                 T res; //int element
  62.                 if(ref.read(i,res))
  63.                 add(i,res);}
  64.  
  65.         }
  66.  
  67.         template <class T>
  68.         List<T>:: ~List(){
  69.             while(!empty())
  70.                     remove(1);
  71.         }
  72.  
  73.         template <class T>
  74.         List<T>& List<T>::operator=(const List<T> &ref){
  75.             if(this!= &ref) {
  76.                 clear();
  77.                 tail=NULL;
  78.                 head=NULL;
  79.                 noEl=0;
  80.                 for(int i=1; i<ref.noEl; i++){
  81.                     T res;
  82.                     if(ref.read(i,res)) add(i,res);
  83.                 }
  84.  
  85.  
  86.             }return *this;
  87.  
  88.         }
  89.  
  90.         template <class T>
  91.         void List<T>::clear(){
  92.             while(!empty()) remove(1);
  93.         }
  94.  
  95.         template <class T>
  96.         bool List<T>:: add(int n, const T &ref_value){
  97.             bool isempty = empty(); //probaj i bez isempty
  98.  
  99.             if(n<1 || ( !isempty && n>noEl+1))
  100.             return false;
  101.                 else
  102.                 {
  103.                     listEl *newEl = new listEl;
  104.                         if(newEl==NULL)
  105.                         return false;
  106.                     else {
  107.  
  108.                         newEl->content = ref_value;
  109.                         if(n==1){
  110.                         newEl->next = head;
  111.                         head = newEl;
  112.                         }
  113.                         else if(n==noEl+1){
  114.  
  115.                         newEl->next = NULL;
  116.                         tail->next = newEl;
  117.                         }
  118.                         else {
  119.                         listEl *tmp = head;
  120.                         for(int i=2; i<n; i++)
  121.                         tmp = tmp->next;
  122.                         newEl->next = tmp->next; //umesto =tmp.next probaj tmp
  123.                         tmp->next = newEl;
  124.                         }
  125.                         noEl++;
  126.                 if(newEl->next==NULL) tail=newEl;
  127.         return true;
  128.  
  129.                 }
  130.     }
  131.  
  132. }
  133.  
  134.  
  135.  
  136.         template <class T>
  137.         bool List<T>:: remove(int n){
  138.             if(empty() || n<1 || n>noEl) return false;
  139.  
  140.                else {
  141.                 if(n==1) {
  142.                 //kada se uklanja sa prve pozicije poenta je da se promeni adresa od head,
  143.                 //a zatim izvrsi brisanje
  144.                 listEl *tmp = head;
  145.                 head = head->next;
  146.                 delete tmp;
  147.                 noEl--;
  148.                 }
  149.             else {
  150.                 listEl *tmp = head;
  151.                 for(int i=2; i<n; i++)
  152.                 tmp = tmp->next;
  153.                 listEl *del = tmp->next;
  154.                 tmp->next = del->next;
  155.                 if(tail==del)
  156.                         tail = tmp;
  157.                     delete del;
  158.                 noEl--;
  159.             }
  160.             return true;
  161.             }
  162.         }
  163.  
  164.         template <class T>
  165.         bool List<T>:: read(int n, T& ref_value)const{
  166.             if(empty() || n<1 || n>noEl) return false;
  167.                 else {
  168.                     if(n==1) ref_value = head->content;
  169.                     else
  170.                     if(n==noEl) ref_value = tail->content;
  171.                     else {
  172.                     listEl *tmp = head;
  173.                         for(int i=1; i<=n; i++)
  174.                             tmp = tmp->next;
  175.                             ref_value = tmp->content;
  176.                         }return true;
  177.  
  178.                 }
  179.  
  180.         }
  181.  
  182. #endif
  183.  
  184.  
  185.  
  186.  
  187. //main.cpp
  188.  
  189.  
  190.  
  191.  
  192. #include "lista.hpp"
  193.  
  194.  
  195. int main(){
  196.  
  197.     List<int> list1;
  198.  
  199.     list1.clear();
  200. cout<<"empty? "<<list1.empty()<<endl;
  201.  
  202.  
  203.     list1.add(1,21);
  204.     list1.add(2,24);
  205.     cout<<list1<<endl;
  206.     cout<<"size: "<<list1.getSize()<<endl;
  207.  
  208. cout<<"empty? "<<list1.empty()<<endl;
  209.  
  210.  
  211.  
  212.  
  213.   //  if(list1.empty() )  cout<<"jeste"<<;
  214.    // else
  215.    // cout<<"nije"<<endl;
  216.  
  217.  
  218.      //   cout<<list1<<endl;
  219.  
  220.  
  221.  
  222.  
  223. return 0;
  224. }
  225.  
  226.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement