rahulbns.ism
BAN USER#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void push(struct node **head_ref,int data)
{
struct node *n=(struct node *)malloc(sizeof(struct node));
n->data=data;
n->next=(*head_ref);
(*head_ref)=n;
}
void thirdList(struct node *head1,struct node *head2,struct node **head)
{
while(head1!=NULL && head2!=NULL)
{
if(head1->data<head2->data)
{
push(head,head1->data);
head1=head1->next;
}
else if(head2->data<head1->data)
{
head2=head2->next;
}
else
{
head1=head1->next;
}
}
if(head1==NULL)
return;
else if(head2==NULL)
{
while(head1!=NULL)
{
push(head,head1->data);
head1=head1->next;
}
}
}
void printList(struct node *head)
{
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
printf("\n");
}
int main()
{
struct node *head1=NULL;
struct node *head2=NULL;
struct node *head=NULL;
push(&head1,25);
push(&head1,20);
push(&head1,15);
push(&head1,7);
push(&head1,5);
push(&head2,30);
push(&head2,15);
push(&head2,5);
thirdList(head1,head2,&head);
printList(head);
return 0;
}
//C code to find Least Common Ancestor
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *newNode(int data)
{
struct node *n=(struct node *)malloc(sizeof(struct node));
n->data=data;
n->left=NULL;
n->right=NULL;
return n;
}
int covers(struct node *root,struct node *p)
{
if(root==NULL)
return 0;
else if(root==p)
return 1;
else
return (covers(root->left,p) || covers(root->right,p));
}
struct node * LCA(struct node *root,struct node *p,struct node *q)
{
if(covers(root->left,p) && covers(root->left,q))
return LCA(root->left,p,q);
if(covers(root->right,p) && covers(root->right,q))
return LCA(root->right,p,q);
return root;
}
int main()
{
struct node *root=newNode(12);
root->left=newNode(20);
root->right=newNode(24);
root->left->left=newNode(11);
root->left->right=newNode(10);
root->left->right->right=newNode(8);
struct node *ans=LCA(root,root->left->left,root->left->right->right);
printf("The LCA is %d\n",ans->data);
return 0;
}
// This is a modified in-order traversal adapted to this problem.
// prev (init to NULL) is used to keep track of previously traversed node.
// head pointer is updated with the list's head as recursion ends.
void treeToDoublyList(Node *p, Node *& prev, Node *& head) {
if (!p) return;
treeToDoublyList(p->left, prev, head);
// current node's left points to previous node
p->left = prev;
if (prev)
prev->right = p; // previous node's right points to current node
else
head = p; // current node (smallest element) is head of
// the list if previous node is not available
// as soon as the recursion ends, the head's left pointer
// points to the last node, and the last node's right pointer
// points to the head pointer.
Node *right = p->right;
head->left = p;
p->right = head;
// updates previous node
prev = p;
treeToDoublyList(right, prev, head);
}
// Given an ordered binary tree, returns a sorted circular
// doubly-linked list. The conversion is done in-place.
Node* treeToDoublyList(Node *root) {
Node *prev = NULL;
Node *head = NULL;
treeToDoublyList(root, prev, head);
return head;
}
test cases may include:
- rahulbns.ism September 19, 20121. load or stress testing: check how much weight the elevator can withstand.
2. what is the speed? too slow or too fast
3. does it stop at every floor, that it is supposed to. what is the time of stoppage?
4. safety testing: what happens if there is a power cut?
5. how much time does the gate remain open, does it close automatically after a certain time? does it open again if someone is in between the gate.
6. elevator gives some kind of signal, if the weight capacity has been exceeded....