Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this java program Create a Java program that performs the follo

ID: 3669696 • Letter: I

Question

I need help with this java program

Create a Java program that performs the following tasks: 1. Create two (2) stacks A. The main stack will store the randomly-generated numbers B. The temp stack will be used for temporary storage during the sort process 2. Generate twenty-five (25) random numbers (input) 3. For each randomly-generated number: A. If the main stack is empty, push the new random number onto the stack B. However, until the main stack is empty or the location of the generated number is found 1. Pop every number off of the main stack that is smaller than the random number (n rn) 2. Push the popped numbers onto the temp stack 3. If the main stack is empty or the location of the generated number has been found a. Push the new random number onto the main stack b. Pop all the numbers off of the temp stack c. Push the popped numbers onto the main stack 4. Display the results (see Example output section) A. Pop every number off of the main stack B. Display the numbers using 3 rows of 10 columns each 1. Be careful! There are not enough numbers to complete the third row Notes Display homework number and programmer's identifier at top of program output Use a Driver class with an execute method that controls the execution of the program

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

/* structure of a stack node */

struct sNode

{

   int data;

   struct sNode *next;

};

  

/* Function to push an item to stack*/

void push(struct sNode** top_ref, int new_data);

  

/* Function to pop an item from stack*/

int pop(struct sNode** top_ref);

  

/* structure of queue having two stacks */

struct queue

{

   struct sNode *stack1;

   struct sNode *stack2;

};

  

/* Function to enqueue an item to queue */

void enQueue(struct queue *q, int x)

{

   push(&q->stack1, x);

}

  

/* Function to dequeue an item from queue */

int deQueue(struct queue *q)

{

   int x;

  

   /* If both stacks are empty then error */

   if(q->stack1 == NULL && q->stack2 == NULL)

   {

      printf("Q is empty");

      getchar();

      exit(0);

   }

   /* Move elements from satck1 to stack 2 only if

       stack2 is empty */

   if(q->stack2 == NULL)

   {

     while(q->stack1 != NULL)

     {

        x = pop(&q->stack1);

        push(&q->stack2, x);

     }

   }

   x = pop(&q->stack2);

   return x;

}

  

/* Function to push an item to stack*/

void push(struct sNode** top_ref, int new_data)

{

  /* allocate node */

  struct sNode* new_node =

            (struct sNode*) malloc(sizeof(struct sNode));

  

  if(new_node == NULL)

  {

     printf("Stack overflow ");

     getchar();

     exit(0);

  }           

  

  /* put in the data */

  new_node->data = new_data;

  

  /* link the old list off the new node */

  new_node->next = (*top_ref);  

  

  /* move the head to point to the new node */

  (*top_ref)    = new_node;

}

  

/* Function to pop an item from stack*/

int pop(struct sNode** top_ref)

{

  int res;

  struct sNode *top;

  

  /*If stack is empty then error */

  if(*top_ref == NULL)

  {

     printf("Stack overflow ");

     getchar();

     exit(0);

  }

  else

  {

     top = *top_ref;

     res = top->data;

     *top_ref = top->next;

     free(top);

     return res;

  }

}

  

/* Driver function to test anove functions */

int main()

{

   /* Create a queue with items 1 2 3*/

   struct queue *q = (struct queue*)malloc(sizeof(struct queue));

   q->stack1 = NULL;

   q->stack2 = NULL;

   enQueue(q, 1);

   enQueue(q, 2);

   enQueue(q, 3);   

  

   /* Dequeue items */

   printf("%d ", deQueue(q));

   printf("%d ", deQueue(q));

   printf("%d ", deQueue(q));

  

   getchar();

}

#include<stdio.h>

#include<stdlib.h>

/* structure of a stack node */

struct sNode

{

   int data;

   struct sNode *next;

};

  

/* Function to push an item to stack*/

void push(struct sNode** top_ref, int new_data);

  

/* Function to pop an item from stack*/

int pop(struct sNode** top_ref);

  

/* structure of queue having two stacks */

struct queue

{

   struct sNode *stack1;

   struct sNode *stack2;

};

  

/* Function to enqueue an item to queue */

void enQueue(struct queue *q, int x)

{

   push(&q->stack1, x);

}

  

/* Function to dequeue an item from queue */

int deQueue(struct queue *q)

{

   int x;

  

   /* If both stacks are empty then error */

   if(q->stack1 == NULL && q->stack2 == NULL)

   {

      printf("Q is empty");

      getchar();

      exit(0);

   }

   /* Move elements from satck1 to stack 2 only if

       stack2 is empty */

   if(q->stack2 == NULL)

   {

     while(q->stack1 != NULL)

     {

        x = pop(&q->stack1);

        push(&q->stack2, x);

     }

   }

   x = pop(&q->stack2);

   return x;

}

  

/* Function to push an item to stack*/

void push(struct sNode** top_ref, int new_data)

{

  /* allocate node */

  struct sNode* new_node =

            (struct sNode*) malloc(sizeof(struct sNode));

  

  if(new_node == NULL)

  {

     printf("Stack overflow ");

     getchar();

     exit(0);

  }           

  

  /* put in the data */

  new_node->data = new_data;

  

  /* link the old list off the new node */

  new_node->next = (*top_ref);  

  

  /* move the head to point to the new node */

  (*top_ref)    = new_node;

}

  

/* Function to pop an item from stack*/

int pop(struct sNode** top_ref)

{

  int res;

  struct sNode *top;

  

  /*If stack is empty then error */

  if(*top_ref == NULL)

  {

     printf("Stack overflow ");

     getchar();

     exit(0);

  }

  else

  {

     top = *top_ref;

     res = top->data;

     *top_ref = top->next;

     free(top);

     return res;

  }

}

  

/* Driver function to test anove functions */

int main()

{

   /* Create a queue with items 1 2 3*/

   struct queue *q = (struct queue*)malloc(sizeof(struct queue));

   q->stack1 = NULL;

   q->stack2 = NULL;

   enQueue(q, 1);

   enQueue(q, 2);

   enQueue(q, 3);   

  

   /* Dequeue items */

   printf("%d ", deQueue(q));

   printf("%d ", deQueue(q));

   printf("%d ", deQueue(q));

  

   getchar();

}