Question

In: Computer Science

In language C Have the program present a menu where I can either: Insert a new...

  • In language C
  • Have the program present a menu where I can either:
    • Insert a new integer onto the stack.
    • Process an integer from the stack.
    • Quit the program.
  • Every time that you present the menu, please print out the contents of the stack before I pick a menu option. If the stack is empty, please let the user know.
  • The point of this assignment is to use dynamic memory allocation. So you must use malloc at the start of the program to allocate memory for a single integer. Then use realloc for the remainder of the program. Statically declaring arrays will result in no credit for the assignment!
  • If I choose the second options, which is to process a node, please print out the value that is being processed and then use realloc to appropriately change the size of your array.
  • Be sure to use the free function to release all memory if I decide to quit the program.

You do not have to do this in a function. This can all be done in the main function if you like.

A queue is a similar data structure in which the first item inserted into the queue is the first item processed (FIFO). After I choose to quit the stack loop, reallocate the array pointer back to a single integer and then repeat the program above, but so it processes the integers in the order of a queue rather than a stack. The output should indicate that we are now using a queue as well.

Solutions

Expert Solution

#include <stdio.h> 
#include <stdlib.h> 

int main()
{
        int count = 0;
        // pointer to implement stack
        int *ptr; 
        //dynamic memory allocation using malloc
        ptr = (int*)malloc(sizeof(int));
        while(1)
        {
                //menu
                printf("Menu:\n");
                printf("1.Insert a new integer onto the stack\n");
                printf("2.Process an integer from the stack\n");
                printf("3.Quit the stack loop\n");
                //Printing state of stack
                if(count==0)
                {
                        printf("Stack is currently empty\n");
                }
                else
                {
                        printf("Current state of stack : ");
                        for (int i = count-1; i>=0; --i){ 
                        if(i!=count-1)
                        printf(", ");
            printf("%d", ptr[i]); 
                } 
                printf("\n\n");
                }
                printf("Enter the desired option\n");
                int temp;
                scanf("%d",&temp);
                if(temp==1)
                {
                        int input;
                        printf("You choosed option 1. Enter the value to be pushed on stack\n");
                        scanf("%d",&input);
                        count++;
                        //Reallocation of memory using realloc
                        ptr = (int*)realloc(ptr,count*sizeof(int));
                        ptr[count-1]=input;
                }
                else if(temp==2)
                {
                        printf("You choosed option 2. Processed element is %d\n",ptr[count-1]);
                        count--;
                        ptr = (int*)realloc(ptr,count*sizeof(int));
                }
                else if(temp==3)
                {
                        printf("You choosed option 3. You are out of the stack loop.\n\n");
                        //free function to release memory
                        free(ptr);
                        break;
                }
                else
                {
                        printf("Invalid Input\n");
                }
        }
        
        //implemetation of queue
        int head=0;
        count = 0;
        ptr = (int*)malloc(sizeof(int));
        while(1)
        {
                printf("Menu:\n");
                printf("1.Insert a new integer in the queue\n");
                printf("2.Process an integer from the queue\n");
                printf("3.Quit the queue and exit\n");
                if(count==0)
                {
                        printf("Queue is currently empty\n");
                }
                else
                {
                        printf("Current state of queue : ");
                        for (int i = head; i<count; ++i){ 
                        if(i!=head)
                        printf(", ");
            printf("%d", ptr[i]); 
                } 
                printf("\n\n");
                }
                printf("Enter the desired option\n");
                int temp;
                scanf("%d",&temp);
                if(temp==1)
                {
                        int input;
                        printf("You choosed option 1. Enter the value to be pushed in queue\n");
                        scanf("%d",&input);
                        count++;
                        ptr = (int*)realloc(ptr,count*sizeof(int));
                        ptr[count-1]=input;
                }
                else if(temp==2)
                {
                        printf("You choosed option 2. Processed element is %d\n",ptr[head]);
                        head++;
                }
                else if(temp==3)
                {
                        printf("You choosed option 3. You are out of the queue loop and exitted from the program.");
                        free(ptr);
                        break;
                }
                else
                {
                        printf("Invalid Input\n");
                }
        }
}

