Created
February 7, 2011 16:13
-
-
Save grk/814622 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __fine_linked_list_hpp__ | |
#define __fine_linked_list_hpp__ | |
#include "linked_list.hpp" | |
using namespace std; | |
template <typename T> | |
class fine_linked_list : public linked_list<T> | |
{ | |
using linked_list<T>::_head; | |
using linked_list<T>::node; | |
/*protected:*/ | |
//class node : private lockable | |
//{ | |
//public: | |
//node(const T& object) | |
//: _next(NULL), _object(object) | |
//{ | |
//} | |
//node() | |
//: _next(NULL) | |
//{ | |
//} | |
//~node() {} | |
//protected: | |
//node* _next; | |
//T _object; | |
//friend class fine_linked_list; | |
/*};*/ | |
public: | |
fine_linked_list() | |
{ | |
} | |
~fine_linked_list() | |
{ | |
} | |
bool add(const T& element) | |
{ | |
_head->lock(); | |
node* prev = _head; | |
node* curr = prev->_next; | |
if (curr) curr->lock(); | |
while (curr && (element > curr->_object)) | |
{ | |
prev->unlock(); | |
prev = curr; | |
curr = curr->_next; | |
if (curr) | |
curr->lock(); | |
} | |
if (curr && (element == curr->_object)) | |
{ | |
prev->unlock(); | |
curr->unlock(); | |
return false; | |
} | |
node* n = new node(element); | |
n->_next = curr; | |
prev->_next = n; | |
if (curr) | |
curr->unlock(); | |
prev->unlock(); | |
return true; | |
} | |
bool remove(const T& element) | |
{ | |
node* prev; | |
node* curr; | |
_head->lock(); | |
prev = _head; | |
curr = _head->_next; | |
if (curr) | |
curr->lock(); | |
while (curr && (element > curr->_object)) | |
{ | |
prev->unlock(); | |
prev = curr; | |
curr = curr->_next; | |
if (curr) | |
curr->lock(); | |
} | |
if (curr && (element == curr->_object)) | |
{ | |
prev->_next = curr->_next; | |
prev->unlock(); | |
curr->unlock(); | |
delete curr; | |
return true; | |
} | |
if (curr) | |
curr->unlock(); | |
prev->unlock(); | |
return false; | |
} | |
void clear() | |
{ | |
node* curr = _head->_next; | |
node* next; | |
while (curr) | |
{ | |
next = curr->_next; | |
delete curr; | |
curr = next; | |
} | |
} | |
int size() | |
{ | |
int count = -1; | |
node* curr = _head; | |
while (curr) | |
{ | |
count++; | |
curr = curr->_next; | |
} | |
return count; | |
} | |
void print() | |
{ | |
node* curr = _head; | |
while (curr) | |
{ | |
cout << "{" << curr->_object << "}"; | |
curr = curr->_next; | |
} | |
cout << endl; | |
} | |
protected: | |
//node* _head; | |
}; | |
#endif | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __linked_list_hpp__ | |
#define __linked_list_hpp__ | |
#include <iostream> | |
#include "lockable.hpp" | |
using namespace std; | |
template <typename T> | |
class linked_list | |
{ | |
protected: | |
class node : private lockable | |
{ | |
protected: | |
node* _next; | |
T _object; | |
public: | |
node(const T& object) | |
: _next(NULL), _object(object) | |
{ | |
} | |
node() | |
: _next(NULL) | |
{ | |
} | |
~node() {} | |
friend class linked_list; | |
}; | |
protected: | |
node* _head; | |
public: | |
linked_list() | |
{ | |
_head = new node(); | |
} | |
~linked_list() | |
{ | |
delete _head; | |
} | |
virtual bool add(const T& element) | |
{ | |
if (_head == NULL) | |
{ | |
_head = new node(element); | |
return true; | |
} | |
else | |
{ | |
node* prev = NULL; | |
node* curr = _head; | |
while (curr && (element > curr->_object)) | |
{ | |
prev = curr; | |
curr = curr->_next; | |
} | |
if (curr && (element == curr->_object)) | |
{ | |
return false; | |
} | |
else | |
{ | |
node* n = new node(element); | |
n->_next = curr; | |
if (prev) | |
prev->_next = n; | |
return true; | |
} | |
} | |
} | |
virtual bool remove(const T& element) | |
{ | |
node* current; | |
node* next; | |
current = NULL; | |
next = _head; | |
while (next && (element > next->_object)) | |
{ | |
current = next; | |
next = next->_next; | |
} | |
if (next && (element == next->_object)) | |
{ | |
if (current) | |
current->_next = next->_next; | |
if (next == _head) | |
_head = NULL; | |
delete next; | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
virtual void clear() | |
{ | |
node* curr = _head; | |
node* next; | |
while (curr) | |
{ | |
next = curr->_next; | |
delete curr; | |
curr = next; | |
} | |
_head = NULL; | |
} | |
virtual int size() | |
{ | |
int count = 0; | |
node* curr = _head; | |
while (curr) | |
{ | |
count++; | |
curr = curr->_next; | |
} | |
return count; | |
} | |
virtual void print() | |
{ | |
node* curr = _head; | |
while (curr) | |
{ | |
cout << "{" << curr->_object << "}"; | |
curr = curr->_next; | |
} | |
cout << endl; | |
} | |
}; | |
#endif /* __linked_list_hpp__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __lockable_hpp__ | |
#define __lockable_hpp__ | |
#include <pthread.h> | |
#include <errno.h> | |
class lockable | |
{ | |
public: | |
lockable() | |
{ | |
pthread_mutex_init(&_lockable_mutex, NULL); | |
} | |
~lockable() | |
{ | |
pthread_mutex_destroy(&_lockable_mutex); | |
} | |
void lock() | |
{ | |
pthread_mutex_lock(&_lockable_mutex); | |
} | |
void unlock() | |
{ | |
pthread_mutex_unlock(&_lockable_mutex); | |
} | |
private: | |
pthread_mutex_t _lockable_mutex; | |
}; | |
#endif /* __lockable_hpp__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "fine_linked_list.hpp" | |
fine_linked_list<int> l; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment