answersLogoWhite

0

include <iostream>

using namespace std;

#define SIZE 10

template <class StackType> class stack {

StackType stck[SIZE];

int topOfStack;

public:

void init() {

topOfStack = 0;

}

void push(StackType ch);

StackType pop();

};

template <class StackType>

void stack<StackType>::push(StackType ob)

{

try {

if(topOfStack==SIZE) throw SIZE;

} catch(int) {

cout << "Stack is full.\n";

return;

}

stck[topOfStack] = ob;

topOfStack++;

}

template <class StackType>

StackType stack<StackType>::pop()

{

try {

if( topOfStack == 0)

throw 0;

} catch(int) {

cout << "Stack is empty.\n";

return 0;

}

topOfStack--;

return stck[topOfStack];

}

int main()

{

stack<char> stack1, stack2;

int i;

stack1.init();

stack2.init();

stack1.push('a');

stack2.push('x');

stack1.push('b');

stack2.push('y');

stack1.push('c');

stack2.push('z');

for(i = 0; i <3; i++)

cout << "Pop stack1: " << stack1.pop() << endl;

for(i = 0; i <4; i++)

cout << "Pop stack2: " << stack2.pop() << endl;

// demonstrate double stacks

stack<double> doubleValueStack1, doubleValueStack2; // create two stacks

// initialize the stacks

doubleValueStack1.init();

doubleValueStack2.init();

doubleValueStack1.push(1.1);

doubleValueStack2.push(2.2);

doubleValueStack1.push(3.3);

doubleValueStack2.push(4.4);

doubleValueStack1.push(5.5);

doubleValueStack2.push(6.6);

for(i = 0; i <3; i++)

cout << "Pop doubleValueStack1: " << doubleValueStack1.pop() << endl;

for(i = 0; i <4; i++)

cout << "Pop doubleValueStack2: " << doubleValueStack2.pop() << endl;

return 0;

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

Array implementation of priority queue example program in c plus plus?

yes


What is the Java program for sorting an array?

Java has a very efficient built in implementation of quick sort. You can use it on any array of primitives or Comparable Objects by invoking Arrays.sort(&lt;array&gt;) See related link.


C program to implement tower of hanoi using array implementation of stack abstract datatype?

stack abstract datatype


What is an error in java?

An error or more commonly known as an Exception is a situation where the java program behaves in a way it is not supposed to do so. It is a problem in the code that is causing the JVM to terminate or throw error messages in the console. Ex: When you initialize an array list with 10 elements and try to access the 11th element in the array list you will get an array index out of bounds exception.


What are the design issue for array?

What is its size? How is its size determined and when (compile/run-time). What does the software using the array do when the array is empty? partially full? full? Avoid the software addressing elements of the array which are undefined, or addressing elements outside the bounds of the array When and who is responsible for allocating and freeing memory when the array is no longer needed (program or called procedure start/termination) or some other time determined during program execution. If the array is implementing a data structure such as a stack, queue, dequeue, list, etc. What is its implementation of the usual data structure operations, Create, Empty, List Items, Top, First, Last, Next, etc.


How do you use in array?

cod a program student degree array in c language


C program for storage representation of 2-D array?

Wright a 'C' program for storage representation of 2-D array.


What is the Java implementation for finding the median of two sorted arrays efficiently?

One efficient Java implementation for finding the median of two sorted arrays is to merge the arrays into one sorted array and then calculate the median based on the length of the combined array.


What is a implementation on n numbers of term in bubble sorting?

void bubblesort (int* array, int size) { if (!array size&lt;2) return; int last_swap = size; while (last_swap&gt;0) { int n=last_swap; for (int i=1; i&lt;last_swap; ++i) { if (array[i]&lt;array[i-1]) { array[i]^=array[i-1]^=array[i]^=array[i-1]; n=i; } last_swap = n; } }


How many dimensions you can declare in array?

It seems that the number of allowed array dimensions is implementation specific and not set by the Java specifications. I'm sure that any Java implementation will allow a reasonable number of dimensions for any project you have. After a quick test, it seems that Java is not limited by an arbitrary number so much as a practical value. If you add hundreds of array dimensions, Java will allow you to do so as long as you have enough memory allocated for Java. After a bit of copy-pasting the program no longer ran, exiting with a StackOverflowError.


Program to count the number of numbers in an array using 8085 microprocessor?

A program which is used to count the number of numbers in an array using a 8085 microprocessor is known as a assembly language program.


Program to print all the even numbers of an array in visual basic?

find even number in array