Friday 15 June 2012

c++ - List L is merged to self -



c++ - List L is merged to self -

i working on c++ programme working linked lists, , have absolutely no thought meant "list l merged self". have previous knowledge linked lists, , never had merge 1 itself.

here format..

// pre: none // post: list l merged self. void linkedlist :: merge(const linkedlist& l){ node * lstcpy = null; node * head = null; // re-create list1 lstcpy node * cur = l; while (cur != null) { if (head == null) { lstcpy = new node; head = lstcpy; } lstcpy->next = new node; lstcpy = lstcpy->next; cur = cur->next; } }

i can't tell actual task is, implementing merge() function predefined signature you're given imply check if list should merged (and nil in case)

void linkedlist:merge(const linkedlist& rhs) { if(this != &rhs) /* check merging doesn't inquire same instance */ { // set actual merging code } }

the status pattern above, checking if(this != &rhs) before doing (unnecessary) operation on current instance used e.g. implementation of assignment operation

linkedlist& linkedlist:operator=(const linkedlist& rhs) { if(this != &rhs) { // assign individual class fellow member values rhs } homecoming *this; }

the point of checking status is, avoid unnecessary operations leave current instance in same state anyways.

merging list itself, won't require action, because operation idempotent. you'll skip such unnecessary operation checking not operating on same instance in 1st place.

// post: list l merged self.

the post status doesn't mean more list 1 merged self, self means current class instance (this). rules not merging same instance still apply.

c++

No comments:

Post a Comment