1. 栈的类实现

#include<iostream>
#include<stdlib.h>
const int MaxStackSize  = 50;
class Stack
{
private:
 DataType stacklist[MaxStackSize];
 int top;
public:
 Stack(void);
 void Push(const DataType &item);
 DataType Pop(void);
 void ClearStack(void);
 DataType Peek(void) const;
 int StackEmpoty(void) const;
 int StackFull(void) const;
};

typedef int DataType;
#include "stack.h"
using namespace std;
Stack::Stack(void):top(-1)
{
}

void Stack::Push(const DataType& item)
{
  if(top == MaxStackSize -1 )
  {
   cerr<<"Stack OverFlow!"<<endl;
   exit(1);
  }

  top++;
  stacklist[top] = item;
}


DataType Stack::Pop(void)
{
DataType temp;
if(top == -1)
{
 cerr<<"Attempt to pop an empty stack!"<<endl;
 exit(1);
}

  temp  = stacklist[top];
  top--;
  return temp;
}


DataType   Stack::Peek(void) const
{
 if(top ==  -1)
 {
  cerr<<"Attempt to peek an empty stack!"<<endl;
  exit(1);
 }

 return stacklist[top];
}


int Stack::StackEmpoty(void) const
{
 return top==-1;
}
int Stack::StackFull(void) const
{
 return top == MaxStackSize -1;

}


void Stack::ClearStack()
{
 top = -1;
}

void main()
{
 Stack s;
 s.Push(10);
 cout<< s.Peek()<<endl;

 if(!s.StackEmpoty())
 {
  cout<<s.Pop()<<endl;
 }

 s.ClearStack();
}

 

2. 回文

 

#include<iostream>
#include<stdlib.h>
const int MaxStackSize  = 50;
class Stack
{
private:
 DataType stacklist[MaxStackSize];
 int top;
public:
 Stack(void);
 void Push(const DataType &item);
 DataType Pop(void);
 void ClearStack(void);
 DataType Peek(void) const;
 int StackEmpoty(void) const;
 int StackFull(void) const;
};

 


typedef char  DataType;
#include "stack.h"
using namespace std;
Stack::Stack(void):top(-1)
{
}

void Stack::Push(const DataType& item)
{
  if(top == MaxStackSize -1 )
  {
   cerr<<"Stack OverFlow!"<<endl;
   exit(1);
  }

  top++;
  stacklist[top] = item;
}


DataType Stack::Pop(void)
{
DataType temp;
if(top == -1)
{
 cerr<<"Attempt to pop an empty stack!"<<endl;
 exit(1);
}

  temp  = stacklist[top];
  top--;
  return temp;
}


DataType   Stack::Peek(void) const
{
 if(top ==  -1)
 {
  cerr<<"Attempt to peek an empty stack!"<<endl;
  exit(1);
 }

 return stacklist[top];
}


int Stack::StackEmpoty(void) const
{
 return top==-1;
}
int Stack::StackFull(void) const
{
 return top == MaxStackSize -1;

}


void Stack::ClearStack()
{
 top = -1;
}


//产生去掉所有空格的新串
void Deblank(char *s, char *t)
{
  // 直到串结束符号NULL
 while(*s != NULL)
 {
  //若为非空,  拷贝到新串
  if(*s !=' ')
   *t++ =  *s;
  s++;      //取下一字符
 }

 *t  = NULL;  //新串后接上NULL
}


void main()
{
const  int True  = 1, False  = 0;
 Stack  s;
 char palstring[80], deblankstring[80],c;

 int i = 0;
 int ispalindrome  = True;

 cin.getline(palstring,80,'n');

 Deblank(palstring,deblankstring);

 i = 0;

 while(deblankstring[i] !=0)
 {
  s.Push(deblankstring[i]);
  i++;

 }

 i = 0 ;
 while(!s.StackEmpoty())
 {
  c =  s.Pop();
  if(c != deblankstring[i])
  {
   ispalindrome  = False;
   break;
  }
  i++;
 }

 if(ispalindrome)
  cout<<'\"'<<palstring<<'\"'<<"is a palindrome" <<endl;
 else
   cout<<'\"'<<palstring<<'\"'<<" is  not a palindrome "<<endl;
}

 

posted on 2009-03-30 19:15  alon  阅读(269)  评论(1编辑  收藏  举报

导航