this is the assingment : Create a class/struct for a node. Create a class/struct
ID: 3745837 • Letter: T
Question
this is the assingment :
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.
this is the code I have so far :
please add a count member to the code that
Count - indicates how many items are on the stack.
Explanation / Answer
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int info;
struct node *link;
}
*top;
class stackLineup
{
public:
node *push(node *, int);
node *pop(node *);
void pass(node *);
// return the number of elements in stack
int count( node * );
stackLineup()
{
top = 0;
}
};
int main()
{
int choice, item;
stackLineup sl;
while (true)
{
cout<<" ~~~~~~~~~~~~~~~~~"<<endl;
cout<<"ready for game?"<<endl;
cout<<" ~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<"press 1 to push the number"<<endl;
cout<<" ~~~~~~~~~~~~~~~~~"<<endl;
cout<<"press 2 to pop the num"<<endl;
cout<<" ~~~~~~~~~~~~~~~~~"<<endl;
cout<<"3 to print stack"<<endl;
cout<<" ~~~~~~~~~~~~~~~~~"<<endl;
cout<<"4.to get the no of nodes"<<endl;
cout<<" ~~~~~~~~~~~~~~~~~"<<endl;
cout<<"5.to get the heck outta here"<<endl;
cout<<" ~~~~~~~~~~~~~~~~~"<<endl;
cout<<"enter ur choice from the beautiful menu: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"ENTER THE NUMBER TO PUSH INTO THE STACK!!!: ";
cin>>item;
top = sl.push(top, item);
break;
case 2:
top = sl.pop(top);
break;
case 3:
sl.pass(top);
break;
case 4 :
cout<<"No of elements in stack : "<<sl.count(top)<<endl;
break;
case 5:
exit (0);
break;
default:
cout<<"beeeeeeeeeeeeeeeeep, nope"<<endl;
}
}
return 4;
}
node *stackLineup::push(node *top, int item)
{
node *tmp;
tmp = new (struct node);
tmp->info = item;
tmp->link = top;
top = tmp;
return top;
}
node *stackLineup::pop(node *top)
{
node *tmp;
if (top == 0)
cout<<"ERROR!!!!! ERORR!!!!!! STACK IS EMPTY********************"<<endl;
else
{
tmp = top;
cout<<"num pooped: "<<tmp->info<<endl;
top = top->link;
free(tmp);
}
return top;
}
void stackLineup::pass(node *top)
{
node *ptr;
ptr = top;
if (top == 0)
cout<<"ERROR!!!!! ERORR!!!!!! STACK IS EMPTY********************"<<endl;
else
{
cout<<"heres your stack: "<<endl;
while (ptr != 0)
{
cout<<ptr->info<<endl;
ptr = ptr->link;
}
}
}
int stackLineup::count(node *top)
{
node *ptr;
ptr = top;
int x = 0;
// if stack is empty
if (top == NULL)
return 0;
else
{
// loop untill list is completely traversed
while (ptr != 0)
{
x++;
// go to next node
ptr = ptr->link;
}
}
return x;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.