Applications Amount of RAM used Microsoft Word 250kb/doc Mozilla Firefox 175kb/w
ID: 3573793 • Letter: A
Question
Applications
Amount of RAM used
Microsoft Word
250kb/doc
Mozilla Firefox
175kb/window
Notepad
92kb/doc
Media Player
231kb/file
Calculator
50kb
Paint
120kb
Visual Studios
512kb
Program Specifications: Given an amount of RAM (in megabytes) that is provided by the user, write a program that utilizes the stack or queue concept where the program allocates space used by the applications listed above. C++ Program.
Data Structure: Use a linked list structure. Bonus: Create an array data structure that runs parallel to the linked list structure.
Actions:
The program will continue to ask the user to select an application to run until they decide to quit the program.
When the user decides to quit, the program will print all the current running items on the stack and the amount of RAM (in megabytes) currently available on the computer.
If the computer doesn’t have enough memory to run an application, applications are removed from the stack or queue until enough memory is available to run the application. So, there will never be an instance where an application cannot be run.
Applications
Amount of RAM used
Microsoft Word
250kb/doc
Mozilla Firefox
175kb/window
Notepad
92kb/doc
Media Player
231kb/file
Calculator
50kb
Paint
120kb
Visual Studios
512kb
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// A structure to represent a stack
struct StackNode
{
int data;
struct StackNode* next;
};
struct StackNode* newNode(int data)
{
struct StackNode* stackNode =
(struct StackNode*) malloc(sizeof(struct StackNode));
stackNode->data = data;
stackNode->next = NULL;
return stackNode;
}
int isEmpty(struct StackNode *root)
{
return !root;
}
void push(struct StackNode** root, int data)
{
struct StackNode* stackNode = newNode(data);
stackNode->next = *root;
*root = stackNode;
printf("%d pushed to stack ", data);
}
int pop(struct StackNode** root)
{
if (isEmpty(*root))
return INT_MIN;
struct StackNode* temp = *root;
*root = (*root)->next;
int popped = temp->data;
free(temp);
return popped;
}
int peek(struct StackNode* root)
{
if (isEmpty(root))
return INT_MIN;
return root->data;
}
int main()
{
struct StackNode* root = NULL;
int ram;
int available_ram;
scanf("%d",&ram);
available_ram = ram;
while(1){
int choice;
cout << "Make a Choice: "<< endl;
cout << "1.quit "<< endl;
cout << "2.task "<< endl;
scanf("%d",&choice);
if(choice==1){
while(peek(&root)!=INT_MIN){
cout >> pop(&root) >> endl;
}
break;
}
else{
int required_ram;
scanf("%d",&required_ram);
if(available_ram - required_ram >=0){
push(&root, required_ram);
available_ram = available_ram - required_ram;
}else{
while(available_ram - required_ram <0){
pop(&root);
}
push(&root, required_ram);
available_ram = available_ram - required_ram;
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.