C++ Programming Tutorial

 
 
 

Data File Structure Programs

Array

Insert, edit, delete, append, display, Srch. Insert, delete, merge, delete multiple occurrences Arrays as a Stack in graphics

Stack

Stack operations using array Stack using static memory allocation Stack using dynamic memory allocation Double ended link list as a stack Lnked list as a Stack Infix expr. to Postfix expr. Postfix expr. into an Infix expr. Arrays as a Stack in graphics Stack as an Arithmetic expr. Evaluater Graphical Rep. of Stack Stack to traverse - inodr, postodr, preodr

Queue

Queue using static memory allocation Queue using dynamic memory allocation Circular queue Linked list as a Queue Double Ended linked list as a Queue Graphical Rep. of Queue Arrays as a Linear Queue Array as a Circular Queue Arrays as a Linear Queue ( in graphics ) Arrays as a Circular Queue ( in graphics )

Linked List

Singly link list Circular linked list Doubly link list Linked list as a Queue Linked list as a Stack Double Ended linked list as a Queue Double Ended linked list as a Stack Infix to Postfix - Linked List as Stack Circular doubly link list Single Ended Linked List - Sorting in both odr Hashing - double ended Linked List Sort of link list

Tree

Linked List as a Binary Srch. Tree Set Class using Binary Srch. Tree Maximum depth of Binary Srch. Tree Minimum Spaning Tree Prims algo - minimum spanning tree Traverse binary tree - inodr, preodr, post Find number in binary Srch. tree display levell

Sorting

Bubble Sort Selection Sort Insertion Sort Radix Sort Merge Sort Quick Sort Heap Sort Linear Sort Shell Sort Topological Sort

Searching

Linear Srch. or Sequential Srch. Binary Search Breadth First Search Traversal Depth First Search Traversal Shortest Path-Given Source-Destination-Dijkstras

Hashing

Simple implementation of Hashing Hashing using double-ended Linked List Hashing using Mid-Square Method Example of Hashing n term of the fibonacci series using recursion

Recursion

Factorial of the given number using recursion Mystery of Towers of Hanoi using recursion
 
 

Image

 # 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;
    }