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
 
 
 /**************************************************************************
  **************************************************************************

     A C++ Program to illustrate the implementation of arrays as
     a Stack ( in graphics ).

 **************************************************************************
 **************************************************************************/

 #include<iostream.h>
 #include<graphics.h>
 #include<stdlib.h>
 #include<conio.h>
 #include<math.h>
 #include<dos.h>

 const max_length=10;

 /***********************************************************************/
 //------------------------------  stack  ------------------------------//
 /***********************************************************************/

 class STACK
    {
       private:
        long stack[max_length];
        int flag[max_length];
        int top;

       public:
        STACK( );
        void pop( );
        void push( );
        void waiting( );
        long get_value( );
        void show_main_screen( );
        void show_working( );
    };



 //--------------------------  STACK( )  ---------------------------------//


 void STACK::STACK( )
    {
       top=-1;

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


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


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

       char String[10]={NULL};

       setfillstyle(1,0);
     bar(35,255,320,275);

       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(1,0);
             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(1,0);
     bar(35,255,320,275);

       long number=atol(String);

       return number;
    }


 //---------------------------  push( )  ---------------------------------//


 void STACK::push( )
    {
       long item=get_value( );

       if(top==max_length-1)
      {
         setcolor(11);
         settextstyle(0,0,3);
           outtextxy(30,250,\"Stack is Full\");

         delay(1000);

         setcolor(0);
         settextstyle(0,0,3);
         outtextxy(30,250,\"Stack is Full\");
      }

       else
      {
         top++;
         stack[top]=item;
         flag[top]=1;
      }
    }


 //---------------------------  pop( )  ----------------------------------//


 void STACK::pop( )
    {
       if(top==-1)
      {
         setcolor(11);
         settextstyle(0,0,3);
           outtextxy(30,250,\"Stack is Empty\");

         delay(1000);

         setcolor(0);
         settextstyle(0,0,3);
           outtextxy(30,250,\"Stack is Empty\");
      }

       else
      {
         stack[top]=0;
         flag[top]=0;
         top--;
       }
    }


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


 void STACK::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);
      }

       int x;
       int y;
       int color;

       for(int count_2=0;count_2<3000;count_2++)
      {
         x=random(640);
         y=random(480);
         color=random(15);

         putpixel(x,y,color);
      }

       setfillstyle(1,7);
       bar(80,55,540,58);

       setcolor(9);
       settextstyle(1,0,5);
     outtextxy(80,10,\"Stack Implementation\");
     outtextxy(81,10,\"Stack Implementation\");

       setcolor(10);
       settextstyle(1,0,5);
     outtextxy(445,380,\"Stack\");
     outtextxy(446,380,\"Stack\");

       setcolor(12);
       settextstyle(8,0,4);
     outtextxy(20,80,\"Press :\");
     outtextxy(21,80,\"Press :\");

       setcolor(14);
       settextstyle(7,0,2);
     outtextxy(50,120,\"< P > to Push an element,\");
     outtextxy(50,150,\"< O > to Pop an element,\");
     outtextxy(50,180,\"< Esc > to Exit.\");

       setcolor(7);
       settextstyle(7,0,2);
     outtextxy(51,120,\"  P  \");
     outtextxy(51,150,\"  O  \");
     outtextxy(51,180,\"  Esc \");

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

       setcolor(15);
     rectangle(449,79,551,379);

       setfillstyle(1,0);
     bar(450,80,550,378);

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

       for(int count_3=0;count_3<max_length;count_3++)
      {
         setcolor(15);
           line(449,79+(count_3*30),550,79+(count_3*30));

         setcolor(15);
         settextstyle(0,0,1);
           outtextxy(380,92+(count_3*30),String[count_3]);
      }
    }


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


 void STACK::show_working( )
    {
       show_main_screen( );

       int size=0;

       char *Buffer=NULL;

       size=imagesize(555,80,635,400);
       Buffer=new char[size];

       getimage(555,80,635,400,Buffer);

       int key_code=0;

       do
      {
         waiting( );

         char key=NULL;

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

         if(key_code==112 || key_code==80)
        push( );

         else if(key_code==111 || key_code==79)
        pop( );

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

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

           putimage(555,80,Buffer,0);

           if(count_1==top)
              {
             setfillstyle(1,11);
               bar(565,360-(count_1*30),595,370-(count_1*30));

             int y1=365-(count_1*30);
             int y2=355-(count_1*30);

             setcolor(11);

             for(int count_2=0;count_2<=20;count_2++)
                {
                   line(558,y1,568,y2);

                   y2++;
                }

             setcolor(15);
             settextstyle(2,0,6);
               outtextxy(600,y1-10,\"Top\");
               outtextxy(601,y1-10,\"Top\");

             flag=1;
        }
        if(flag)
          break;
         }

      delay(500);

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

        else
           setfillstyle(1,0);

        bar(450,378-(count_3*30),550,350-(count_3*30));
         }

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

        char value[6]={NULL};

        itoa(stack[count_4],value,10);

        if(flag[count_4]!=0)
           outtextxy(480,360-(count_4*30),value);
         }

      }
       while(key_code!=27);
    }


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


 void STACK::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(1,0);
        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(1,0);
      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\");

       STACK obj;

       obj.show_working( );

       closegraph( );
       return 0;
    }

    Related Post:
  1. Program to show the 3D Reflection Transformation along zx-plane

  2. Program to create a circular linked list

  3. Program to draw a circle using Polynomial Method

  4. Program to draw a line using Bresenhams Line Algorithm (BLA)

  5. Program that provides an example of passing structure as parameter using call by reference

  6. Program of that provides an example of function overloading

  7. Program that prints odd numbers, even numbers and the numbers that are divisible by 5 b/w 0 to 50

  8. Program that reads marks obtained by a student in a test of 100 marks and print pass if marks are greater than or equal to 50

  9. Program to illustrate static member functions

  10. Program to show the projection of 3D objects using Orthographics Parallel Projection w.r.t. xy-plane and along vector v

  11. Program that reads two numbers from user, computes and display their sum.

  12. Program to translate integer numbers given in words into numeric representation

  13. Program that list of letters should be an alphabetical list of upper case letters followed by an alphabetical list of lower case letters

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

  15. Program to reverse the sequence of the words in the sentence and display the result

  16. Program to show the implementation of Window-to-Viewport Coordinate Transformaton

  17. Program to draw a line using Parametric equations

  18. Program to read a Linear System of Equations,then evaluate it by using Guass-Seidel Itrative Method and show the result

  19. Program to illustrate the use of friend classes

  20. Program to illustrate the use of call-by-refrence method in functions

 
 
Didn't find what you were looking for? Find more on Program to illustrate the implementation of arrays as a Stack in graphics