Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //lista.hpp
- #ifndef LISTA_DEF
- #define LISTA_DEF
- #include <stdlib.h>
- #include <iostream>
- using namespace std;
- template <class T>
- class List{
- private:
- struct listEl{
- T content;
- struct listEl *next;
- };
- int noEl;
- listEl *tail;
- listEl *head;
- public:
- List() {tail=NULL; head=NULL; noEl=0;}
- List(const List<T>&);
- virtual ~List();
- List<T>& operator=(const List<T>&);
- int getSize()const {return noEl;}
- void clear();
- bool empty()const{return head==NULL? 1:0;}
- bool add(int, const T&);
- bool remove(int);
- bool read(int, T&)const;
- };
- template <class T>
- ostream& operator<<(ostream &out, const List<T> &ref){
- out<<"-------"<<endl;
- for(int i=1; i<=ref.getSize(); i++){
- out<<" ";
- T res; //npr int rezultat
- ref.read(i,res);//sa i-te pozicije ocitaj sadrzaj i upisi u res
- out<<res;
- } out<<"------";
- return out;
- }
- template <class T>
- List<T>:: List(const List<T> &ref){
- tail=NULL;
- head=NULL;
- noEl=0;
- for(int i=1; i<=ref.noEl; i++)
- {
- T res; //int element
- if(ref.read(i,res))
- add(i,res);}
- }
- template <class T>
- List<T>:: ~List(){
- while(!empty())
- remove(1);
- }
- template <class T>
- List<T>& List<T>::operator=(const List<T> &ref){
- if(this!= &ref) {
- clear();
- tail=NULL;
- head=NULL;
- noEl=0;
- for(int i=1; i<ref.noEl; i++){
- T res;
- if(ref.read(i,res)) add(i,res);
- }
- }return *this;
- }
- template <class T>
- void List<T>::clear(){
- while(!empty()) remove(1);
- }
- template <class T>
- bool List<T>:: add(int n, const T &ref_value){
- bool isempty = empty(); //probaj i bez isempty
- if(n<1 || ( !isempty && n>noEl+1))
- return false;
- else
- {
- listEl *newEl = new listEl;
- if(newEl==NULL)
- return false;
- else {
- newEl->content = ref_value;
- if(n==1){
- newEl->next = head;
- head = newEl;
- }
- else if(n==noEl+1){
- newEl->next = NULL;
- tail->next = newEl;
- }
- else {
- listEl *tmp = head;
- for(int i=2; i<n; i++)
- tmp = tmp->next;
- newEl->next = tmp->next; //umesto =tmp.next probaj tmp
- tmp->next = newEl;
- }
- noEl++;
- if(newEl->next==NULL) tail=newEl;
- return true;
- }
- }
- }
- template <class T>
- bool List<T>:: remove(int n){
- if(empty() || n<1 || n>noEl) return false;
- else {
- if(n==1) {
- //kada se uklanja sa prve pozicije poenta je da se promeni adresa od head,
- //a zatim izvrsi brisanje
- listEl *tmp = head;
- head = head->next;
- delete tmp;
- noEl--;
- }
- else {
- listEl *tmp = head;
- for(int i=2; i<n; i++)
- tmp = tmp->next;
- listEl *del = tmp->next;
- tmp->next = del->next;
- if(tail==del)
- tail = tmp;
- delete del;
- noEl--;
- }
- return true;
- }
- }
- template <class T>
- bool List<T>:: read(int n, T& ref_value)const{
- if(empty() || n<1 || n>noEl) return false;
- else {
- if(n==1) ref_value = head->content;
- else
- if(n==noEl) ref_value = tail->content;
- else {
- listEl *tmp = head;
- for(int i=1; i<=n; i++)
- tmp = tmp->next;
- ref_value = tmp->content;
- }return true;
- }
- }
- #endif
- //main.cpp
- #include "lista.hpp"
- int main(){
- List<int> list1;
- list1.clear();
- cout<<"empty? "<<list1.empty()<<endl;
- list1.add(1,21);
- list1.add(2,24);
- cout<<list1<<endl;
- cout<<"size: "<<list1.getSize()<<endl;
- cout<<"empty? "<<list1.empty()<<endl;
- // if(list1.empty() ) cout<<"jeste"<<;
- // else
- // cout<<"nije"<<endl;
- // cout<<list1<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement