Question for the techincally oriented around here - is the following a reasonable method of implementing a stack ?
class stack
{
int data;
stack* next;
public:
stack();
int pop();
void push(int);
};
stack::stack()
{
data=0;
next=NULL;
}
int stack:op()
{
if(next==NULL)
{
cout<<"Stack empty"<
exit(0);
}
stack* p=this;
while(p->next->next!=NULL)
p=p->next;
int temp=p->next->data;
delete p->next;
return temp;
}
void push(int a)
{
stack* p=this;
while(p->next!=NULL)
p=p->next;
p->next=new stack;
p->next->data=a;
}
The code tags are , for some reason , mangling the code . I'll attach a file instead .
class stack
{
int data;
stack* next;
public:
stack();
int pop();
void push(int);
};
stack::stack()
{
data=0;
next=NULL;
}
int stack:op()
{
if(next==NULL)
{
cout<<"Stack empty"<
}
stack* p=this;
while(p->next->next!=NULL)
p=p->next;
int temp=p->next->data;
delete p->next;
return temp;
}
void push(int a)
{
stack* p=this;
while(p->next!=NULL)
p=p->next;
p->next=new stack;
p->next->data=a;
}
The code tags are , for some reason , mangling the code . I'll attach a file instead .
Comment