#include<iostream.h>
#include<conio.h>
class linked_list_stack
{
private:struct node
{
int data;
node *link;
};
node *top;
node *entry;
node *print;
node *bottom;
node *last_entry;
node *second_last_entry;
public:linked_list_stack()
{
top=NULL;
bottom=NULL;
}
void push();
void pop();
void print_list();
void show_working();
};
void linked_list_stack::push()
{
int num;
cout<<"Enter the value to be pushed:"<<endl;
cin>>num;
entry=new node;
if(bottom==NULL)
{
entry ->data=num;
entry ->link=NULL;
bottom=entry;
top=entry;
}
else
{
entry->data=num;
entry->link=NULL;
top->link=entry;
top=entry;
}
cout<<"the"<<num<<"is pushed on to stack"<<endl;
cout<<"press any key to return to menu"<<endl;
getch();
}
void linked_list_stack::pop()
{
if(bottom==NULL)
cout<<"**** error *****:stack is empty"<<endl;
else
{
for(last_entry=bottom;last_entry->link!=NULL;last_entry=last_entry->link)
second_last_entry=last_entry;
if(top==bottom)
bottom=NULL;
int popped_element=top->data;
delete top;
top=second_last_entry;
top->link=NULL;
cout<<"the"<<popped_element<<"is popped from the stack";
cout<<endl;
}
cout<<"press any key to return to menu"<<endl;
getch();
}
void linked_list_stack::print_list()
{
print=bottom;
if(print!=NULL)
cout<<"values are pushed onto stack are"<<endl;
else
cout<<"******* Nothing to display ********";
cout<<endl;
while(print!=NULL)
{
cout<<"\t"<<print->data<<endl;
print=print->link;
cout<<"press any key to continue";
getch();
}
void linked_list_stack::show_working()
{
clrscr();
char key=NULL;
do
{
gotoxy(5,5);
cout<<"***** Implementation of linked list as a stack ****"<<endl;
gotoxy(10,8);
cout<<"Select one of the linked list operation"<<endl;
gotoxy(15,10);
cout<<"press P to push avalue\n";
gotoxy(15,12);
cout<<"press O to pop a value\n";
gotoxy(15,14);
cout<<"press S to print a value\n";
gotoxy(15,16);
cout<<"press E to exit\n";
input:
gotoxy(10,20);
cout<<"enter the choice"<<endl;
key=getch();
if(int(key)==27||key=='e'||key=='E')
break;
else if(key=='p'||key=='P')
push();
else if(key=='o'||key=='O')
pop();
else if(key=='s'||key=='S')
print_list();
else
goto input;
}
while(1);
}
See anything odd about the function show_working(), now that the indentation is done? See how all the other functions start at the far left? Is there anything funny about the end of the previous function?