following linked list implementation with stacks works fine
#include <iostream>
using namespace std;
class Link
{
public:
long ddata;
Link *next;
Link(long d){
ddata=d;
}
void displaylink(){
cout<<ddata<<" "<<endl;
}
};
class LinkedList{
private :
Link * first;
public:
LinkedList(){
first=NULL;
}
bool empthy(){
return first==NULL;
}
void insertfirst(long dd){
Link *newlink=new Link(dd);
newlink->next=first;
first=newlink;
}
long deletefirst(){
Link *temp=first;
first=first->next;
return temp->ddata;
}
void display(){
Link *current=first;
while(current!=NULL){
current->displaylink();
current=current->next;
}
cout<<endl;
}
};
class StackList{
public:
LinkedList *ls;
public:
StackList(){
ls=new LinkedList();
}
void push(long j){
ls->insertfirst(j);
}
long pop(){
return ls->deletefirst();
}
bool empthy()
{
return (ls->empthy());
}
void displaystack(){
ls->display();
}
};
int main(){
StackList *SL=new StackList();
SL->push(20);
SL->push(40);
SL->push(60);
SL->displaystack();
SL->pop();
SL->displaystack();
return 0;
}
but i am interested if it has some possible bugs or can it be optimized more?if you see some nonsecury point in this code please tell me how to correct it?thanks a lot,it is just curiosity question
Your memory management is flawed – as is, it will leak because there are
news but nodeletes. Once you take care of thedeletes, make sure you handle copies of the classes (have the rule of three in mind).