Templating, Introduction to algorithm analysis, Linked list, Stacks/queues (and applications), Recursion Flashcards
Assign. 4 you constructed a sequence template that better constructed the same application. The application (driver) program involved in making the template class particularly advantageous can best be described as
It uses 2 sequences of DIFFERENT ITEM TYPES but both sequences BEHAVE EXACTLY THE SAME WAY
Whats the major flaw suffered?
Head unintentionally lost; potential null-pointer exception; stale pointer; illegal access of memory already released
void DestroyList (Node*& head)
{
Node *cursor = head, *next = 0;
while (cursor != 0)
{
next = cursor->link;
delete cursor;
cursor = next;
}
}
Stale pointer because there are other pointers that point to nodes in the list, and those pointers are not updated or invalidated when the list is destroyed
Whats the major flaw suffered?
Head unintentionally lost; potential null-pointer exception; stale pointer; illegal access of memory already released
void RemoveHead (Node*& head)
{
if (head == 0) return;
delete head;
head = head->link;
}
Illegal access of memory already released because the function deletes the ‘head’ node before updating the ‘head’ pointer to point to the next node. This means that when the ‘head’ pointer is updated after deletion, it is pointing to a memory location that has already been released.
Whats the major flaw suffered?
Head unintentionally lost; potential null-pointer exception; stale pointer; illegal access of memory already released
bool NonEmptyAndNegativelyHeaded (Node* head)
{
return head->data < 0 && head != 0;
}
Potential null-pointer exception because the function first checks if ‘head’s data’ member is less than 0 before checking if ‘head’ is a null pointer. If head is null, then accessing its data member would result in undefined behavior, including a program crash. Therefore, it is important to first check if ‘head’ is null before accessing its data member.
Whats the major flaw suffered?
Head unintentionally lost; potential null-pointer exception; stale pointer; illegal access of memory already released
void PrintList (Node*& copyOfHead)
{
while (copyOfHead != 0)
{
cout «_space;copyOfHead->data «_space;endl;
copyOfHead = copyOfHead->link;
}
}
Head unintentionally lost the function takes a reference to a pointer to the head of the list. The problem with this function is that it modifies the value of copyOfHead during its execution, which can result in the head pointer being lost or changed unintentionally. Specifically, the copyOfHead pointer is advanced to the next node each time through the loop, effectively modifying the original head pointer.
When implementing a queue with a singly-linked list (that keeps a tail pointer), it is most logical to have _______ because the other choice will not allow us to do _______
First blank:
the front at head and the rear at tail
the rear at head and the front at tail
Second blank:
front operation in O(1) time
push operation in O(1) time
pop operation in O(1) time
the front at head and the rear at tail, pop operation in O(1) time
(T/F) From problem-solving strategy recursion can be considered a more general (not more specialized) form of divide-and-conquer (or reduce-and-conquer as some preferred)
False is more specialized
(T/F) A recursive function that DOESN’T have at least a base case is likely to cause stack overflow
True
(T/F) A recursive function that DOES have at least a base case will never cause stack overflow
False
FOUR criteria important for successful application of recursion
recursively decomposible, base case(s), making progress, ultimate reachability of base case(s)
How many asterisks are printed by the function call foo (4)?
void foo (int i)
{
if (i > 0)
{
foo (i - 2);
foo (i - 2);
}
cout «_space;’ * ‘;
}
7
What could go wrong with the code?
void goo (int n)
{
if (n != 0)
{
cout «_space;n;
goo (n - 2);
}
}
stack overflow, there is no base case or condition that would terminate the recursive calls, the function will keep calling itself until the call stack overflows. Since the function is decrementing n by 2 at each recursive call, it will eventually reach negative values and continue to call itself, leading to a stack overflow.