C code which implemets stack and queue and allows the user to choose between three options: 1.to insert an element, 2. to take out and print the element, 3 to quit and exit.

First the stack implementation is done using int pointer ptr and dynmaic memory allocation is done using malloc.

Integer variable count is maintained which represents the size of the stack. Contents of the stack are printed out after each time menu is printed. Memory is reallocated if either option1 or 2 is chosen using realloc() and memory is freed using free() if option3 is choden.  

Queue implementation is performed in a similar fashion but the processing of elements is different in the queue. Queue is based on first in first out scheme where the first element entering the queue is the first to leave out where as Stack is based on last in first out scheme where the last element entering the stack is the first to leave out.


Related Solutions

C language Write a program in C to implement Queue and its operation (like create, insert,...
C language Write a program in C to implement Queue and its operation (like create, insert, delete, search) using array data structure.
I have to create a program in C++ where a user can enter as many numbers...
I have to create a program in C++ where a user can enter as many numbers as they want (they predetermine the number of values to be inputted) and then the program can echo that input back to the user and then determine if the numbers were even, odd, or a zero and it outputs how many of each were found. This is to be down with four void functions and no arrays. The program initializes the variables zero, odds,...
In C++ Language English/Spanish Translation Program. Create a menu driven program that translates English to Spanish...
In C++ Language English/Spanish Translation Program. Create a menu driven program that translates English to Spanish and Spanish to English. Your translation program should use arrays for this program. You will need to populate the arrays with the contents of the English and Spanish data files provided. The two files are ordered such that each word in the one file corresponds to the respective translation in the other (i.e.: the first word in the ENG.txt file corresponds to the first...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu to choose from – 1. Convert temperature input from the user in degrees Fahrenheit to degrees Celsius 2. Convert temperature input from the user in degrees Celsius to degrees Fahrenheit 3. Quit. Formulae you will need: C = (5 / 9) * (F-32) and F = (9/5) * C + 32 1. Use functions to accomplish 1 and 2 above. 2. Use at least...
I have this matlab program, and need to turn it into a C++ program. Can anyone...
I have this matlab program, and need to turn it into a C++ program. Can anyone help me with this? % Prompt the user for the values x and y x = input ('Enter the x coefficient: '); y = input ('Enter the y coefficient: '); % Calculate the function f(x,y) based upon % the signs of x and y. if x >= 0    if y >= 0        fun = x + y;    else        fun = x + y^2;    end...
Present a screenshot of the following Program to open up the created file in C++ language....
Present a screenshot of the following Program to open up the created file in C++ language. The list of random numbers will be below the instructions I have provided for you a file named Random.txt for use in this program. This file contains a long list of random numbers. You are to write a program that opens the file, reads all the numbers from the file and calculates the following: A. The number of numbers in the file B. The...
Java Write a menu driven program that implements the following linked list operations : INSERT (at...
Java Write a menu driven program that implements the following linked list operations : INSERT (at the beginning) INSERT_ALPHA (in alphabetical order) DELETE (Identify by contents, i.e. "John", not #3) COUNT CLEAR
C# Yahtzee Program I am creating a Yahtzee program where you have the option to choose...
C# Yahtzee Program I am creating a Yahtzee program where you have the option to choose 5 or more dice that will be rolled. I just need help trying to create a random dice roll and seeding it. Could you give me some code examples of getting a dice roll of a six sided die that won't display similar results.
Write a C/C++ program that simulate a menu based binary numbercalculator. This calculate shall have...
Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities:Covert a binary string to corresponding positive integersConvert a positive integer to its binary representationAdd two binary numbers, both numbers are represented as a string of 0s and 1sTo reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the following three functions:int binary_to_decimal(string...
c programing language When a new program is executed, a new process is created with the...
c programing language When a new program is executed, a new process is created with the next available process ID. However, there are a few special processes that always have the same process ID, which are usually given the ID value less than 5 these are called system processes. Can you identify which of the two system processes have the process ID of 0 and 1 respectively?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT