Solution DSA Midterm Fall 2014: Statement Will Not Deduct Any Marks, Don't Write More Than Space Provided
Solution DSA Midterm Fall 2014: Statement Will Not Deduct Any Marks, Don't Write More Than Space Provided
int count=0;
if (s1.pop()==s2.pop()) count++;
return count;
𝒍𝒈𝒃 𝒏
𝒍𝒈𝒂 𝒏 = 𝒘𝒉𝒆𝒓𝒆 𝒍𝒈𝒃 𝒂 𝒊𝒔 𝒄𝒐𝒏𝒔𝒕𝒂𝒏𝒕, 𝒉𝒆𝒏𝒄𝒆 𝒍𝒈𝒂 𝒏 = 𝒄𝒍𝒈𝒃 𝒏, 𝒎𝒆𝒂𝒏𝒔 𝒕𝒉𝒆𝒚 𝒂𝒓𝒆 𝒂𝒔𝒚𝒎𝒑𝒕𝒐𝒕𝒊𝒄𝒂𝒍𝒍𝒚 𝒆𝒒𝒖𝒂𝒍,
𝒍𝒈𝒃 𝒂
𝒕𝒉𝒆𝒓𝒆𝒇𝒐𝒓𝒆 𝒍𝒐𝒈 𝒃𝒂𝒔𝒆 𝒅𝒐𝒆𝒔 𝒏𝒐𝒕 𝒎𝒂𝒕𝒕𝒆𝒓 𝒊𝒏 𝒂𝒔𝒚𝒎𝒑𝒕𝒐𝒕𝒊𝒄 𝒄𝒐𝒎𝒑𝒍𝒆𝒙𝒊𝒕𝒚
f. For doubly header circular linked list write member function to reverse list by swapping data, assume swap(Node
*t1, Node *t2) function is given? [To secure full marks ensure minimum steps], count time complexity of your
code?
for (t1=head->next, t2=head->prev;t1!=t2 && t1->prev!=t2;t1=t1->next, t2=t2->next)
swap(t1, t2);
First step has time n/2+1; whereas next line will executes n/2 times, hence total time is
n+1.
g. Given BigInteger is implemented using doubly header circular linked list, write function (without using stack) bool
has (BigInteger &b) return true if first integer is same to second or first integer is larger and has same digits on
corresponding position. Define best case & worst case and compute time complexity for each?
Best Case: Mismatch in first digit
Worst Case: No mismatch and both has n digits Best Case Worst Case
DNode *t1, *t2; 1 1
for (t1=head->p, t2=b.head->p;t1!=head && t2!=b.head;) 1 n+1
{ n
if (t1->data!=t2->data) return false; 1 n
t1=t1->prev;t2=t2->prev;
}
if (t1!=head) return true; 1
else return false;
______________ ______________
3
3n+3