# include <iostream.h> # include <conio.h> class Linked_list_Stack { private: struct node { int data; node *next; }; node *top; node *entry; node *print; node *bottom; node *last_entry; node *second_last_entry; public: Linked_list_Stack( ); void pop( ); void push( ); void print_original_list( ); void print_reverse_list( ); void sort_list_in_ascending_order( ); void sort_list_in_decending_order( ); void show_working( ); }; /**************************************************************************/ //---------------------- Linked_list_Stack( ) --------------------------// /**************************************************************************/ Linked_list_Stack::Linked_list_Stack ( ) { top=NULL; bottom=NULL; } /**************************************************************************/ //------------------------------- push( ) ------------------------------// /**************************************************************************/ void Linked_list_Stack::push( ) { int num; cout<<\"\\n\\n\\n\\n\\n\\t Enter value to push onto Stack : \"; cin>>num; entry=new node; if(bottom==NULL) { entry->data=num; entry->next=NULL; bottom=entry; top=entry; } else { entry->data=num; entry->next=NULL; top->next=entry; top=entry; } cout<<\"\\n\\n\\t *** \"<<num<<\" is pushed onto the Stack.\"<<endl; cout<<\"\\n\\n\\n\\t\\t Pres any key to return to Menu. \"; getch( ); } /**************************************************************************/ //---------------------------- pop( ) ----------------------------------// /**************************************************************************/ void Linked_list_Stack::pop( ) { if(bottom==NULL) cout<<\"\\n\\n\\n\\t *** Error : Stack is empty. \\n\"<<endl; else { for(last_entry=bottom;last_entry->next!=NULL; last_entry=last_entry->next) second_last_entry=last_entry; if(top==bottom) bottom=NULL; int poped_element=top->data; delete top; top=second_last_entry; top->next=NULL; cout<<\"\\n\\n\\n\\t *** \"<<poped_element<<\" is poped from the Stack.\"<<endl; } cout<<\"\\n\\n\\n\\t\\t Pres any key to return to Menu. \"; getch( ); } /**************************************************************************/ //---------------------- print_original_list( ) ------------------------// /**************************************************************************/ void Linked_list_Stack::print_original_list( ) { print=bottom; if(print!=NULL) cout<<\"\\n\\n\\n\\n\\n\\t The List in original order is : \\n\"<<endl; else cout<<\"\\n\\n\\n\\n\\n\\t *** Nothing to show. \"<<endl; while(print!=NULL) { cout<<\"\\t \"<<print->data<<endl; print=print->next; } cout<<\"\\n\\n\\n\\t\\t Pres any key to return to Menu. \"; getch( ); } /**************************************************************************/ //----------------------- print_reverse_list( ) ------------------------// /**************************************************************************/ void Linked_list_Stack::print_reverse_list( ) { if(bottom==NULL) cout<<\"\\n\\n\\n\\n\\n\\t *** Nothing to show. \"<<endl; else { cout<<\"\\n\\n\\n\\n\\n\\t The List in reverse order is : \\n\"<<endl; node *temp; last_entry=top->next; for(print=bottom;print!=NULL;print=print->next) { for(temp=bottom;temp!=last_entry;temp=temp->next) second_last_entry=temp; last_entry=second_last_entry; cout<<\"\\t \"<<last_entry->data<<endl; } } cout<<\"\\n\\n\\n\\t\\t Pres any key to return to Menu. \"; getch( ); } /**************************************************************************/ //----------------- sort_list_in_ascending_order( ) --------------------// /**************************************************************************/ void Linked_list_Stack::sort_list_in_ascending_order( ) { if(bottom==NULL) cout<<\"\\n\\n\\n\\n\\n\\t *** Nothing to sort. \"<<endl; else { node *temp; for(print=bottom;print!=NULL;print=print->next) { for(temp=bottom;temp!=NULL;temp=temp->next) { int data_1=print->data; int data_2=temp->data; if(data_1<data_2) { print->data=data_2; temp->data=data_1; } } } print=bottom; cout<<\"\\n\\n\\n\\n\\n\\t The linked list in Ascending order is : \\n\"<<endl; while(print!=NULL) { cout<<\"\\t \"<<print->data<<endl; print=print->next; } } cout<<\"\\n\\n\\n\\t\\t Pres any key to return to Menu. \"; getch( ); } /**************************************************************************/ //----------------- sort_list_in_decending_order( ) --------------------// /**************************************************************************/ void Linked_list_Stack::sort_list_in_decending_order( ) { if(bottom==NULL) cout<<\"\\n\\n\\n\\n\\n\\t *** Nothing to sort. \"<<endl; else { node *temp; for(print=bottom;print!=NULL;print=print->next) { for(temp=bottom;temp!=NULL;temp=temp->next) { int data_1=print->data; int data_2=temp->data; if(data_1>data_2) { print->data=data_2; temp->data=data_1; } } } print=bottom; cout<<\"\\n\\n\\n\\n\\n\\t The linked list in Decending Order is : \\n\"<<endl; while(print!=NULL) { cout<<\"\\t \"<<print->data<<endl; print=print->next; } } cout<<\"\\n\\n\\n\\t\\t Pres any key to return to Menu. \"; getch( ); } /**************************************************************************/ //--------------------------- show_working( ) --------------------------// /**************************************************************************/ void Linked_list_Stack::show_working( ) { char Key=NULL; do { clrscr( ); gotoxy(5,5); cout<<\"********** Implementation of Linked List as a Stack **********\"<<endl; gotoxy(10,8); cout<<\"Select one of the listed operation :\"<<endl; gotoxy(15,10); cout<<\"- Press \\\'P\\\' to Push a value\"<<endl; gotoxy(15,12); cout<<\"- Press \\\'O\\\' to Pop a value\"<<endl; gotoxy(15,14); cout<<\"- Press \\\'S\\\' to Print the list in Original order\"<<endl; gotoxy(15,16); cout<<\"- Press \\\'R\\\' to Print the list in Reverse order\"<<endl; gotoxy(15,18); cout<<\"- Press \\\'A\\\' to Sort the list in Ascending order\"<<endl; gotoxy(15,20); cout<<\"- Press \\\'D\\\' to Sort the list in Decending order\"<<endl; gotoxy(15,22); cout<<\"- Press \\\'E\\\' to Exit\"<<endl; Input: gotoxy(10,26); cout<<\" \"; gotoxy(10,26); cout<<\"Enter your Choice : \"; Key=getche( ); 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_original_list( ); else if(Key==\'r\' || Key==\'R\') print_reverse_list( ); else if(Key==\'a\' || Key==\'A\') sort_list_in_ascending_order( ); else if(Key==\'d\' || Key==\'D\') sort_list_in_decending_order( ); else goto Input; } while(1); } int main( ) { Linked_list_Stack obj; obj.show_working( ); return 0; }