Can someone translate this C code into java. #include #include struct assignnode{ int head; int

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Can someone translate this C code into java. #include #include struct assignnode{ int head; int

Post by answerhappygod »

Can someone translate this C code into java.
#include <stdio.h>#include <stdlib.h>struct assignnode{ int head; int first; int after; int before;} *PROCESS = NULL;typedef struct assignnode type;int process_count;/***************************************************************/void hierarchy_parameters() { /* declare local vars */ printf('Enter the number of processes'); scanf("%d", &process_count); PROCESS = (type*)malloc(process_count * sizeof(type)); PROCESS[0].head = 0; PROCESS[0].first = -1; PROCESS[0].after = -1; PROCESS[0].before = -1; /* for each PROCESS index i from 0 up to (but not including) maximum number*/ for (int i = 1; i < process_count; i++){ PROCESS[0].head = 0; PROCESS[0].first = -1; PROCESS[0].after = -1; PROCESS[0].before = -1; } return;} /* end of procedure *//***************************************************************/void create_process() { int a; int b = 1; int next; int child_count = 0; printf("Enter the parent process index: "); scanf("%d", &a); while (PROCESS[a].head != -1) { b++; } PROCESS.head = a; PROCESS[a].first = -1; PROCESS[a].before = -1; if (PROCESS[a].first == -1){ PROCESS[a].first = b; child_count = 1; }else { child_count = 2; next = PROCESS[a].first; while (PROCESS[next].before != -1) { next = PROCESS[next].before; child_count++; } PROCESS[next].before = b; PROCESS.after = next } printf("cr[%d]:\tcreate_processs the %dth child of PROCESS[%d] at PROCESS[%d]\n\n", a, child_count, a, b); print_process() return;} /* end of procedure *//***************************************************************/void print_process() { int i; printf("i\tHead\tFirst\tAfter\tBefore\n"); printf("-----------------------------\n"); for (i=0; i < process_count; i++){ printf("%d", i); if (PROCESS.head != -1){ printf("\t%d", PROCESS.head); } else printf("\t"); if (PROCESS.first != -1){ printf("\t%d", PROCESS.first); } else printf("\t"); if (PROCESS.after != -1){ printf("\t%d", PROCESS.after); } else printf("\t"); if (PROCESS.before != -1){ printf("\t%d", PROCESS.before); } else printf("\t"); printf(" \n"); } return;} /* end of procedure *//***************************************************************/void destroy_process(int a) { /* declare local vars */ if (a == -1) return; else { destroy_process(PROCESS[a].before); destroy_process(PROCESS[a].first) printf("PROCESS[%d]\t\n", a); PROCESS[a].head = -1; PROCESS[a].first = -1; PROCESS[a].after = -1; PROCESS[a].before = -1; } return;} /* end of procedure *//***************************************************************/void process_destroyer() { int a; printf("Enter the process whose descendants are to be destoryed: "); scanf("%d", &a); printf("de[%d]:\tdestroys all the descendants of PROCESS[%d] which includes: ", a, a); destroy_process(PROCESS[a].first); PROCESS[a].first = -1; printf("\n"); return;} /* end of procedure *//***************************************************************/void exit_program() { if (PROCESS != NULL) free(PROCESS); return;} /* end of procedure *//***************************************************************/int main() { /* declare local vars */ int choices; while (choices != 4) { printf("Process creation and destruction\n"); printf("--------------------------------\n"); printf("1) Enter parameters\n"); printf("2) Create a new child process\n"); printf("3) Destroy all descendants of a process\n"); printf("4) Quit program and free memory\n\n"); printf("Enter selection: "); scanf("%d", &choices); if (choices == 1) { hierarchy_parameters(); } else if (choices == 2) { create_process(); } else if (choices == 3) { process_destroyer(); print_process(); } else if (choices == 4) { exit_program(); } } return 1; /* indicates success */} /* end of procedure */
#include <stdio.h>
#include <stdlib.h>
struct assignnode
{
int head;
int first;
int after;
int before;
} *PROCESS = NULL;
typedef struct assignnode type;
int process_count;
/***************************************************************/
void hierarchy_parameters() {
/* declare local vars */
printf('Enter the number of processes');
scanf("%d", &process_count);
PROCESS = (type*)malloc(process_count * sizeof(type));
PROCESS[0].head = 0;
PROCESS[0].first = -1;
PROCESS[0].after = -1;
PROCESS[0].before = -1;
/* for each PROCESS index i from 0 up to (but not including) maximum number*/
for (int i = 1; i < process_count; i++){
PROCESS[0].head = 0;
PROCESS[0].first = -1;
PROCESS[0].after = -1;
PROCESS[0].before = -1;
}
return;
} /* end of procedure */
/***************************************************************/
void create_process() {
int a;
int b = 1;
int next;
int child_count = 0;
printf("Enter the parent process index: ");
scanf("%d", &a);
while (PROCESS[a].head != -1)
{
b++;
}
PROCESS.head = a;
PROCESS[a].first = -1;
PROCESS[a].before = -1;
if (PROCESS[a].first == -1){
PROCESS[a].first = b;
child_count = 1;
}else
{
child_count = 2;
next = PROCESS[a].first;
while (PROCESS[next].before != -1)
{
next = PROCESS[next].before;
child_count++;
}
PROCESS[next].before = b;
PROCESS.after = next
}
printf("cr[%d]:\tcreate_processs the %dth child of PROCESS[%d] at PROCESS[%d]\n\n", a, child_count, a, b);
print_process()
return;
} /* end of procedure */
/***************************************************************/
void print_process() {
int i;
printf("i\tHead\tFirst\tAfter\tBefore\n");
printf("-----------------------------\n");
for (i=0; i < process_count; i++){
printf("%d", i);
if (PROCESS.head != -1){
printf("\t%d", PROCESS.head);
}
else printf("\t");
if (PROCESS[i].first != -1){
printf("\t%d", PROCESS[i].first);
}
else printf("\t");
if (PROCESS[i].after != -1){
printf("\t%d", PROCESS[i].after);
}
else printf("\t");
if (PROCESS[i].before != -1){
printf("\t%d", PROCESS[i].before);
}
else printf("\t");
printf(" \n");
}
return;
} /* end of procedure */
/***************************************************************/
void destroy_process(int a) {
/* declare local vars */
if (a == -1) return;
else
{
destroy_process(PROCESS[a].before);
destroy_process(PROCESS[a].first)
printf("PROCESS[%d]\t\n", a);
PROCESS[a].head = -1;
PROCESS[a].first = -1;
PROCESS[a].after = -1;
PROCESS[a].before = -1;
}
return;
} /* end of procedure */
/***************************************************************/
void process_destroyer() {
int a;
printf("Enter the process whose descendants are to be destoryed: ");
scanf("%d", &a);
printf("de[%d]:\tdestroys all the descendants of PROCESS[%d] which includes: ", a, a);
destroy_process(PROCESS[a].first);
PROCESS[a].first = -1;
printf("\n");
return;
} /* end of procedure */
/***************************************************************/
void exit_program() {
if (PROCESS != NULL) free(PROCESS);
return;
} /* end of procedure */
/***************************************************************/
int main() {
/* declare local vars */
int choices;
while (choices != 4)
{
printf("Process creation and destruction\n");
printf("--------------------------------\n");
printf("1) Enter parameters\n");
printf("2) Create a new child process\n");
printf("3) Destroy all descendants of a process\n");
printf("4) Quit program and free memory\n\n");
printf("Enter selection: ");
scanf("%d", &choices);
if (choices == 1)
{
hierarchy_parameters();
}
else if (choices == 2)
{
create_process();
}
else if (choices == 3)
{
process_destroyer();
print_process();
}
else if (choices == 4)
{
exit_program();
}
}
return 1; /* indicates success */
} /* end of procedure */
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply