The goal of this project was to create a class (named lifo) of Last-In-First-Out Lists (Stacks) of integers with variable length. The class actually defines a new Data Type, named lifo. Each variable of this type will be a LIFO List. Of course, the class must have defined some operations (member-functions) for handling the lists, ie PUSH an integer to a stack, POP an integer from the stack, PRINT a list, COPY one list to another, COMPARE two lists, CONCATENATE two lists, SEARCH a list for an element etc.
The program is quite simple. It creates an “array” a of 100 elements of type lifo. Thus, it creates 100 LIFO lists and displayes a simple menu as user interface with the above actions.

It was written in Borland C++ v.5 but I think you can compile and run it in any compiler due of its simplicity. It only requires the iostream.h for cout and cin.

#include       /* The mostly used library. Needed */
#include          /* Used for the function getch()   */
 
/* Boolean in C++.
Used by the member functions of the class         */
enum Bool {False,True};    
 
/* HEADER AND DECLARATION ONLY
  OF THE CLASS LIFO      */
 class lifo {
  public :
  lifo ()                   ;
  void push (int)           ;
  int  pop ()               ;
  void print ()             ;
  void seek ()              ;
  void copy (lifo& )        ;
  void concat (lifo&, lifo&);
  Bool compare (lifo&)      ;
  Bool member( int )        ;
  int len ()                ;
  int top ()                ;
  ~lifo ()                  ;
  private :
  int *list,*temp           ;
  int length                ;
 };
 
void main (){
             /* the answer-key in MAINMENU */
 char key;
 int x,y,z, num,m;
 /* Creates an array-style object of LIFO */
 lifo a[100];   
 
    cout << "\n      Identification           ";
    cout << "\n Coded by KALAMARAS DIMITRIS   ";
    cout << "\n This is my diploma project    ";
 
/* default list number is 0 */
 num=0;
/* printing of main menu -- repeated by for loop*/
 for ( ;; )
  {
   cout << "\n";
       cout << "\n/* MAIN MENU */\n";
       cout << "\n(_1_)  Push x ";
       cout << "\n(_2_)  Pop x ";
       cout << "\n(_3_)  Print List ";
       cout << "\n(_4_)  Search element ";
       cout << "\n(_5_)  Check element";
       cout << "\n(_6_)  Choose Active List ";
       cout << "\n(_7_)  Copy List ";
       cout << "\n(_8_)  Compare Lists";
       cout << "\n(_9_)  Concatenate Lists";
       cout << "\n(_0_)  Return TOP    ";
       cout << "\n(_E_)  Exit\n ";
       cout << "\n< Current List : " << num <<", Length = "
        << a[num].len () << " > ";
   cout << "\n> " ;
   cin >> key;
   cout << "\n\n";
   switch (key)
   {
      case '1'  :    {   cout << "/* PUSH */\n";
             cout << "Enter integer to push : ";
             cin >> x;
             a[num].push (x);  /* push x onto a(num) */
             break;
           }
      case '2'  :    {   cout << " /* POP */ ";
             if (a[num].len () != 0 )
             {
             cout << "\nLast entered integer : "
              << a[num].pop ();
             cout << "\n\nPress any key.";
             m=getch();
             break ;
             }
             else a[num].pop();
             cout << "\nPress any key.";
             m=getch();
             break;
           }
      case '3'  :    {   cout << "/* PRINT LIST */\n";
             a[num].print ();
             cout << "\n\nPress any key.";
             m=getch();
             break;
           }
      case '4'  :    {   a[num].seek ();  break;  }
      case '5'  :    {   cout << "/* CHECK */ \n";
             cout << "Enter int:"; cin >> x ;
             if (a[num].member (x)) cout <<"\nFound!\n";
             else cout << "\n Not Found.";
             cout << "\n\nPress any key.";
             m=getch();
             break;
            }
      case '6'  :    {   cout << " /* CHANGE CURRENT LIST */ ";
             cout << "\nChoose LIFO list number (0-99): ";
             cin >> num; break;
           }
      case '7'  :    {   cout << "/* COPY LISTS  */ " ;
             cout << "\nSource List : "; cin >> x ;
             cout << "\nTarget List : "; cin >> y;
             a[x].copy ( a[y] );
             cout <<"\n List : "<< y ;
              a[y].print ();
             cout << "\n\nPress any key.";
             m=getch();
             break;
            }
      case '8'  :    {   cout << "/* COMPARE LISTS */ ";
             cout << "\nFirst List : " ; cin >> x;
             cout << "\nSecond List : " ; cin >> y;
              if ( ! a[x].compare ( a[y] ) )
              {cout << "\nLists are different." ;}
              else
              {cout << "\nLists " << x << " and "
                << y << " are the same. ";}
              cout << "\n\nPress any key.";
             m=getch();
              break;
            }
      case '9'   :   {   cout << "/* LISTS' CONCATENATION */ ";
             cout << "\nFirst List : "; cin >> x;
             cout << "\nSecond List : "; cin >> y ;
             cout << "\nTarget List : "; cin >> z;
             a[y].concat ( a [x], a[z] );
             cout << "\n List : " << z ;
             a[z].print ();
             cout << "\n\nPress any key.";
             m=getch();
             break;
            }
      case '0'  :    {  cout << "/* TOP */ ";
            if (a[num].len() != 0 )
            {
            cout << "\n Top Of List ["<< num << "] is : ";
            cout << a[num].top();
            cout << "\n\nPress Any Key To Continue.\n";
            m=getch();
            break;
            }
            else break;
           }
      case 'E'  :
      case 'e'  :    {      goto telos ;  }
      default   : { cout << "Wrong. Try Again." ; break; }
   }
  }
   telos:  cout << "\n\nGoodbye.";
}
 
