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
 
 
 #include<iostream.h>
 #include<graphics.h>
 #include<stdlib.h>
 #include<conio.h>
 #include<math.h>
 #include<dos.h>

 const max_length=10;

 class Queue
    {
       private:
        long queue[max_length];
        int flag[max_length];
        int rear;
        int front;

       public:
        Queue( );
        void Delete( );
        void Insert( );
        void waiting( );
        long get_value( );
        void show_screen( );
        void show_working( );
    };



 //--------------------------  Queue( )  ---------------------------------//


 void Queue::Queue( )
    {
       rear=-1;
       front=-1;

       for(int count=0;count<max_length;count++)
      {
         queue[count]=0;
         flag[count]=0;
      }
    }


 //----------------------  get_value( )  ---------------------------------//


 long Queue::get_value( )
    {
       int count=0;

       char String[10]={NULL};

       setcolor(11);
       settextstyle(2,0,7);
     outtextxy(40,250,\"Enter the element : \");
     outtextxy(41,250,\"Enter the element : \");

       do
      {
         int key_code=0;

         char key=NULL;

         if(kbhit( ))
        {
           key=getch( );
           key_code=int(key);
        }

         if( (count>0 && count<5) && key_code==13)
        break;

         else if( (key_code>=48 && key_code<=57 || key_code==46) &&
                                     count<4)
        {
           String[count]=key;

           count++;
        }

         else if(key_code==8 && count>0)
        {
           setfillstyle(10,6);
             bar(260,255,320,275);

           count--;
           String[count]=NULL;
        }

         setcolor(11);
         settextstyle(2,0,6);

         moveto(265,255);
           outtext(String);

         moveto(265,255);
           outtext(String);

         int x=getx( );
         int y=gety( )+9;

         while(!kbhit( ))
        {
           settextstyle(0,0,1);
             setcolor(15);
               moveto(x+2,y);
             outtext(\"_\");

           delay(250);

             setcolor(0);
               moveto(x+2,y);
             outtext(\"_\");

           delay(200);
           }
      }
       while(count<6);

       delay(500);

       setfillstyle(10,6);
     bar(35,255,320,275);

       long number=atol(String);

       return number;
    }


 //-------------------------------  Insert( )  ---------------------------//


 void Queue::Insert( )
    {
       long item=get_value( );

       if( (rear==front-1) || (front==0 && rear==max_length-1) )
      {
         setcolor(11);
         settextstyle(1,0,4);
           outtextxy(30,250,\"Queue is Full\");

         delay(1000);

         setfillstyle(10,6);
           bar(30,260,300,290);
      }

       else if(rear==-1 && front==-1)
      {
         rear++;
         queue[rear]=item;
         front=rear;
         flag[rear]=1;
      }

       else if(rear==max_length-1 && front!=0)
      {
         rear=0;
         queue[rear]=item;
         flag[rear]=1;
      }


       else
      {
         rear++;
         queue[rear]=item;
         flag[rear]=1;
      }
    }


 //---------------------------  Delete( )  -------------------------------//


 void Queue::Delete( )
    {
       if(rear==-1 && front==-1)
      {
         setcolor(11);
         settextstyle(1,0,4);
           outtextxy(30,250,\"Queue is Empty\");

         delay(1000);

         setfillstyle(10,6);
           bar(30,260,300,290);
      }

       else
      {
         queue[front]=0;
         flag[front]=0;

         if(front==rear)
        {
           front=-1;
           rear=-1;
        }

         else if(front==max_length-1)
        front=0;

         else
        front++;
      }
    }


 //-------------------------  show_screen( )  ----------------------------//


 void Queue::show_screen( )
    {
       for(int count_1=0;count_1<5;count_1++)
      {
         setcolor(7);
           rectangle(count_1,count_1,getmaxx( )-count_1,
                             getmaxy( )-count_1);
      }

       setfillstyle(10,6);
     bar(5,5,getmaxx( )-5,getmaxy( )-5);

       setfillstyle(1,7);
     bar(80,60,550,63);

       settextstyle(7,0,3);
     setcolor(8);
       outtextxy(108,12,\"Circular\");
       outtextxy(109,12,\"Circular\");

     setcolor(10);
       outtextxy(110,10,\"Circular\");
       outtextxy(111,10,\"Circular\");

       settextstyle(1,0,5);
     setcolor(8);
       outtextxy(78,17,\"Queue Implementation\");
       outtextxy(79,17,\"Queue Implementation\");

     setcolor(12);
       outtextxy(80,15,\"Queue Implementation\");
       outtextxy(81,15,\"Queue Implementation\");

       settextstyle(1,0,5);
     setcolor(8);
       outtextxy(418,382,\"Queue\");
       outtextxy(419,382,\"Queue\");

     setcolor(15);
       outtextxy(420,380,\"Queue\");
       outtextxy(421,380,\"Queue\");

       settextstyle(7,0,3);
     setcolor(8);
       outtextxy(448,377,\"Circular\");
       outtextxy(449,377,\"Circular\");

     setcolor(14);
       outtextxy(450,375,\"Circular\");
       outtextxy(451,375,\"Circular\");

       settextstyle(8,0,4);
     setcolor(8);
       outtextxy(18,82,\"Press :\");
       outtextxy(19,82,\"Press :\");

     setcolor(3);
       outtextxy(20,80,\"Press :\");
       outtextxy(21,80,\"Press :\");

       setfillstyle(2,9);
     bar(16,118,135,121);

       setcolor(14);
       settextstyle(2,0,6);
     outtextxy(50,130,\"<I> to Insert an element\");
     outtextxy(51,130,\"<I> to Insert an element\");

     outtextxy(50,150,\"<D> to Delete an element\");
     outtextxy(51,150,\"<D> to Delete an element\");

     outtextxy(50,170,\"<E> to Exit\");
     outtextxy(51,170,\"<E> to Exit\");

       setcolor(11);
     outtextxy(60,130,\"I\");
     outtextxy(61,130,\"I\");

     outtextxy(60,150,\"D\");
     outtextxy(61,150,\"D\");

     outtextxy(60,170,\"E\");
     outtextxy(61,170,\"E\");

       setcolor(7);
       settextstyle(0,0,1);
     outtextxy(430,452,\"* CopyRights (C) 2000-02\");
     outtextxy(430,465,\"* Muhammad Tahir Shahzad\");

       setcolor(15);
     rectangle(429,79,531,379);

       setfillstyle(1,0);
     bar(430,80,530,378);

       char string[10][10]={\"Queue[9]\",\"Queue[8]\",\"Queue[7]\",\"Queue[6]\",
                \"Queue[5]\",\"Queue[4]\",\"Queue[3]\",\"Queue[2]\",
                \"Queue[1]\",\"Queue[0]\"};

       for(int count_2=0;count_2<max_length;count_2++)
      {
         setcolor(15);
           line(429,79+(count_2*30),530,79+(count_2*30));

         setcolor(15);
         settextstyle(0,0,1);
           outtextxy(355,92+(count_2*30),string[count_2]);
      }
    }


 //-------------------------  show_working( )  ---------------------------//


 void Queue::show_working( )
    {
       show_screen( );

       int key_code=0;

       do
      {
         waiting( );

         char key=\'\\0\';

         key=getch( );
         key_code=int(key);

         if(key_code==105 || key_code==73)
        Insert( );

         else if(key_code==100 || key_code==68)
        Delete( );

         else if(key_code==27 || key_code==101)
        exit(0);

         setfillstyle(10,6);
         bar(540,80,620,400);

         for(int count_1=0;count_1<max_length;count_1++)
        {
           int flag=0;

           if(count_1==rear && flag==0)
              {
             setfillstyle(1,9);
               bar(550,358-(count_1*30),570,363-(count_1*30));

             setcolor(9);

             for(int count_2=0;count_2<=10;count_2++)
                   line(540,363-(count_1*30),550,
                        363-(count_1*30)-count_2);

             setcolor(7);
             settextstyle(0,0,1);
               outtextxy(577,357-(count_1*30),\"Rear\");

             flag=1;
              }

           flag=0;

           if(count_1==front && flag==0)
              {
             setfillstyle(1,11);
               bar(550,368-(count_1*30),570,373-(count_1*30));

             setcolor(11);

             for(int count_3=0;count_3<=10;count_3++)
                line(540,368-(count_1*30),550,
                          368-(count_1*30)+count_3);

             setcolor(7);
             settextstyle(0,0,1);
               outtextxy(577,369-(count_1*30),\"Front\");

             flag=1;
              }
        }

         delay(500);

         for(int count_4=0;count_4<max_length;count_4++)
        {
           if(flag[count_4]!=0)
              setfillstyle(1,count_4+1);

           else
              setfillstyle(1,0);

            bar(430,378-(count_4*30),530,350-(count_4*30));
        }

         for(int count_5=0;count_5<max_length;count_5++)
        {
           setcolor(15);
           settextstyle(0,0,1);

           char value[6]={\'\\0\'};

           itoa(queue[count_5],value,10);

           if(flag[count_5]!=0)
              outtextxy(460,360-(count_5*30),value);
        }
      }
       while(key_code!=27);
    }


 //----------------------------  waiting( )  -----------------------------//


 void Queue::waiting( )
    {
    do
       {
          setfillstyle(1,4);
        bar(51,440,99,445);

          setfillstyle(1,10);
        bar(101,440,149,445);

          setfillstyle(1,9);
        bar(151,440,199,445);

          setfillstyle(1,14);
        bar(201,440,249,445);

          delay(300);

          setfillstyle(10,6);
        bar(51,440,99,445);

          delay(150);

        bar(101,440,149,445);

          delay(150);

        bar(151,440,199,445);

          delay(150);

        bar(201,440,249,445);

          delay(150);

          setfillstyle(1,4);
        bar(51,440,99,445);

          delay(150);

          setfillstyle(1,10);
        bar(101,440,149,445);

          delay(150);

          setfillstyle(1,9);
        bar(151,440,199,445);

          delay(150);

          setfillstyle(1,14);
        bar(201,440,249,445);
       }
    while(!kbhit());

    setfillstyle(10,6);
      bar(51,440,99,445);
      bar(101,440,149,445);
      bar(151,440,199,445);
      bar(201,440,249,445);
   }

 main( )
    {
       int driver=VGA;
       int mode=VGAHI;

       initgraph(&driver,&mode,\"..\\\\Bgi\");

       Queue obj;

       obj.show_working( );

       closegraph( );
       return 0;
    }