Guest User

Untitled

a guest
Jun 12th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6.  
  7.  
  8. template <class T>
  9. class List{
  10. public:
  11. //*****************************************************//
  12.     struct el{
  13.         T value;
  14.         el *next;
  15.             el(T&w):value(w),next(NULL){};   // lista inicjalizacyjna
  16.     };
  17. //*****************************************************//
  18.  
  19. //*************************POLA************************//
  20.     el *head;    // glowa
  21. //*****************************************************//
  22.  
  23.  
  24. //*************************METODY**********************//
  25.     List(); //konstruktor
  26.     bool IsEmpty(void); // sprawdza czy pusta
  27.     void Add(T val);    // dodaje wartosc
  28.     T GetVal(int idx);   // pobeira nty element (tutaj lepiej by bylo przeciazyc []
  29.                                                 //zeby moc skorzystac z qsort
  30.  
  31.  
  32. //*****************************************************//
  33.  
  34.  
  35. };
  36.  
  37. template <class T>
  38. List<T>::List(){
  39.     head=NULL;
  40. }
  41.  
  42. template <class T>
  43. bool List<T>::IsEmpty(void){
  44.     return head==NULL;
  45. }
  46.  
  47. template <class T>
  48. void List<T>::Add(T val){
  49.     if(this->IsEmpty()){
  50.         head=new el(val);
  51.     }
  52.     else {
  53.         el* tmp=head;
  54.         while(tmp->next!=NULL) tmp=tmp->next;
  55.         el* new_el = new el(val);
  56.         tmp->next = new_el;
  57.     }
  58. }
  59.  
  60.  
  61.  
  62. template <class T>
  63. T List<T>::GetVal(int idx){
  64.     el *tmp = head;
  65.     //if((tmp->next==NULL)&&idx>0) return -1;
  66.  
  67.     while(idx){
  68.         idx--;
  69.         tmp=tmp->next;
  70.         if(idx>0&&tmp->next==NULL) return -1;
  71.     }
  72.     return tmp->value;
  73. }
  74.  
  75.  
  76. int main()
  77. {
  78.     List<int> moja_lista();
  79.  
  80.     moja_lista.IsEmpty();
  81.  
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment