#include<iostream.h>
#include<conio.h>
class StackUsingLink
{private: struct node
{ int data;
node *link;
} *top;
public:
StackUsingLink();
void push(int item);
int pop();
int peak();
void display();
}x;
StackUsingLink::StackUsingLink() { top=NULL;}
void StackUsingLink::push(int item)
{ node *temp;
temp=new node;
if (temp==NULL)
cout<<"\n Stack is Full\n"<<endl;
else
{ temp->data = item;
temp->link = top;
top=temp;
}
}
int StackUsingLink::pop()
{int n=-9999;
if (top == NULL) cout<<"The Stack is Empty\n";
else
{n=top->data;
top=top->link;}
return n;
}
int StackUsingLink::peak()
{ int n=-9999;
if (top ==NULL) cout<<"\n The stack is Empty\n";
else n=top->data;
return n;
}
void StackUsingLink::display()
{ node *temp;temp=top;
if (temp !=NULL)
{ cout<< "top ---> ";
while(temp != NULL)
{cout.width(5);
cout<<temp->data;
temp=temp->link;
}
cout<<endl;
}
else cout<<"\n The stack is empty...\n"<<endl;
}
void main()
{
clrscr();
x.push(8);
x.push(7);
x.push(5);
x.push(2);
cout<<"\nThe elements of the stack\n";
x.display();
int m=x.peak();
cout<<"\nThe topmost element is"<<m<<endl;
cout<<"\n After calling peak the stack is\n";
x.display();
m=x.pop();
cout<<"\nAfter deleting a topmost element "<<m<<" the stack
is..\n";
x.display();
getch();
}
No comments:
Post a Comment