|
From: chromatonus 7. <chr...@ho...> - 2001-11-30 02:28:40
|
hi, may I know how to display elements and count the number of elements in a
stack defined below, without destruction? thank you very much!
// Program: STACK 2
// Objective: To understand how a stack works as implemented by a class
// and an array for the data
// Last modified 11-29-01
#include <iostream.h>
#include <conio.h>
const max_stack = 100;
class stacktype
{
public:
stacktype();
// default constructor
// postcondition: initializes stack by setting TOP = 0
void initialize();
// postcondition: erase stack by setting TOP = 0
void push( int );
// postcondition: If the stack is not full,
// increase TOP by 1 , then
// put info into DATA[TOP]
int pop();
// postcondition: If the stack is not empty,
// put DATA[TOP] into x,
// decrease TOP by 1,
// return x
bool is_empty();
// postcondition: Return true if stack is empty, false otherwise.
bool is_full();
// postcondition: Return true if stack is full, false otherwise.
private:
int mydata[max_stack];
int mytop; // subscript of the top of the stack
};
//==============================================================================
//============================== Free functions
================================
//==============================================================================
void display_top( stacktype );
// precondition: send in stack
// postcondition: display, without destruction, the value in data[top]
void display_stack( stacktype );
// precondition: send in stack
// postcondition: display, without destruction, the values of all the
// elements in the stack, in the order entered.
int count_values( stacktype );
// precondition: send in stack
// postcondition: return the number of data pieces
int menu();
// postcondition: initial screen. Display options, allow user to enter
// choice and return this value
//==============================================================================
//============================== Implementation
================================
//==============================================================================
stacktype::stacktype()
{
mytop = 0;
}
void stacktype::initialize()
{
mytop = 0;
}
bool stacktype::is_empty()
{
if( mytop == 0 )
return true;
else
return false;
}
bool stacktype::is_full()
{
if( mytop == 99 )
return true;
else
return false;
}
void stacktype::push( int x )
{
if( is_full() == false )
{
mytop++;
mydata[mytop] = x;
}
}
int stacktype::pop()
{
if( is_empty() == false )
{
int x = mydata[mytop];
mytop--;
return x;
}
}
//==============================================================================
//============================== Client Program
================================
//==============================================================================
int menu()
{
int choice;
cout<< "STACK EDITOR MENU OPTIONS" << "\n";
cout<< "1. Create new stack" << "\n";
cout<< "2. Add elements to stack" << "\n";
cout<< "3. Delete elements from stack" << "\n";
cout<< "4. Display top element in stack " << "\n";
cout<< "5. Display whole stack in the order entered" << "\n";
cout<< "6. Count numbers of elements in stack" << "\n";
cout<< "7. Exit" << "\n" << "\n";
gotoxy(0,8);
cout<< "choice? ";
cin>> choice;
return(choice);
}
int main()
{
stacktype S, T;
int choice, x, i;
choice = menu();
while( choice != 7 )
{
switch( choice )
{
case 1: S.initialize();
gotoxy(0,8);
cout<< "choice? ";
cin>> choice;
break;
case 2: gotoxy(0,10);
while(S.is_full() == false)
{
cout<< "number to insert? ";
cin>> x;
S.push(x);
}
gotoxy(0,8);
cout<< "choice? ";
cin>> choice;
break;
case 3: gotoxy(0,10);
S.pop();
gotoxy(0,8);
cout<< "choice? ";
cin>> choice;
break;
case 4: gotoxy(0,12);
display_top(S);
gotoxy(0,8);
cout<< "\n" << "choice? ";
cin>> choice;
break;
case 5: display_stack(S);
gotoxy(0,8);
cout<< "choice? ";
cin>> choice;
break;
case 6: count_values(S);
cout<< i;
gotoxy(0,8);
cout<< "choice? ";
cin>> choice;
break;
case 7: exit();
break;
choice = menu();
}
}
}
void display_top( stacktype S )
{
S.pop();
cout<< S.mytop;
}
void display_stack( stacktype S )
{
stacktype T;
while( S.mytop != 0 )
{
S.pop();
T.push(S.mytop);
}
while( T.mytop != 0 )
{
cout<< T.pop();
T.mytop--;
}
}
int count_values( stacktype S )
{
int i = 0;
if(S.mytop != 0)
{
while(S.mytop != 0)
{
S.pop();
i++;
}
return i;
}
/////// ???????
}
int exit()
{
return x;
}
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
|