cqyanbo
BAN USER- 0of 0 votes
AnswersGive a min and max of an integer array, write a function to randomly return a number inside of this range, but not in the list. Also write a class that contains this function.
- cqyanbo in United States for Google New York City| Report Duplicate | Flag | PURGE
Google Software Engineer / Developer Arrays
#include<stdlib.h>
#include<stdio.h>
typedef struct node
{
struct node *next; // the reference to the next node
int data; // will store information
} node;
node* Insert(node *temp, node **h)
{
temp->next=*h; // store the address of the pointer head(second field)
h = &temp; // transfer the address of 'temp' to 'head'
return *h;
}
node * reverse( node * ptr )
{
node * temp;
node * previous = NULL;
while(ptr != NULL) {
temp = ptr->next;
ptr->next = previous;
previous = ptr;
ptr = temp;
}
return previous;
}
node* SumLists(node *a, node *b)
{
node* sum = NULL;
int carry = 0;
while (a!=NULL && b!=NULL) {
int tmp = a->data + b->data + carry;
carry = 0;
if (tmp >= 10) {
carry = 1;
tmp = tmp%10;
}
node* current = (struct node*)malloc(sizeof(node));
current->data = tmp;
sum = Insert(current, &sum);
a = a->next;
b = b->next;
}
while(a!=NULL) {
node* current = (struct node*)malloc(sizeof(node));
int tmp = a->data+carry;
if (tmp>10) {
carry = tmp%10;
}
else {
carry = 0;
}
current->data = tmp;
sum = Insert(current, &sum);
a = a->next;
}
while(b!=NULL) {
node* current = (struct node*)malloc(sizeof(node));
int tmp = b->data+carry;
if (tmp>10) {
carry = tmp%10;
}
else {
carry = 0;
}
current->data = tmp;
sum = Insert(current, &sum);
b = b->next;
}
if(carry != 0)
{
node* current = (struct node*)malloc(sizeof(node));
current->data = carry;
sum = Insert(current, &sum);
}
//sum = reverse(sum);
return sum;
}
int main()
{
struct node *head = NULL; //empty linked list
int i = 0;
int a = 0;
for (i; i<3; i++) {
struct node* temp = (struct node*)calloc(1, sizeof(struct node));
temp->data = i; // store data(first field)
head = Insert(temp, &head);
}
struct node *head1 = NULL;
i=0;
int j = 2;
for (i; i<3; i++) {
struct node* temp = (struct node*)calloc(1, sizeof(struct node));
temp->data = j+7; // store data(first field)
head1 = Insert(temp, &head1);
}
node* tmp = head;
while (tmp != NULL) {
printf("%d",tmp->data);
tmp = tmp->next;
}
printf("------\n");
tmp = head1;
while (tmp != NULL) {
printf("%d",tmp->data);
tmp = tmp->next;
}
printf("------\n");
head = reverse(head);
head1 = reverse(head1);
head = SumLists(head, head1);
printf("sum=");
while (head != NULL) {
printf("%d",head->data);
head = head->next;
}
printf("\n");
}
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
struct node *next; // the reference to the next node
int data; // will store information
} node;
node* Insert(node *temp, node **h)
{
temp->next=*h; // store the address of the pointer head(second field)
h = &temp; // transfer the address of 'temp' to 'head'
return *h;
}
node* MergePoint(struct node *a, struct node *b)
{
struct node *c = NULL;
// compare items from two arrays
while (a != NULL) {
int tmp = a->data;
while (b != NULL) {
if(b->data == tmp)
{
node* d = (struct node*)calloc(1, sizeof(struct node));;
d->data = b->data;
c = Insert(d, &c);
}
else if(b->data<tmp) {
break;
}
b = b->next;
}
a = a->next;
}
return c;
}
int main()
{
struct node *head = NULL; //empty linked list
int i = 0;
int a = 0;
for (i; i<5; i++) {
struct node* temp = (struct node*)calloc(1, sizeof(struct node));
temp->data = i; // store data(first field)
head = Insert(temp, &head);
}
struct node *head1 = NULL;
i=0;
int j = 1;
for (j; j<6; j++) {
struct node* temp = (struct node*)calloc(1, sizeof(struct node));
temp->data = j+1; // store data(first field)
head1 = Insert(temp, &head1);
}
node* c = MergePoint(head, head1);
while (head != NULL) {
printf("1st array = %d\n",head->data);
head = head->next;
}
while (head1 != NULL) {
printf("2rd array = %d\n",head1->data);
head1 = head1->next;
}
while (c != NULL) {
printf("result = %d\n",c->data);
c = c->next;
}
}
}
- cqyanbo February 15, 2015