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_main_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==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
      {
         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=rear=-1;

         else
        front++;
      }
    }


 //-----------------------  show_main_screen( )  -------------------------//


 void Queue::show_main_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,\"Linear\");
       outtextxy(109,12,\"Linear\");

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

       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,\"Linear\");
       outtextxy(449,377,\"Linear\");

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

       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_main_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,615,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;
    }

    Related Post:
  1. Program to show the implementation of Window-to-Viewport Coordinate Transformaton

  2. Program that provides facilities of calculator in c++

  3. Program to illustrate the functions returning pointers

  4. Program to illustrate the use of inline functions

  5. Program that performs selection search

  6. Program that provides example of dynamic constructor

  7. Program to read a Non-Linear equation in one variable, then evaluate it using Modified False-Position Method and display its kD accurate root

  8. Program to print, read and calculate sum, smallest, largest element an array using pointers

  9. PROGRAM FOR LL(1) PARSING

  10. Program to fill different types of geometric shapes using Flood Fill Algorithm (Using Linked-List)

  11. Program that take font and background color and text input from a user and display it in center aligned

  12. Program for election contest among five candidates

  13. Program to read marks of 10 students for 4 subjects and compute and display total marks and status of each student

  14. Program to rotate the word Computer by 1 results in rCompute

  15. Program to estimate the Differential value of the function at the given point from the given data using Mid-Point Method

  16. Program to estimate the Integral value of the function at the given points from the given data using Romberg Method

  17. Program to illustrate the Bubble Sorting of arrays

  18. Program to show the implementation of Liang-Barsky Line Clipping Algorithm

  19. Program to create a queue using static memory allocation

  20. Program to show the 3D Reflection Transformation along zx-plane