Question

In: Computer Science

Write your own version of a class template that will create a static stack of any...

Write your own version of a class template that will create a static stack of any data type. Demonstrate the class with a driver program.

please make a version to copy.

Solutions

Expert Solution

source code:


#include <iostream>
#include<conio.h>
#define size 4
using namespace std;

class stack
{
int data[size];
int top;
public:
stack()
{
top=-1;
}
void push();
void pop();
void display();
};

void stack::push()
{
if(top==size-1)
{
cout<<"\nStack is full";
return;
}
else
{
top++;
cout<<"Enter Data : ";
cin>>data[top];
}
}

void stack::pop()
{
if(top==-1)
cout<<"\n Stack is empty";
else
{
cout<<data[top]<<"deleted "<<endl;
top--;
}
}

void stack::display()
{
int t=top;
while(t>=0)
{
cout<<data[t]<<endl;
t--;
}
}

int main()
{
stack st;
int ch;
do
{
cout<<"\n1. Push\n2. Pop\n3. Display \n4.Quit\nEnter Choice(1-4) ";
cin>>ch;
switch(ch)
{
case 1: st.push();break;
case 2: st.pop();break;
case 3: st.display();
}
}while(ch!=4);

return 0;
}

if you have any doubts ask me...


Related Solutions

C++ Static Stack Template In this chapter you studied IntStack, a class that implements a static...
C++ Static Stack Template In this chapter you studied IntStack, a class that implements a static stack of integers. Write a template that will create a static stack of any data type. Demonstrate the class with a driver program. Dynamic Stack Template In this chapter you studied DynIntStack, a class that implements a dynamic stack of integers. Write a template that will create a dynamic stack of any data type. Demonstrate the class with a driver program. Static Queue Template...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as the...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as...
Question 2 (Function Template) Write a template version of the iterative binary search algorithm that searches...
Question 2 (Function Template) Write a template version of the iterative binary search algorithm that searches an array of arbitrary type for a given key. Declare and implement a class called Student that keeps the student id, name, and grade. Include a default constructor, the overloaded insertion (<<) operator and also the overloaded extraction operator (>>). Declare and implement another class called Book that keeps the book’s title, author, and price. Just like the Student class, Include in class Book...
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
(create your own question) Prepare a static budget and use it for performance reporting.
(create your own question) Prepare a static budget and use it for performance reporting.
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your program does not need to do anything else.  Indicate via comments where memory for at least one variable in each memory area is allocated. a) Write code that allocates static memory (only include one declaration): b) Write code that allocates stack memory (only include one declaration): c) Write code that allocates heap memory (only include one declaration): 2) Edit the C++ program below to include...
Write a program to implement the IntStack that stores a static stack of integers and performs...
Write a program to implement the IntStack that stores a static stack of integers and performs the pop, push, isFull, and isEmpty operations. Write the main class to create a static stack of numbers 10, 20, 30, 40, and 50 then try the member functions. C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT