Question

In: Computer Science

C# To write a program using a stack and a program using a queue. Code a...

C#

To write a program using a stack and a program using a queue.

Code a class encapsulating a queue of foods using a circular array. A food has the following attributes: name, the number of calories per serving and the number of servings per container. Limit your queue to 20 food items. In addition to writing the enqueue, dequeue, and peek methods, you will write two more methods: a method that returns the average calories per serving of all the foods in the queue; a method that returns the food items with the highest total calories (ie: number of calories per serving * servings per container). Write a program to test your queue with all these methods

Solutions

Expert Solution


import java.util.* ;

class Solution
{
  
static class FoodItem
{
   String name;
int calories;
int serving;
   FoodItem link;
}
  
static class Queue
{
   FoodItem front, rear;
}
  

static void enQueue(Queue q, String name, int calories, int serving)
{
FoodItem temp = new FoodItem();
   temp.name= name;
temp.calories=calories;
temp.serving= serving;
   if (q .front == null)
       q .front = temp;
   else
       q .rear .link = temp;
  
   q .rear = temp;
   q .rear .link = q .front;
}
  

static int deQueue(Queue q)
{
   if (q .front == null)
   {
       System.out.printf ("Queue is empty");
       return Integer.MIN_VALUE;
   }
  
  
   int value;
   if (q .front == q .rear)
   {
       value = q .front .data;
       q .front = null;
       q .rear = null;
   }
   else
   {
       FoodItem temp = q .front;
       value = temp .data;
       q .front = q .front .link;
       q .rear .link= q .front;
   }
  
   return value ;
}
  

static void averageCalories( Queue q)
{
int n=0;
int sumCalories=0;
   FoodItem temp = q .front;
   System.out.printf("\nAverage calories is : ");
   while (temp .link != q .front)
   { n++;
       sumCalories= sumCalories + temp.calories;
       temp = temp .link;
   }
int averageCalories = sumCalories/n;
   System.out.printf("%d", averageCalories);
}
  

static void highestCalories( Queue q)
{
int HighestCalories=0;
String name;
   FoodItem temp = q .front;
   System.out.printf("\nFood with highest calories is: ");
   while (temp .link != q .front)
   {   
       int n= temp.calories*temp.servings;
if(n>HighestCalories)
{
HighestCalories=n;
name= temp.name;
}
       temp = temp .link;
   }
  
   System.out.printf(name);
}
  
/* Driver of the program */
public static void main(String args[])
{
  
   Queue q = new Queue();
   q .front = q .rear = null;
   int noOfFoodItems;
Scanner input = new Scanner(System.in);
  
   System.out.println("enter no of food items: ")

noOfFoodItems = input.nextInt();
  
if(noOfFoodItems<=20)
{
for(int i=0;i<noOfFoodItems;i++)
{
String name = input.nextString();
int calories =input.nextInt();
int serving = input.nextInt();
  
enQueue(q, name,calories,serving);
}
}
  
     
  
  
   averageCalories(q);
   highestCalories(q);

}
}


Related Solutions

Given C++ Stack Code, Modify the code to work like a Queue.(First in First out) Stack...
Given C++ Stack Code, Modify the code to work like a Queue.(First in First out) Stack #ifndef STACK_H #define STACK_H #include "Node.h" template class Stack { private: Node* top; public: // Constructor Stack() { top = nullptr; } void push(Object ab) { if (top != nullptr) { Node* newNode = new Node(ab, *top); top = newNode; } else { Node* newNode = new Node(ab); top = newNode; } } Object pop() { if (top != nullptr) { Node *returnVal =...
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H...
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H #define STACK_H #include "Node.h" template class Stack { private: Node* top; public: // Constructor Stack() { top = nullptr; } void push(Object ab) { if (top != nullptr) { Node* newNode = new Node(ab, *top); top = newNode; } else { Node* newNode = new Node(ab); top = newNode; } } Object pop() { if (top != nullptr) { Node *returnVal = top; top...
Write a c++program using queue to find the minimum value in a queue. Use the above...
Write a c++program using queue to find the minimum value in a queue. Use the above program for implementing queue. You must use dequeue function to read values from queue.  
((by C++ ))Write a program that will reverse the content of a Queue using the following...
((by C++ ))Write a program that will reverse the content of a Queue using the following standard queue operations. enqueue(x) : Add an item x to rear of queue. dequeue() : Remove an item from front of queue. empty() : Checks if a queue is empty or not. For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the...
Given a queue of integers, write an algorithm and the program in c++ that, using only...
Given a queue of integers, write an algorithm and the program in c++ that, using only the queue ADT, calculates and prints the sum and the average of the integers in the queue without changing the contents of the queue.
I want to know 'Linked link + queue' and 'Linked link Stack' code in C++ But...
I want to know 'Linked link + queue' and 'Linked link Stack' code in C++ But there is a condition. There shall be the following 'menu': *****Menu***** 1. Insert into Stack 2. Insert into Queue 3. Delete from Stack 4. Delete from Queue 5. View Content in Stack 6. View Content in Queue ********************************************** Let me give you a example. >> 1 30 // Insert 30 into Stack >>1 58// Insert 58 into Stack >>1 26 // Insert 26 into...
In this lab, using C++, you will create two data structures: a stack and a queue....
In this lab, using C++, you will create two data structures: a stack and a queue. You will use STL containers to demonstrate basic ADTs. Queue For the queue, you will simulate a buffer. Remember it is first-in-first-out. The user will enter a number for the number of rounds to run your simulation. You need one function that randomly generates a number. You will also have a user specified percentage, and the function uses this percentage to randomly put the...
In c++, using stack structure, write a program that will take a sequence of characters (string)...
In c++, using stack structure, write a program that will take a sequence of characters (string) and determine whether it is a palindrome. Use the linked version of the stack.
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT