notes86 - 1-26-04Disadvantages of single-linked...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1-26-04Disadvantages of single-linked lists:•Deletion of the last element in the linked list requires traversing the entire list.•Also removing an intermediate element takes O(n)The header node and trailer node do not store any values. These nodes are called “dummy nodes” or sentinel nodes.The sentinel nodes simplify the operations in the list by eliminating special cases at the beginning and the end of the list.typedef int ListEntryType;struct DLinkedListNode{ListEntryType _val;DLinkedListNode *_next;DLinkedListNode *_previous;}class DLinkedList:{DLinkedListNode *_header;DLinkedListNode *_trailer;public:DLinkedList();void add Front(ListEntryType val);void add End(ListEntryType val);void removeFront(ListEntryType *pval);bool removeEnd(ListEntryType *pval);}DLinkedList(){_header = new DLinkedListNode;assert(_header != NULL);_trailer = new DLinkedListNode;assert(_trailer != null);_header->_next = _trailer;_trader->previous = _header;// These values are only intialized for completeness, but are not used_header->_previous = NULL;_trailer->_next = NULL;_header->_val = 0;_trailer->_val = 0;}void addEnd(ListEntryType val){DLinkedListNode *node = new DLinkedListNode;assert(node != NULL);node->_val = val;node->_next = _trailer;node->previous = _trailer->_previous;_trailer->_previous->_next = node;_trailer->_previous = node;}bool removeFront(ListEntryType *pval){if(_header->_next = _trailer)return false;DLinkedList *node = _header->_next;node->_next->_previous = _header;_header->_next = node->_next;*pval = node->_val;delete node;return true;}You can use the double linked list to implement the stack or the queue....
View Full 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 / 8

notes86 - 1-26-04Disadvantages of single-linked...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online