C Implement a sequence class using a stack queue or deque Pl
C++ Implement a sequence class using a stack, queue, or deque. Please include the main, cpp, and header file.
Solution
Answer:
Program code:
Stack.h:
#ifndef STACK_H
#define STACK_H
//implementation of class
class Sequencestack
{
//declares the data members
int stackVal[5];
int topval;
public:
//calls the constructor
Sequencestack();
//declares the methods
void push(int x1);
void pop();
void display();
};
#endif
Stack.cpp
#include\"stack.h\"
#include<iostream>
using namespace std;
//Implements the construtor for the class
Sequencestack::Sequencestack()
{
//assigns the value -1
topval=-1;
}
//defines the function push()
void Sequencestack:: push(int x1)
{
//checks the top value
if(topval > 4)
{
//prints the stack
cout <<\"stack over flow\";
return;
}
//assigns the x top value
stackVal[++topval]=x1;
cout <<\"Inserted:\" <<x1;
}
//defines the pop() function
void Sequencestack::pop()
{
//checks the top value
if(topval <0)
{
cout <<\"stack under flow\";
return;
}
cout <<\"deleted:\" <<stackVal[topval--];
}
//defines the function display()
void Sequencestack::display()
{
//checks the top value
if(topval<0)
{
cout <<\" stack empty\";
return;
}
//loop to print the elements
for(int it=topval;it>=0;it--)
cout <<stackVal[it] <<\" \";
}
Main.cpp:
#include\"stack.h\"
#include<iostream>
using namespace std;
//main program
int main()
{
//declares the variables
int chioce;
//instance for the class
Sequencestack stk1;
while(1)
{
//Prints the choices
cout <<\"\ 1.push 2.pop 3.display 4.exit\ Enter ur choice\";
cin >> chioce;
//switch choice declaration
switch(chioce)
{
//case 1 declaration
case 1: cout <<\"enter the element\";
//getting the element
cin >> chioce;
//call of push()
stk1.push(chioce);
break;
//case 2 declaration
case 2: stk1.pop(); break;
//case 3 declaration
case 3: stk1.display();break;
//case 4 declaration
case 4: exit(0);
}
}
system(\"pause\");
return 0;
}
Sample output:
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element12
Inserted:12
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element34
Inserted:34
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element23
Inserted:23
1.push 2.pop 3.display 4.exit
Enter ur choice3
23 34 12
1.push 2.pop 3.display 4.exit
Enter ur choice




