rishikantku
BAN USERvoid MergeHalfSortedList(stIntList* pHead)
{
if(NULL == pHead)
return;
stIntList* pTemp = pHead;
stIntList* pHead2 = pHead;
stIntList* pList1Last = pHead;
while(pHead2 != NULL)
{
if(pHead2->pNList != NULL && pHead2->data > pHead2->pNList->data) //condition when second list head is found
{
pList1Last = pHead2;
pHead2 = pHead2->pNList;
break;
}
pList1Last = pHead2;
pHead2 = pHead2->pNList;
}
if(pHead2 == NULL)
{
std::cout<<"\nList is already Sorted\n";
return;
}
//Now Head 2 is found Merge them into single list
stIntList* pTemp2 = pTemp;
stIntList* pRoot = NULL;
stIntList* pCurr = NULL;
pList1Last->pNList = NULL;
while((pTemp2 != NULL) && (pHead2 != NULL))
{
if(pCurr == NULL)
{
if(pTemp2->data > pHead2->data)
{
pCurr = pHead2;
pHead2 = pHead2->pNList;
}
else
{
pCurr = pTemp2;
pTemp2 = pTemp2->pNList;
}
pRoot = pCurr;
continue;
}
if(pTemp2->data > pHead2->data)
{
pCurr->pNList = pHead2;
pCurr = pCurr->pNList;
pHead2 = pHead2->pNList;
}
else
{
pCurr->pNList = pTemp2;
pCurr = pCurr->pNList;
pTemp2 = pTemp2->pNList;
}
continue;
}
if(pHead2)
{
while(pHead2)
{
pCurr->pNList = pHead2;
pCurr = pCurr->pNList;
pHead2 = pHead2->pNList;
}
}
else if(pTemp2)
{
while(pTemp2)
{
pCurr->pNList = pTemp2;
pCurr = pCurr->pNList;
pTemp2 = pTemp2->pNList;
}
}
printIntList(pRoot);
}
check for loop
- rishikantku June 20, 2013void InsertToSortedList(stIntList* pSorted, int data)
{
stIntList* pTemp = pSorted;
stIntList* pPrev = NULL;
while(pTemp != NULL)
{
pPrev = pTemp;
if(pTemp->data >= data)
{
stIntList* pNew = new stIntList;
pNew->data = data;
pNew->pNList = pTemp->pNList;
pPrev->pNList = pNew;
std::cout<<"\nInseted Succesfully "<<data<<" at "<<pNew;
break;
}
pTemp = pTemp->pNList;
}
}
void InsertToSortedList(stIntList* pSorted, int data)
{
stIntList* pTemp = pSorted;
stIntList* pPrev = NULL;
while(pTemp != NULL)
{
pPrev = pTemp;
if(pTemp->data >= data)
{
stIntList* pNew = new stIntList;
pNew->data = data;
pNew->pNList = pTemp->pNList;
pPrev->pNList = pNew;
std::cout<<"\nInseted Succesfully "<<data<<" at "<<pNew;
break;
}
pTemp = pTemp->pNList;
}
}
void MergeListAlternatively(stIntList* pList1,stIntList* pList2,stIntList*& pSorted)
{
bool bFirstIncremented = false;
stIntList* pTemp1 = pList1;
stIntList* pTemp2 = pList2;
stIntList* pCurr = NULL;
while(pTemp1 != NULL || pTemp2 != NULL)
{
if(!bFirstIncremented)
{
if(pTemp1 != NULL)
{
if(pSorted == NULL)
{
pSorted = new stIntList;
pSorted->data = pTemp1->data;
pSorted->pNList = NULL;
pCurr = pSorted;
}
else
{
stIntList* pNew = new stIntList;
pNew->data = pTemp1->data;
pNew->pNList = NULL;
stIntList* pTemp = pCurr;
pTemp->pNList = pNew;
pCurr = pNew;
}
pTemp1 = pTemp1->pNList;
}
bFirstIncremented = true;
}
if(bFirstIncremented)
{
if(pTemp2 != NULL)
{
stIntList* pNew = new stIntList;
pNew->data = pTemp2->data;
pNew->pNList = NULL;
stIntList* pTemp = pCurr;
pTemp->pNList = pNew;
pCurr = pNew;
bFirstIncremented = false;
pTemp2 = pTemp2->pNList;
}
bFirstIncremented = false;
}
}
}
void AppendNthElementsFromEndToStart(stIntList* pRoot,stIntList*& pNew, int n)
{
if(NULL == pRoot)
return;
stIntList* pCurr = NULL;
stIntList* pKthElem = pRoot;
stIntList* pPrev = NULL;
stIntList* pCurPrev = NULL;
int count = 1;
while(pKthElem != NULL)
{
pPrev = pKthElem;
pCurPrev = pCurr;
if(count < n)
{
if(NULL == pKthElem)
break;
pKthElem = pKthElem->pNList;
count++;
continue;
}
if(count == n)
{
pCurr = pRoot;
pKthElem = pKthElem->pNList;
count++;
continue;
}
pCurr = pCurr->pNList;
pKthElem = pKthElem->pNList;
}
//Now Current Node is the Nth node from reverse.
pCurPrev->pNList = NULL;
pNew = pCurr;
pPrev->pNList = pRoot;
}
void MergeUnSortedList(stIntList* pList1,stIntList* pList2,stIntList*& pSorted)
{
stIntList* pList1Temp = pList1;
stIntList* pList2Temp = pList2;
std::map<int,int> mapNoVsCount;
while(pList1Temp != NULL || pList2Temp != NULL)
{
if(pList1Temp != NULL)
{
std::map<int,int>::iterator it1 = mapNoVsCount.find(pList1Temp->data);
if(it1 != mapNoVsCount.end())
it1->second++;
else
{
std::pair<int,int> key(pList1Temp->data,1);
mapNoVsCount.insert(key);
}
pList1Temp = pList1Temp->pNList;
}
if(pList2Temp != NULL)
{
std::map<int,int>::iterator it1 = mapNoVsCount.find(pList2Temp->data);
if(it1 != mapNoVsCount.end())
it1->second++;
else
{
std::pair<int,int> key(pList2Temp->data,1);
mapNoVsCount.insert(key);
}
pList2Temp = pList2Temp->pNList;
}
}
stIntList* pMerge = NULL;
std::map<int,int>::iterator begin = mapNoVsCount.begin();
while(begin != mapNoVsCount.end())
{
for(int i = 0; i < begin->second; i++)
{
insertIntIntoList(pSorted,pMerge,begin->first);
}
begin++;
}
}
Dude you are not supposed to paste questions, as contest is still going on.
Please remove this post asap
Will it not be always a root node???
Please correct me if i am wrong
stTree* MaxSubtreeSum(stTree* pTree,int& MaxSum,stTree*& pMaxNode)
{
if(pTree == NULL)
return pMaxNode;
int TempSum = SubTreeSum(pTree);
if(TempSum > MaxSum)
{
MaxSum = TempSum;
pMaxNode = pTree;
}
MaxSubtreeSum(pTree->left,MaxSum,pMaxNode);
MaxSubtreeSum(pTree->right,MaxSum,pMaxNode);
return pMaxNode;
}
int SubTreeSum(stTree* pTree)
{
if(pTree == NULL)
return 0;
return (pTree->data + SubTreeSum(pTree->left) + SubTreeSum(pTree->right));
}
A wins the race
as speed of A = 100/t at time t
speed of B = 90/t
time taken by A = 110/100*t = 1.1t
time taken by B = 100/90*t = 1.11t
Repmariacbrister, Graphics Programmer at Graphic Systems
Hey, I am Maria and I am from Bluefield. Currently, I work as a freelancer graphic artist. From an early ...
RepGet powerful wazifa to know who did black magic. Guru Ji is the master of black magic totke, kala jadu ...
void PushZereosToEnd(int* arr,int len)
- rishikantku March 20, 2014{
int posZero = -1;
for(int i = 0; i < len; i++)
{
if(arr[i] == 0 && posZero == -1) //this is the zero index fisrt time
posZero = i;
else if(arr[i] != 0 && posZero != -1) //need to swap
{
int temp = arr[i];
arr[i] = arr[posZero];
arr[posZero] = temp;
posZero++;
}
}
for(int i = 0; i < len; i++)
std::cout<<arr[i]<<"\n";
}