notes71

# notes71 - typedef EntryType int; class QueueArray :public...

This preview shows pages 1–3. Sign up to view the full content.

typedef EntryType int; class QueueArray :public Queue{ int *array; int max; int head; int tall; int n; public: QueueArray (int max){ _max = max; _head = 0; _tail = 0; _n = 0; _array = new int[max]; } void add(int element){ assert(_n < _max); _array[_tail] = element; _tail = (_tail+1)%_max; _n++; } int remove(){ asert(_n > 0); int tmp = _array[_head]; _head = (_head+1)%_max; _n--; return tmp; } } For Linked List:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
-Removing a node from a single linked list takes time O(n). You need to find the node previous to current and then make previous.next = current.next. struct Node{ int val; Node *_next; void removeNode(Node *p){ if(_head = = p){ _head=p->_next; return; } Node *g = _head; g = g->_next; } Double-Linked Lists -Therfore removing an arbitrary element "p" takes time O(n) -Double-linked lists allow removal of an arbeitrary itme p in O(1) -The nodes on double-linked lists have pointers to the previous and the next nodes -First and last nodes are not used to store values. "Sentinel" nodes are used to simplfy
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue.

### Page1 / 5

notes71 - typedef EntryType int; class QueueArray :public...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online