sudhanshu97gupta
BAN USERMy implementation in C.
#include <stdio.h>
#include <stdlib.h>
struct node {
int val;
struct node* next;
};
struct node* createNode(int val) {
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->val = val;
newNode->next = NULL;
return newNode;
}
struct node *sortlist(struct node *root) {
if(root == NULL || root->next == NULL)
return root;
struct node *ptrFront=root, *ptrBack=NULL, *ptr=root;
struct node *HEAD = createNode(-1);
while(ptr->next != NULL) {
if(ptr->val > ptr->next->val) {
ptrBack = ptr->next;
ptr->next = NULL;
break;
}
ptr = ptr->next;
}
ptr = HEAD;
while(ptrFront != NULL && ptrBack != NULL) {
if(ptrFront->val > ptrBack->val) {
ptr->next = ptrBack;
ptrBack = ptrBack->next;
} else {
ptr->next = ptrFront;
ptrFront = ptrFront->next;
}
ptr = ptr->next;
}
while(ptrFront != NULL) {
ptr->next = ptrFront;
ptrFront = ptrFront->next;
ptr = ptr->next;
}
while(ptrBack != NULL) {
ptr->next = ptrBack;
ptrBack = ptrBack->next;
ptr = ptr->next;
}
HEAD = HEAD->next;
return HEAD;
}
void printList(struct node* HEAD) {
struct node* ptr = HEAD;
while(ptr != NULL) {
printf("%d ", ptr->val);
ptr = ptr->next;
}
return;
}
int main()
{
struct node* root = createNode(1);
root->next = createNode(2);
root->next->next = createNode(3);
root->next->next->next = createNode(1);
root->next->next->next->next = createNode(2);
struct node* HEAD = sortlist(root);
printList(HEAD);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct node {
int val;
struct node* left;
struct node* right;
};
void rec_calcDiff(struct node* root, int height, long int* evenSum, long int* oddSum) {
if(root == NULL)
return;
rec_calcDiff(root->left, height+1, evenSum, oddSum);
rec_calcDiff(root->right, height+1, evenSum, oddSum);
if(height%2 == 0)
*evenSum += root->val;
else
*oddSum += root->val;
return;
}
long int calcDiff(struct node* root) {
int height=1;
long int evenSum=0, oddSum=0;
rec_calcDiff(root, height, &evenSum, &oddSum);
return (oddSum - evenSum);
}
struct node* createNode(int val) {
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->val = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
main()
{
struct node* root = createNode(7);
root->left = createNode(8);
root->right = createNode(4);
root->left->left = createNode(33);
root->left->right = createNode(77);
root->right->right = createNode(3);
printf("%ld\n", calcDiff(root));
return 0;
}
Repdeloresrdaniels, Cloud Support Associate at JDA
Crossed the country marketing wool in Nigeria. Was quite successful How to Get Girlfriend Back By Vashikaran Mantra . Spent a ...
RepRoyaltyAd is the top digital marketing company. We have a team of SEO experts that helps you to maximise your ...
I am assuming that getNext() and hasNext() is given. Giving only the getUniqueCharacter() Implementation.
- sudhanshu97gupta October 14, 2013