UNIVERSITETI POLITEKNIK I TIRANES FAKULTETI I TEKNOLOGJISE DHE INFORMACIONIT DEPARTAMENTI ELEKTRONIKES DHE TELEKOMUNIKACIONIT Punë Laboratori nr 2 Tema : Listat e lidhura me pointera Lënda: Algoritmikë dhe Programim i Avancuar Punoi: Darla Zhuka Drejtimi: Elektronike 2B Kodi ne C++ Linked_list.cpp #include "Linked_list.h" using namespace std; template< class T > Linked_list< T >::Linked_list() { _pHead = new List_node<T>; _pHead->_pNext = _pHead; _pHead->_pPrev = _pHead; _length = 0; } template< class T > Linked_list< T >::Linked_list(const Linked_list<T>& L) { _length = L.size(); _pHead = new List_node<T>; _pHead->_pNext = _pHead; _pHead->_pPrev = _pHead; if (!L.empty()) { position p = L.last(); for (int i=0; i <= L._length; i++) { insert(L.read(p), begin()); p = L.previous(p); } } } template< class T > Linked_list< T >::~Linked_list() { while(!empty()) erase(begin()); delete _pHead; } template< class T > void Linked_list< T >::create() { if (empty()) _length = 0; } template< class T > bool Linked_list< T >::empty() const { return(_pHead == _pHead->_pNext); } template< class T > typename Linked_list< T >::position Linked_list< T >::begin() const { return (_pHead->_pNext); } template< class T > typename Linked_list< T >::position Linked_list< T >::last() const { return (_pHead->_pPrev); } template< class T > typename Linked_list< T >::position Linked_list< T >::next(position p) const { return(p->_pNext); } template< class T > typename Linked_list< T >::position Linked_list< T >::previous(position p) const { if (p != _pHead) return(p->_pPrev); } template< class T > bool Linked_list< T >::end(position p) const { return(p == _pHead); } template< class T > typename Linked_list< T >::value_type Linked_list< T >::read(position p) const { if (!end(p)) return(p->_value); } template< class T > void Linked_list< T >::write(const value_type &a, position p) { if (!end(p)) p->_value = a; } template< class T > void Linked_list< T >::insert(const value_type &a, position p) { position t = new List_node<T>; t->_value = a; t->_pNext = p; t->_pPrev = p->_pPrev; p->_pPrev->_pNext = t; p->_pPrev = t; _length++; } template< class T > void Linked_list< T >::erase(position p) { if (!empty() && !end(p)) { p->_pPrev->_pNext = p->_pNext; p->_pNext->_pPrev = p->_pPrev; delete p; } } template<class T> Linked_list<T>& Linked_list<T>::operator=(const Linked_list<T>& L) { if (this != &L) { _length = L.size(); _pHead = new List_node<T>; _pHead->_pNext = _pHead; _pHead->_pPrev = _pHead; //cout << L.empty(); //cout << L.size(); if (!L.empty()) { position p = L.last(); for (int i=0; i < L.size(); i++) { //cout << i, L.read(p); insert(L.read(p), begin()); p = L.previous(p); } } } return *this; } template<class T> bool Linked_list<T>::operator==(const Linked_list<T> &L) const { if (L.size() != _length) return false; position p, pL; p = begin(); pL = L.begin(); while (!end(p)) { if (p->_value != pL->_value) return false; p = p->_pNext; pL = pL->_pNext; } return true; } template class Linked_list<int>; template class Linked_list<float>; template< class T > typename Linked_list< T >::position Linked_list< T >::gjej(const value_type &a) { int count=0; position *p; _pHead=begin();; for(int i=0;i<_length;i++){ if(_pHead->_value==a){ count++; *p=_pHead; // cout<<*p<<endl; } // cout<<_pHead->_value<<endl; _pHead=_pHead->_pNext ; } cout<<"Vlera "<<a<<" gjendet "<<count<<" here ne liste."; return *p; } template< class T > void Linked_list< T >::rendit(){ position p=begin();; _pHead=begin(); value_type a; for(int i=0;i<_length;i++){ for(int j=i;j<_length-1;j++){ _pHead=_pHead->_pNext; if(_pHead->_value<p->_value){ a=_pHead->_value; _pHead->_value=p->_value; p->_value=a; } } p=p->_pNext; _pHead=p; } } template< class T > void Linked_list< T >::bashkangjit(const Linked_list<T> &list){ position p=list.begin(); while(!list.end(p)){ _pHead=last(); _pHead=_pHead->_pNext; insert(p->_value,_pHead->_pNext); p=p->_pNext; } rendit(); } Linked_list.h #ifndef _LINKED_LIST_H #define _LINKED_LIST_H #include <iostream> using namespace std; template <class T> struct List_node{ T _value; List_node<T> * _pPrev; List_node<T> * _pNext; }; template<class T> class Linked_list { public: typedef T value_type; typedef List_node<T>* position; // konstruktoret Linked_list(); Linked_list(int); // konstruktori i kopjes Linked_list(const Linked_list<T>& ); //destruktori ~Linked_list(); // operatoret void create(); bool empty() const; value_type read(position) const; void write(const value_type &, position); position begin() const; position last() const; bool end(position) const; position next(position) const; position previous(position) const; position gjej(const value_type &);// kerkesa A void rendit();// kerkesa B void bashkangjit(const Linked_list<T>&);// kerkesa C void insert(const value_type &, position); void erase(position); int size() const { return _length; }; // overload i operatoreve Linked_list<T>& operator=(const Linked_list<T>&); // operatori i vleredhenies bool operator==(const Linked_list<T> &) const; // kontrollon dy lista per barazi friend std::ostream& operator<<(ostream& os, const Linked_list<T> &l) { position p; p = l.begin(); os << "["; while (!l.end(p)) { if (p != l.begin()) os << ", " << l.read(p); else os << l.read(p); p = l.next(p); } os << "]" << endl; return os; } private: List_node<T> * _pHead; int _length; // gjatesia e listes }; #endif // _LINKED_LIST_H Main.cpp #include "Linked_list.h" #include <iostream> using namespace std; int main() { Linked_list<int> list1, list2; Linked_list<int>::position iter; int x; /* shtohen kater elementet ne listen list1 */ x = 1; list1.insert(x, list1.begin()); x = 2; list1.insert(x, list1.begin()); x = 3; list1.insert(x, list1.begin()); list1.insert(3, list1.begin()); x = 4; list1.insert(x, list1.begin()); /* afishimi i elementeve te list1 */ cout << "list1=" ; cout << list1; list2 = list1; cout <<endl<< "Pasi list2=list1, list2="; cout << list2; /* perdoret operatori i bere overload == */ if (list1 == list2) cout << "list1 == list2" << endl; else cout << "list1 != list2" << endl; list2.erase(list2.begin()); cout << "Pas fshirjes se elementit te pare, list2="; cout << list2; /* perdoret operatori i bere overload == */ if (list1 == list2) cout << "list1 == list2" << endl; else cout << "list1 != list2" << endl; cout<<endl<<"Pozicioni i fundit: "<<list1.gjej(3)<<endl; list1.rendit(); cout<<"Lista e renditur: "<<list1; list1.bashkangjit(list2); cout <<"Lista e bashkengjitur: "<<list1; }