CMSC 341 Lists 3 DoublyLinked Lists Option add
CMSC 341 Lists 3
Doubly-Linked Lists Option: add pointer to previous node Issues – doubles number of pointers – allows immediate (O(1)) access to previous node 2
Circular Linked Lists Configurations – header in circle – header before circle – no header 3
Doubly-Linked Circular Linked List May or may not have a header Useful for applications requiring fast access to head and tail 4
Vector Implementation template <class Object> void List<Object>: : insert(const Object &x, const List. Itr<Object> &p) { if (!p. is. Past. End()) { for (i=this. last; i > p. current+1; i--) this. nodes[i+1] = this. nodes[i]; this. nodes[p. current+1] = x; } } 5
Cursor Implementation Linked list look&feel – data stored as collection of nodes: data and next – nodes allocated and deallocated without dynamic memory allocation Basic concepts – have node pool of all nodes, keep list of free ones – use pool indices in place of pointers; 0 == NULL 6
Cursor List Class template <class Object> class List { List(); // same old list public interface public: struct Cursor. Node { Cursor. Node() : next(0) {}; private: Cursor. Node(const Object &the. Element, int n) : element(the. Element), next(n) {}; Object element; int next; friend class List<Object>; friend class List. Itr<Object>; }; 7
Cursor List Class (cont. ) … private: int header; static vector<Cursor. Node> cursor. Space; static void initialize. Cursor. Space(); static int alloc(); static void free (int p); friend class List. Itr<Object>; }; 8
Cursor Initialization template <class Object> void List<Object>: : initialize. Cursor. Space() { static int cursor. Space. Is. Initialized = false; if (!cursor. Space. Is. Initialized) { cursor. Space. resize(100); for(int i=0; i < cursor. Space. size(); i++) cursor. Space[i]. next = i+1; cursor. Space[cursor. Space. size()-1]. next = 0; cursor. Space. Is. Initialized = true; } } 9
cursor. Space 10
Cursor Allocation template <class Object> void List<Object>: : alloc() { int p = cursor. Space[0]. next; cursor. Space[0]. next = cursor. Space[p]. next; return p; } template <class Object> void List<Object>: : free(int p) { cursor. Space[p]. next = cursor. Space[0]. next; cursor. Space[0]. next = p; } 11
Cursor Implementation (cont. ) template <class Object> List. Itr<Object> List<Object>: : find(const Object &x) const { int itr = cursor. Space[header]. next; while (itr!=0 && cursor. Space[itr]. element != x) itr = cursor. Space[itr]. next; return List. Itr<Object>(itr); } 12
Cursor Implementation (cont. ) template <class Object> void List<Object>: : insert(const Object &x, const List. Itr<Object> &p) { if (!p. is. Past. End()) { int pos = p. current; int tmp = alloc(); cursor. Space[tmp] = Cursor. Node(x, cursor. Space[pos]. next); cursor. Space[pos]. next = tmp; } } 13
Cursor Implementation (cont. ) template <class Object> void List<Object>: : remove(const Object &x) { List. Itr<Object> p = find. Previous(x); int pos= p. current; if (cursor. Space[pos]. next != 0) { int tmp = cursor. Space[pos]. next; cursor. Space[pos]. next = cursor. Space[tmp]. next; free (tmp); } } 14
Comparing Performance 15
- Slides: 15