Sem
BAN USERSimple and sweet
function printSorted($node1, $node2) {
$current1 = $node1;
$current2 = $node2;
$stack1 = new Stack();
$stack2 = new Stack();
while (true) {
if ($current1 || $current2) {
if ($current1) {
$stack1->push($current1);
$current1 = $current1->left;
}
if ($current2) {
$stack2->push($current2);
$current2 = $current2->left;
}
continue;
}
if ($stack1->isEmpty() && $stack2->isEmpty()) {
break;
}
if ($stack1->top() < $stack2->top()) {
$next = $stack1->pop();
$current1 = $next->right;
} else {
$next = $stack2->pop();
$current2 = $next->right;
}
echo $next->data . "\t";
}
}
void spiralPrint(int m, int n, int a[R][C])
{
int i, k = 0, l = 0;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator
*/
while (k < m && l < n)
{
/* Print the first row from the remaining rows */
for (i = l; i < n; ++i)
{
printf("%d ", a[k][i]);
}
k++;
/* Print the last column from the remaining columns */
for (i = k; i < m; ++i)
{
printf("%d ", a[i][n-1]);
}
n--;
/* Print the last row from the remaining rows */
if ( k < m)
{
for (i = n-1; i >= l; --i)
{
printf("%d ", a[m-1][i]);
}
m--;
}
/* Print the first column from the remaining columns */
if (l < n)
{
for (i = m-1; i >= k; --i)
{
printf("%d ", a[i][l]);
}
l++;
}
}
}
function foo($node) {
$queue = array();
$queue[0] = array();
$i = 0;
$queue[$i][] = $node;
while (true) {
if (count($queue[$i]) == 0) {
break;
}
$n = $i ? 0 : 1;
$queue[$n] = array();
while (count($queue[$i]) > 0) {
$entry = array_pop($queue[$i]);
$queue[$n][] = $entry;
echo $entry . "\t";
}
echo "\n";
$i = $n;
}
}
function foo($node) {
$queue = array();
$queue[0] = array();
$i = 0;
$queue[$i][] = $node;
while (true) {
if (count($queue[$i]) == 0) {
break;
}
$n = $i ? 0 : 1;
$queue[$n] = array();
while (count($queue[$i]) > 0) {
$entry = array_pop($queue[$i]);
$queue[$n][] = $entry;
echo $entry . "\t";
}
echo "\n";
$i = $n;
}
}
What if Hash table maintains frequency as well as an array of nodes in the link list corresponding to HashKey. so when we push we either create a new node which becomes tail or if hash key exists we insert this node in the existing list (anywhere).
lemme know your thoughts on this
Boolean satisfiability problem, NP Complete
- Sem April 09, 2012