Question
I'm not sure what this question is asking. Here is my code #include using namespace std; class node { public: int data; node *next; };
I'm not sure what this question is asking.
Here is my code
#include
using namespace std;
class node
{
public:
int data;
node *next;
};
class PointerList
{
public:
PointerList()
{
top = NULL;
}
bool empty()
{
if(top == NULL)
return true;
else
return false;
}
void insert(int position, int element)
{
node *newptr = new node;
newptr->data = element;
if(top == NULL) //insert the very first element
if(position==0)
{
newptr->next = NULL;
top = newptr;
}
else
cout
else
if(position==0) //insert on the first position when some elements existed
{
newptr->next = top;
top = newptr;
}
else //most cases belongs to this situation (as showed in the class slide)
{
node *preptr;
preptr = top;
for(int i=0;i preptr=preptr->next; newptr->next = preptr->next; preptr->next = newptr; } } void remove(int position) { if(position==0) //delete the first element { node * ptr = top; top = ptr->next; delete(ptr); } else { node *preptr; preptr = top; for(int i=0;i preptr=preptr->next; node * ptr = preptr->next; preptr->next = ptr->next; delete(ptr); } } void print() { node *temp; temp = top; while(temp!=NULL) { coutdata temp=temp->next; } } private: node *top; int stackData; }; int main() { PointerList *sl = new PointerList(); sl->insert(0,10); sl->insert(1,20); sl->insert(2,30); sl->insert(3,40); sl->insert(0,50); sl->insert(3,60); sl->insert(5,70); sl->remove(2); sl->print(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started