d
BAN USERfor first part - delete all elements starting from 't'th node to end of list
for second part - delete elements starting from 't'th node to end
now only t-1 elements are remaining, delete the first node from that list - since a traversal of t nodes from 1st node on a list of length t-1 would end at 1st node
From there on follow simple traversal and delete nodes.
Wondering if the last part can be generalized
class Node {
public:
Node * left;
Node * right;
Node * parent;
};
leftflag = 0;
rightflag = 0;
Traverse thru right subtree of y and search for z, if found set right flag as 1
Else traverse thru parent/grandparent of y until the parent node is not root
For each parent/grandparent greater than y - Search thru the right subtree of parent
even at root - search thru right subtree of root. If z found set rightflag as 1
If right flag is 0 exit with FALSE
Else Traverse thru left subtree of y and search for x, if found set left flag as 1
Else traverse thru parent/grandparent of y until the parent node is not root
For each parent/grandparent less than y - Search thru the left subtree of parent
even at root - search thru left subtree of root. If x found set leftflag as 1
if leftflag = 0, exit with FALSE status
exit with TRUE status
I think we can use visitor pattern here - Stress class is the visitor, furniture class is the object and chair n table extend it
Furniture class has implementation pointer to material class (wood, metal extend them)
Now in ur test function u can pass furniture ptr and stress ptr
The use of this pattern is that we can increase the instances of stress class easily
forgot to put the idea - when u insert the elements into set (binary tree) - it sorts the elements according to default predicate, and fails to enter the duplicate element in set and when you iterate both the sets, and compares the element at ith position in one set to element of ith element in 2nd - if any element is different, the function fails
- d May 15, 2010Lets say before the table got corrupted it had 2 files
file A spanned from node 45 thru 1 to node 56 and had the foll entry in table before it got corrupted
A -- 8->9->1->11->3->12->NULL
file B spanned from node 23 thru 2 to node 5 and had the entry in table before it got corrupted as
B -- 10->7->6->4->2->15->5->NULL
The idea is to start from node 1 and traverse the list that passed thru node 1 (if node 1 is not empty)
Maintain two sets -
lets say set "X" for storing all the nodes encountered and
set "Y" for storing the head node of all the lists traversed.
since we started from 1 (which is non null), the current head of list thru node 1 is 1
continue the foll steps till node n
for node i starting from 1 till n-
check if "i" is not null
if "i" exists in set X
then continue to next node
else
add i to set Y
traverse thru the linked list that passes thru node "i"
for each node ("j") encountered in the list check
if the node "j" exists in set Y
merge the current list with list starting from "j"
- remove j from set Y, since the list whose head was "j"
will have its head as "i"
else
add the node to set X
else
move to next node
construct new table by iterating thru nodes in set Y and list them against dummy file name
following the example
starting it from node 1 to node 15
1st iter - list would be 1->11->3->12->NULL
set X - 1, 11, 3, 12
set Y - 1
2nd iter - list would be 2->15->5->NULL
set X - 1, 11, 3, 12, 2, 15, 5
set Y - 1, 2
3rd iter - 3 is available in set X so move to node 4
4th iter - list would be 4->2->15->5->NULL
set X - 1, 11, 3, 12, 2, 15, 5, 4
set Y - 1, 4
5th iter - 5 is available in set X so move to node 6
6th iter - list would be 6->4->2->15->5->NULL
set X - 1, 11, 3, 12, 2, 15, 5, 4, 6
set Y - 1, 6
7th iter - list is 7->6->4->2->15->5->NULL
set X - 1, 11, 3, 12, 2, 15, 5, 4, 6, 7
set Y - 1, 7
8th iter - list is 8->9->1->11->3->12->NULL
set X - 1, 11, 3, 12, 2, 15, 5, 4, 6, 7, 8, 9
set Y - 8, 7
9th iter - 9 is in set X continue to 10
10th iter - list is 10->7->6->4->2->15->5->NULL
set X - 1, 11, 3, 12, 2, 15, 5, 4, 6, 7, 8, 9, 10
set Y - 8, 10
11th, 12th, - 11, 12 is in set X
13th, 14th are NULL continue to 15
15th is in set X
Lets say before the table got corrupted it had 2 files
file A spanned from node 45 thru 1 to node 56 and had the foll entry in table before it got corrupted
A -- 8->9->1->11->3->12->NULL
file B spanned from node 23 thru 2 to node 5 and had the entry in table before it got corrupted as
B -- 10->7->6->4->2->15->5->NULL
The idea is to start from node 1 and traverse the list that passed thru node 1 (if node 1 is not empty)
Maintain two sets -
lets say set "X" for storing all the nodes encountered and
set "Y" for storing the head node of all the lists traversed.
since we started from 1 (which is non null), the current head of list thru node 1 is 1
continue the foll steps till node n
for node i starting from 1 till n-
check if "i" is not null
if "i" exists in set X
then continue to next node
else
add i to set Y
traverse thru the linked list that passes thru node "i"
for each node ("j") encountered in the list check
if the node "j" exists in set Y
merge the current list with list starting from "j"
- remove j from set Y, since the list whose head was "j"
will have its head as "i"
else
add the node to set X
else
move to next node
construct new table by iterating thru nodes in set Y and list them against dummy file name
following the example
starting it from node 1 to node 15
1st iter - list would be 1->11->3->12->NULL
set X - 1, 11, 3, 12
set Y - 1
2nd iter - list would be 2->15->5->NULL
set X - 1, 11, 3, 12, 2, 15, 5
set Y - 1, 2
3rd iter - 3 is available in set X so move to node 4
4th iter - list would be 4->2->15->5->NULL
set X - 1, 11, 3, 12, 2, 15, 5, 4
set Y - 1, 4
5th iter - 5 is available in set X so move to node 6
6th iter - list would be 6->4->2->15->5->NULL
set X - 1, 11, 3, 12, 2, 15, 5, 4, 6
set Y - 1, 6
7th iter - list is 7->6->4->2->15->5->NULL
set X - 1, 11, 3, 12, 2, 15, 5, 4, 6, 7
set Y - 1, 7
8th iter - list is 8->9->1->11->3->12->NULL
set X - 1, 11, 3, 12, 2, 15, 5, 4, 6, 7, 8, 9
set Y - 8, 7
9th iter - 9 is in set X continue to 10
10th iter - list is 10->7->6->4->2->15->5->NULL
set X - 1, 11, 3, 12, 2, 15, 5, 4, 6, 7, 8, 9, 10
set Y - 8, 10
11th, 12th, - 11, 12 is in set X
13th, 14th are NULL continue to 15
15th is in set X
bool isValid(char *p) {
- d May 26, 2010int i = 0;
while (*(p+i)) {
if (((i & 1) == 0) && (*(p + i) != '(')) {
return 0;
}
if (((i & 1) != 0) && (*(p + i) != ')')) {
return 0;
}
++i;
}
return 1;
}