hi ,
I just was trying to implement a stack using doubly linked list . Well , i cannot exactly find out the error. Any hint would be of great help.
Heres the code
#include<iostream.h>
#include<conio.h>
class abc
{
public:
int num;
abc* next;
abc* previous;
friend void push(abc*,int);
void pop(abc*);
friend void display();
};
abc* head;
void display()
{
while(1)
{
cout<<head->num;
if(head->next==NULL)
break;
head=head->next;
}
}
void push(abc* top,int no)
{
static int ctr=0;
if(ctr==0)
{
top=new abc;
if(top==NULL)
{
cout<<"Overflow"<<endl;return;
}
head=top;
top->num=no;
top->next=NULL;
top->previous=top;
ctr++;
}
else
{
top->next=new abc;
if(top->next==NULL)
{
cout<<"Overflow"<<endl;return;
}
top->previous=top;
top=top->next;
top->num=no;
top->next=NULL;
}
}
int main()
{
abc* top=NULL;
int n;
do
{
cout<<"Enter 1 for push , 2 for peep , 3 for pop , 4 for change and -1 to exit "<<endl;
cin>>n;
int x;
switch(n)
{
case 1:
cout<<"Enter number you want to push"<<endl;
cin>>x;
push(top,x);
display();
break;
}
}while(n!=-1);
}