c++ data structures write comments on the lines of code explaining what they do
ID: 3741787 • Letter: C
Question
c++ data structures
write comments on the lines of code explaining what they do
Create a class/struct for a node. Create a class/struct for a list. Members: Top - a node that tracks the top of the stack Count - indicates how many items are on the stack. Methods: Push - Accepts a number and adds to the top of the stack. Pop - Returns a number from the top of the stack. - If the stack is empty, emit an error indicating the stack is empty. IsEmpty - Returns a boolean indicating if the stack is empty
Explanation / Answer
# include<iostream.h>
# include<conio.h>
# define SIZE 10
class node
{
int z[SIZE];
int top, count=0; //Count for the status of stack and top will point at top of the stack
public:
node();
void push(int);
int pop();
int isempty();
int isfull();
};
node::node()
{
top=0; //Initializing the top of the stack to 0
}
int node::isempty() // This condition will tell you whether the stack is empty or not.
{
return (top==0?1:0);
}
int node::isfull() // This condition will tell you whether the stack is full or not.
{
return (top==SIZE?1:0);
}
int node::pop()
{
if(!isempty())
{
cout<<"Item is to be poped out form stack "<<z[top-1]<<endl;
count--;
return(z[--top]);
}
else
{
cerr<<"Stack underflow. You have nothing to pop!";
}
return 0;
}
void node::push(int j)
{
if(!isfull()) //Whether the stack is full or not
{
cout<<"Item to be pushed into stack: "<<i<<endl;
z[top]=j;
top++;
count++;
}
else
{
cerr<<"Overflow of stack error Data loss may happen";
}
}
void main()
{
clrscr();
node s;
s.push(5);
s.push(7);
s.push(2);
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.