hi all,
i am new here :)
i have my pracs tmrw, and so was trying out a few programmes to be prepared...
but im stuck right now...
i tried a lot to find my mistake, but didnt succeed...
i hope u guys can help me - please, its urgent...
here is the code:
Code:
//3 linked stack
#include<conio.h>
#include<iostream.h>
#include<process.h>
struct node
{
int a,b;
node *next;
};
class linkedstack
{
node* top;
public:
linkedstack()
{
top==NULL;
}
~linkedstack()
{
node *ptr;
ptr=top;
while(ptr!=NULL)
{
top=ptr->next;
delete ptr;
ptr=top;
}
void push();
void pop();
void display();
};
void linkedstack::push()
{
node* newnode;
newnode = new node;
cout<<"enter a";
cin>>newnode->a;
cout<<"enter b";
cin>>newnode->b;
newnode->next=NULL;
if(top==NULL)
top=newnode;
else
{
top=newnode;
newnode->next=top;
}
}
void linkedstack::pop()
{
node *ptr;
if(top==NULL)
cout<<"underflow";
else
{
ptr=top;
top=top->next;
delete ptr;
}
}
void linkedstack::display()
{
node *ptr;
if(top==NULL)
cout<<"underflow";
else
{
ptr=top;
while(ptr!=NULL)
{
cout<<ptr->a;
cout<<ptr->b;
ptr=ptr->next;
}
}
}
void main()
{
int ch;
linkedstack L1;
do
{
clrscr();
cout<<"1. Push element\n";
cout<<"2. Pop element\n";
cout<<"3. Display elements\n";
cout<<"4. Exit\n";
cout<<"Enter choice\n";
cin>>ch;
if(ch==1)
{
L1.push();
getch();
}
else if(ch==2)
{
L1.pop();
getch();
}
else if(ch==3)
{
L1.display();
getch();
}
else
exit(0);
}
while(ch!=4);
}