/* Construction-member function of class lifo. Initial list Length=0
 list is a pointer to an array [] of integers. Its length will increase
 by one after a successfull PUSH and decrease by one after a POP.
 temp is a pointer to a temporary array [] of integers. It will be
 used for storing list's elements during PUSH and POP actions. */
  lifo::lifo ()   {
    length=0;
    list=new int [length];
    temp = new int [length];
  }
 
/*
  performs the PUSH action of an integer in the list.
  The integer x is passed  by value as the paremeter of the function.
  list is copied onto temp. list is deleted, then recreated with length
  increased by one. At the end, temp is copied to list.   */
 void lifo :: push (int x )  {
    if ( length == 0 )   {
     delete  [] list;
     list = new int [++length];
     list[length-1] = x;
    }
    else   {
     delete [] temp;
     temp = new int [length] ;
     for (register i=0 ; i <= length-1 ; i++ )    {
      temp [i] = list [i];
     }
     delete [] list;
     list = new int [++length];
     for ( register i=0 ; i <= (length-2) ; i++ )  {
      list [i] = temp [i];
     }
     list [length-1] = x;
   }
 }
 
/*  performs the POP action of the last entered integer
 from the active list. List must be of Last In First Out type. */
 int lifo :: pop ()   {
   int top;
   if  ( length == 1 )   {
      top = list [length-1];
      delete [] list;
      list = new int [--length];
      return top ;
   }
   else if ( length == 0 )       {
        cout << " \nError. UNDERFLOW.";
        return -1;
    }
   else   {
    delete [] temp;
    temp = new int [length];
    for ( register i=0 ; i <= length-1 ; i++ )
     {    temp [i] = list [i];        }
    delete [] list;
    list = new int [--length];
    for ( register i=0 ; i <= length-1 ; i++ )
     {       list [i] = temp [i];       }
    top = temp [length] ;
    return  top;
   }
 }
 
/* performs the PRINT action of the active list */
 void lifo::print ( )   {
    cout << "\n";
    for ( register i = 0 ; i <= length-1 ; i ++ )  {
     cout << "[ " << list [i] << " ]" << "\t";
   }
 }
 
/*  performs the SEARCH action in the list for an element */
 void lifo :: seek () {
  int m,x;
  cout << "\nEnter element to seek : ";
  cin >> x;
  if (! member ( x ) )    {
    cout << "\nSorry. Not Found.\n";
    cout << "\nPress any key.";
     m=getch();
  }
  else   {
     for ( register i=0 ; i<=length-1 ; i++)  {
      if (list[i] == x )    {
       cout << "\nFound at "
        << (i+1)
        << " position of current stack.";
       cout << "\nPress any key.";
       m=getch();
      }
    }
  }
 }
 
/* Returns True if the given integer
 exists in the active list and False in the opposite case. */
  Bool lifo :: member ( int x )   {
   for (register i=0 ; i<=length -1 ; ++i)
    if ( list[i] == x ) return True;
   return False ;
  }
 
/* performs the COPY action of one list to another.
/* Target list b is passed by reference in the function.  */
  void lifo :: copy ( lifo &b  )   {
   b.list = new int [length];
   for (register i=0 ; i<=length-1 ; i++)
    b.list[i]=list[i];
   b.length=length;
  }
 
/* performs the COMPARE action of two lists.
 Returns True if the lists are identical and False if they are different. */
 Bool lifo :: compare (lifo &b)   {
   if (b.length != length ) return False;
    for (register i=0 ; i<=length-1 ; i++)
     if ( b.list[i] != list[i] ) return False ;
   return True;
   }
 
/* performs the CONCATENATION action of two lists
 into a third one. The first list is always a, the second b and the
 target list is c. Both b,c lists are passed by reference &*/
  void lifo :: concat (lifo &b, lifo &c)  {
   b.copy (c);
   for ( register i=0 ; i<=length-1 ; ++i)
      c.push ( list[i] ) ;
  }
 
/* returns the LENGTH of the active list */
 int lifo :: len ( )  {
    return length;
  }
 
/* prints out the TOP element of the stack. */
  int lifo :: top ()  {
  return list[length-1];
 }
 
/* Deletes the pointers remaining alive. */
  lifo :: ~lifo ()  {
   delete [] list;
   delete [] temp;
 }
Share it!