











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












List L; data (^1) data 2 data 3
List::List() : size(0), head(NULL) { // end default constructor }
List L;
// returns a pointer to the desired node List::ListNode* List::find(int index) const { if ((index < 1) || (index > getLength())) return NULL; else { // count from the beginning of the list ListNode* cur = head; for (int skip = 1; skip < index; ++skip) cur = cur->next; return cur; } }
void List::remove(int index) throw(ListException) { ListNode* cur; if ((index < 1) || (index > getLength())) throw ListException( "ListException: remove index out of range"); else { --size; if (index == 1) { // delete the first node from the list cur = head; // save the pointer to node head = head->next; } else { listNode* prev = find(index - 1); // delete the node after the // node to which prev points cur = prev->next; //save pointer to node prev->next = cur->next; } // return node to system cur->next = NULL; delete cur; cur = NULL; } }
void List::retrieve(int index, ListItemType& dataItem) const throw(ListException) { if ((index < 1) || (index > getLength())) throw ListException( "ListException: retrieve index out of range"); else { // get pointer to node, then data in node ListNode* cur = find(index); dataItem = cur->item; } }
List& List::operator=(const List& rhs) { // check for assignment to self if (this != &rhs) { // deallocate left-hand side while (!isEmpty()) remove(1); if (rhs.head == NULL) head = NULL; else { // copy first node head = new ListNode; assert(head != NULL); // check allocation head->item = rhs.head->item; // copy rest of list ListNode* newPtr = head; // new list pointer // newPtr points to last node in new list // origPtr points to nodes in original list for (ListNode* origPtr = rhs.head->next; origPtr != NULL; origPtr = origPtr->next) { newPtr->next = new ListNode; assert(newPtr->next != NULL); newPtr = newPtr->next; newPtr->item = origPtr->item; } newPtr->next = NULL; } size = rhs.size; // copy size of list } return *this; }
List::~List() { while (!isEmpty()) remove(1); }
List::List() : size(0) { head = new ListNode; // Create the Dummy Node head->next = NULL; // end default constructor } void List::insert(int index, ListItemType newItem) throw (ListException) { int newLength = getLength() + 1; if ((index < 1) || (index > newLength)) throw ListException( "ListException: insert index out of range"); else { // create new node and place newItem in it ListNode* newPtr = new ListNode; if (newPtr == NULL) throw ListException( "ListException: insert cannot allocate memory"); else { size = newLength; newPtr->item = newItem; ListNode* prev = find(index); // insert new node after node // to which prev points newPtr->next = prev->next; prev->next = newPtr; } } }
dummy List L;
dummy List L;
List::List() : size(0) { head = new ListNode; head->next = NULL; tail = head; }
dummy data1 data2 (^) data Two_Way_Linked_List T; head
dummy Two_Way_Linked_List T; head
typedef desired-type-of-list-item listItemType; class List { public: // constructors and destructor List(); // default constructor List(const List& aList); // copy constructor ~List(); // deletes all nodes in list // list operations List& operator=(const List& aList); bool isEmpty() const; int getLength() const; void insert(int index, ListItemType newItem) throw(ListException); void remove(int index) throw(ListException); void retrieve(int index, ListItemType& dataItem) const throw(ListException); private: struct ListNode { ListItemType item; ListNode* next; ListNode* precede; }; int size; // number of items in list ListNode* head; // pointer to linked list of items ListNode* find(int index) const; // returns a pointer to the // position-th node in the // linked list }; // Default constructor: List::List() : size(0) { head = new ListNode; head->next = head; head->precede = head; }