Home Forums C Programming Can You Help Me Plz! Reply To: Can You Help Me Plz!

#3417
Humayan
Participant

You want to add a stack to the program? Heres a stack I wrote for another project:
 

/****************************************************************
* File Name : c:programshelpshell.cpp
* Date : December,26,2007
* Comments : new project
* Compiler/Assembler :
* Program Shell Generated At: 3:08:45 p.m.
=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include < iostream >
//#include < string.h >
//#include < conio.h >
//#include < math.h >
//#include < iomanip >
//#include < ctype.h >
using namespace std;
struct stackNode
{
 fretNode * current;
 fretNode * first;
 fretNode * last;
 int repeats;
 stackNode * next;
};
struct stateNode
{
    fretNode * fp;
    fretNode * cp;
    fretNode * cfp;
    fretNode * cbp;
    stackNode * cs;
    stackNode * fs;
    stateNode * next;
};
 

//>>>>>>>>>>>>>>>>>>>>>>>> GLOBAL DATA <<<<<<<<<<<<<<<<<<<<<<<<<
    bool stackError = false;
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@
 bool push  ( stackNode *& stack , fretNode * current , fretNode * first , fretNode * last , int repeats );
 bool pop   ( stackNode *& stack );
 void clear ( stackNode *& stack );
 bool copy ( stackNode *& destination , stackNode * source );
   // stackNode * clone ( stackNode * stackToClone );
    bool pushState ( stateNode *& stack , fretNode * currentPattern , fretNode * currentFrontPattern , fretNode * currentBackPattern , stackNode * currentStack , stackNode * frontStack );
    bool popState ( stateNode *& stack , fretNode *& currentPattern , fretNode *& currentFrontPattern , fretNode *& currentBackPattern , stackNode *& currentStack , stackNode *& frontStack );
 void clear ( stateNode *& stack );
 bool copy ( stateNode *& destination , stateNode * source );
    void setState ( stateNode *& stack , fretNode *& currentPattern , fretNode *& currentFrontPattern , fretNode *& currentBackPattern , stackNode *& currentStack , stackNode *& frontStack );
//##################################################################################
 

/******************************* FUNCTION DEFINITION ******************************
 Name : push
 Parameters :
  current a(n) fretNode * ( fretNode * )  ,
  first a(n) fretNode * ( fretNode * )  ,
  last a(n) fretNode * ( fretNode * )  ,
  repeats a(n) int

 Returns: Void type
 Comments:
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
bool push ( stackNode *& stack , fretNode * current , fretNode * first , fretNode * last , int repeats )
{
    stackNode * s = new stackNode;
    if ( ! s )
    {
        stackError = true;
        return false;
    }
    s -> current = current;
    s -> first = first;
    s -> last = last;
    s -> repeats = repeats;
    s -> next = stack;
 stack = s;
 
 return true;
}
/******************************* FUNCTION DEFINITION ******************************
 Name : pop
 Parameters :
  void

 Returns: Void type
 Comments:
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
bool pop ( stackNode *& stack  )
{
    if ( stack )
    {
        stackNode * temp = stack;
        stack = stack -> next;
        delete temp;
      
        return true;
    }
    return false;
}
/******************************* FUNCTION DEFINITION ******************************
 Name : clear
 Parameters :
  void

 Returns: Void type
 Comments:
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void clear ( stackNode *& stack )
{
    stackNode * temp;
   
    while ( stack )
    {
        temp = stack;
        stack = stack -> next;
        delete temp;
    }
    stack = 0;
    return;
}
/******************************* FUNCTION DEFINITION ******************************
 Name : copy
 Parameters :
  destination a(n) stackNode * ( stackNode * )  ,
  source a(n) stackNode ( stackNode )

 Returns: user defined type , bool
 Comments:
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
bool copy ( stackNode *& destination , stackNode * source )
{
    int i = 0;
    stackNode * last = 0;
   
    destination = 0;
   
    while ( source )
    {
        stackNode * s = new stackNode;
        if ( ! s )
        {
            stackError = true;
            return false;
        }
        if ( i == 0 )
        {
            destination = s;
            i ++;
        }
        if ( last )
            last -> next = s;
       
       
        s -> current = source -> current;
        s -> first = source -> first;
     s -> last = source -> last;
        s -> repeats = source -> repeats;
        s -> next = 0;
        last = s;
        source = source -> next;
 }
 
 return true;
}
/******************************* FUNCTION DEFINITION ******************************
 Name : pushState
 Parameters :
  stack a(n) stateNode *& ( stateNode *& )  ,
  frontPattern a(n) stateNode * ( stateNode * )  ,
  currentPattern a(n) stateNode * ( stateNode * )  ,
  currentFrontPattern a(n) stateNode * ( stateNode * )  ,
  currentBackPattern a(n) stateNode * ( stateNode * )  ,
  currentStack a(n) stackNode * ( stackNode * )  ,
  frontStack a(n) stackNode * ( stackNode * )

 Returns: user defined type , bool
 Comments:
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
bool pushState ( stateNode *& stack , fretNode * currentPattern , fretNode * currentFrontPattern , fretNode * currentBackPattern , stackNode * currentStack , stackNode * frontStack )
{
   
    stateNode * s = new stateNode;
    if ( ! s )
    {
        stackError = true;
        return false;
    }
   
   // s -> fp = frontPattern;
    s -> cp = currentPattern;
    s -> cfp = currentFrontPattern;
    s -> cbp = currentBackPattern;
    copy ( s -> cs , currentStack );
    copy ( s -> fs , frontStack );
    s -> next = stack;
    stack = s;

 return true;
}
/******************************* FUNCTION DEFINITION ******************************
 Name : popState
 Parameters :
  stack a(n) stackNode * ( stackNode * )  ,
  frontPattern a(n) stateNode *& ( stateNode *& )  ,
  currentPattern a(n) stateNode *& ( stateNode *& )  ,
  currentFrontPattern a(n) stateNode *& ( stateNode *& )  ,
  currentBackPattern a(n) stateNode *& ( stateNode *& )  ,
  currentStack a(n) stackNode *& ( stackNode *& )  ,
  frontStack a(n) stackNode *& ( stackNode *& )

 Returns: user defined type , bool
 Comments:
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
bool popState ( stateNode *& stack , fretNode *& currentPattern , fretNode *& currentFrontPattern , fretNode *& currentBackPattern , stackNode *& currentStack , stackNode *& frontStack )
{
    if ( stack == 0 )
    {
        return false;
    }
   
   // frontPattern = stack -> fp;
    currentPattern = stack -> cp;
    currentFrontPattern = stack -> cfp;
    currentBackPattern = stack -> cbp;
    currentStack = stack -> cs;
    frontStack = stack -> fs;
    stack = stack -> next;
   
    return true;
}
/******************************* FUNCTION DEFINITION ******************************
 Name : clear
 Parameters :
  void

 Returns: Void type
 Comments:
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void clear ( stateNode *& stack )
{
    stateNode * temp;
   
    while ( stack )
    {
        temp = stack;
        stack = stack -> next;
        clear ( temp -> cs );
        clear ( temp -> fs );
        delete temp;
    }
    stack = 0;
    return;
}
/******************************* FUNCTION DEFINITION ******************************
 Name : copy
 Parameters :
  destination a(n) stackNode * ( stackNode * )  ,
  source a(n) stackNode ( stackNode )

 Returns: user defined type , bool
 Comments:
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
bool copy ( stateNode *& destination , stateNode * source )
{
    int i = 0;
    stateNode * last = 0;
   
    destination = 0;
    while ( source )
    {
        stateNode * s = new stateNode;
        if ( ! s )
        {
            stackError = true;
            return false;
        }
        if ( i == 0 )
        {
            destination = s;
            i ++;
        }
        if ( last )
            last -> next = s;
       
        //s -> fp = source -> fp;
        s -> cp = source -> cp;
        s -> cfp = source -> cfp;
     s -> cbp = source -> cbp;
        copy ( s -> cs , source -> cs );
        copy ( s -> fs , source -> fs );
        s -> next = 0;
        last = s;
        source = source -> next;
 }
 
 return true;
}
/******************************* FUNCTION DEFINITION ******************************
 Name : setState
 Parameters :
  stack a(n) stateNode *& ( stateNode *& )  ,
  currentPattern a(n) fretNode *& ( fretNode *& )  ,
  currentFrontPattern a(n) fretNode *& ( fretNode *& )  ,
  currentBackPattern a(n) fretNode *& ( fretNode *& )  ,
  currentStack a(n) stackNode *& ( stackNode *& )  ,
  frontStack a(n) stackNode *& ( stackNode *& )

 Returns: user defined type , void
 Comments:
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void setState ( stateNode *& stack , fretNode *& currentPattern , fretNode *& currentFrontPattern , fretNode *& currentBackPattern , stackNode *& currentStack , stackNode *& frontStack )
{
    currentPattern = stack -> cp;
    currentFrontPattern = stack -> cfp;
 currentBackPattern = stack -> cbp;
    currentStack = stack -> cs;
    frontStack = stack -> fs;
 return;
}