Netbeans/ JAVA write this code: document every line. Two Classes Create an array
ID: 3669038 • Letter: N
Question
Netbeans/ JAVA write this code: document every line. Two Classes
Create an array of Strings, and using a stack, reverse the order of the Strings in the array For example, the strings in the array might be "Joe", Fred, "Mary". The reversed array would be "Mary Fred, "Joe" You should create a Stack class and a StackElement class for this assignment, then use these classes in your software. Your stack should be a linked data structure, similar to a linked list. The main method should initialize an array of Strings either with an explicit array declaration or by reading the data from a file. You should have at least five Strings in your array but the software should work no matter how many Strings are in the array Your stack class should maintain a stack pointer and the size of the stack, and should have methods to: Push a new element onto a stack (This method should put data onto the stack, which means it must first put the data in a stack element Pop an element off the stack and return the data in the element. Print a list of the data in the stack. Return the current stack size.Explanation / Answer
#include<iostream.h>
#include<conio.h>
int max=7;
int t=0;
class stack
{
int s[7];
public:
void push(int);
void pop();
void top();
void empty();
void show();
};
void stack::push(int y) //Push Operation
{
if(t<max)
{
t=t+1;
s[t]=y;
}
else
cout<<endl<<"stack overflows..."<<endl;
}
void stack::pop() //Pop Operation
{
int item;
if(t>=0)
{
t=t-1;
item=s[t+1];
cout<<endl<<"popped item >>"<<item<<endl;
}
else
cout<<endl<<"stack underflows"<<endl;
}
void stack::top() //To find the top of the stack
{
if(t>=0)
cout<<endl<<"topmost element >> "<<s[t]<<endl;
else
cout<<endl<<"stack underflows..."<<endl;
}
void stack::empty() //To check if the stack is empty
{
if(t<0)
cout<<endl<<"stack is empty..."<<endl;
else
cout<<endl<<"stack is not empty..."<<endl;
}
void main()
{
int a,x;
stack s1;
clrscr();
do
{
cout<<"enter an option..."<<endl<<"1-push"<<endl<<"2-pop"<<endl<<"3-top"<<endl<<"4-empty"<<endl;
cout<<"5-end"<<endl;
cin>>a;
cout<<endl;
switch(a)
{
case 1:
{
cout<<endl<<"enter a value >> "<<endl;
cin>>x;
s1.push(x);
}
break;
case 2:
s1.pop();
break;
case 3:
s1.top();
break;
case 4:
s1.empty();
break;
}
}
while(a!=5);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.