Question

Hi could you guys help me with this one and yes I'll rate you up thank...

Hi could you guys help me with this one and yes I'll rate you up thank you! this is for c++ : )

Design a stack in C++ that in addition to push, pop, and top functions, also has a min function that returns the minimum element in the entire stack. The stack can contain the minimum element in the top.

The purpose of having a min function is for runtime purposes, such that the minimum element can be retrieved quickly when needed (as opposed to traversing an entire linked list from start to end just to find the minimum object).

You can implement this by modifying the push function, so that each time a new element is pushed it is checked if the new element is less than the top or greater than the top (if the new element is greater than the top then the top and new are swapped).

0 0
Add a comment Improve this question Transcribed image text
Answer #1


ANSWER:-

CODE:-

#include <iostream>
#include <stack>
using namespace std;
int top = -1; // it is a variable that points to top of the stack
void push(int stack[]){
   int c;
   // read number from keyboard
   cout<<"Enter No: ";
   cin>>c;
   //pushing element into array
    stack[++top] = c;
   cout<<"Item pushed."<<endl;
}
void pop(int stack[]){
   if(top>=0){
       // displaying pop element
       cout<<"Item popped."<<endl;
       cout<<"Popped element is " << stack[top]<<endl;
       top--;
   }else{
       cout<<"No elements in the stack."<<endl;
   }
  
}
void topp(int stack[]){
   if(top>=0)
       cout<<"Top of the stack is " << stack[top]<<endl;
   else{
       cout<<"No elements in the stack."<<endl;
   }
}
void min(int stack[]){
   int mini = 9999;
   int i = 0;
   if(top>=0){
       // findind min element
       while(i<=top){
           if(mini>stack[i])
               mini = stack[i];
           i++;
       }
       cout<<"Minimum element in the stack is "<<mini<<endl;  
   }else{
       cout<<"No elements in the stack."<<endl;
   }
}
main(){
   int ch;
   int stack[10];
   cout<<"****Welcome*****"<<endl;
   do{
       cout<<"1.Push"<<endl;
       cout<<"2.Pop"<<endl;
       cout<<"3.Top"<<endl;
       cout<<"4.Minimum"<<endl;
       cout<<"5.Exit"<<endl;
       cin>>ch;
       switch(ch){
           case 1:
               push(stack);
               break;
           case 2:
               pop(stack);
               break;
           case 3:
               topp(stack);
               break;
           case 4:
               min(stack);
               break;
           case 5:
               cout << "Thank you!!!"<<endl;
               break;
           default:
               cout<<"Please enter valid option"<<endl;
               break;
       }
   }while(ch!=5);
}

Output:-

Code Screenshots:-

      THANK YOU!!!!!

Add a comment
Know the answer?
Add Answer to:
Hi could you guys help me with this one and yes I'll rate you up thank...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design a stack in C++ that in addition to push, pop, and top functions, also has...

    Design a stack in C++ that in addition to push, pop, and top functions, also has a min function that returns the minimum element in the entire stack. The stack can contain the minimum element in the top. The purpose of having a min function is for run time purposes, such that the minimum element can be retrieved quickly when needed (as opposed to traversing an entire linked list from start to end just to find the minimum object). You...

  • Hi could you guys help me with this one! thank you and yes I'll rate you...

    Hi could you guys help me with this one! thank you and yes I'll rate you up! thank you Design a class Contact with the data and functionality for one made up contact person ( Don't use actual contact). As data it should store a name, email, and phone. The functions should include getter (accessor) and setter (modifier) functions for each of these data items. Write a small main function that initializes an array of size 10 with: 3 Contact...

  • Hi could you guys help me with this 3 questions tho and yes I'll rate you...

    Hi could you guys help me with this 3 questions tho and yes I'll rate you up thank you! 5. A C++ class is most similar to a(n) a. inline function b. pointer c. library function d. structure e. reference 6. A __________ is a member function that is automatically called when a class object is __________. a. constructor, created b. destructor, created c. static function, deallocated d. utility function, declared e. None of these 7. Which is the base...

  • Hi could you guys help me with this one and yes I'll rate you up after...

    Hi could you guys help me with this one and yes I'll rate you up after thank youu ^^ Suppose a firm is currently producing 900 computers per week and charging a price of $1,200 per computer. a. Show how the firm will respond to a negative demand shock if prices are flexible b. Generalizing from the computer market specifically to the economy as a whole, what will happen when this negative demand shock occurs across the economy's many markets?...

  • PYTHON please help this is very confusing! THANK YOU! Q5 18 Points In this question, we...

    PYTHON please help this is very confusing! THANK YOU! Q5 18 Points In this question, we will implement the DrippingStack class. This is a fixed capacity stack; it's size does not increase or decrease automatically. Capacity of this DrippingStack will be determined during initialization. When push is invoked with the DrippingStack at full capacity, rather than throwing a FullStack exception, a more typical semantic is to accept the pushed element at the top while dripping the oldest element from the...

  • HI, I am having trouble finsihing this program i was wondering if someone could help me....

    HI, I am having trouble finsihing this program i was wondering if someone could help me. Thanks in adavnce. Here is the code that I have so far..... //Stack.h // implementation file for the stack class #include <iostream> using namespace std; const int stack_size = 100; class stack { private: char data [stack_size]; // elements in the stack int top; // index of the top element of the stack public: stack (); // constructor creates an empty stack void push...

  • I need help with a c++ code, i am new learner on stack, please help me...

    I need help with a c++ code, i am new learner on stack, please help me write Stack.cpp according to Stack.h provided, thanks(Notes: That data structure is a singly linked list in which pushed items are placed at the tail of the linked list. Similarly, popped items will be removed from the tail of the list.) Actually,I can write a regular one, but i'm not sure how to use the tail in the question. class Stack { private: // Desc:...

  • I need help with this coding. A. Create ArrayIntStack.java (one that's REALLY O(constant), totally!!!) B. Create...

    I need help with this coding. A. Create ArrayIntStack.java (one that's REALLY O(constant), totally!!!) B. Create the Javadoc web page (separate Assignment #15b). A. Create your own Stack Class, call it ArrayIntStack.java and write the standard four public Stack methods as listed below (this is the easy part of the assignment). Use at least one private helper method, maybe to ensure capacity of the array. All methods in your ArrayIntStack must have O(constant) run-time. Your ArrayIntStack extends no other Classes...

  • Please just fill in the codes where this CharacterOrganizer.java class asks for (Bolded). It's a simple...

    Please just fill in the codes where this CharacterOrganizer.java class asks for (Bolded). It's a simple Java Stacks assignment. I'll rate you thumbs up! Thank you Program Info: Your program needs to read in characters and push them into the stack called startStack. Then your program should remove each element from the startStack from its top one by one, and the objective to insert all characters into the stack called finalStack in the alphabetical order. If the top character of...

  • You will write the following files: mystack.h - contains the class definition for the mystack class....

    You will write the following files: mystack.h - contains the class definition for the mystack class. mystack.cpp - contains the definitions for member functions of the mystack class. inpost.cpp - contains your convert() function. inpost.h - contains the function prototype for convert() so that the main() can call it. Each of the files (with the exception of inpost.h) is described in more detail below. All header files should contain header guards to prevent them from being included multiple times in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT