第二次数据结构作业

来源:互联网 发布:马扎克编程入门 编辑:程序博客网 时间:2024/05/16 11:01

       上次作业有几处错误  这是第二次作业~使用stack 和queue

         以后就都写成类了 分.h 和.cpp这样规范些. 请高手指教

 

停车场管理。设停车场内只有一个可停放几辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在车场的最北端),若车场内已停满几辆汽车,则后来的汽车只能在门外的便道上等候,一旦停车场内有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,由于停车场是狭长的通道,在它之后开入车场的车辆必须先退出车场为它让路,待该辆车开出大门外后,为它让路的车辆再按原次序进入车场。在这里假设汽车不能从便道上开走。试设计一个停车场管理程序。

 

 

#ifndef queue_h
#define queue_h
 
//----
# define stack_init_size 1000
# define increment  
100
# define datetype  
string//date type  
//-----
typedef class queue
{
public:
    datetype  
*date;
 
int  front,rear;

    
void init()
    
{
        
this->date =new datetype[10000] ;
      
this->front=0;
      
this->rear=1;
        
    }
;
    
void Inseart(queue & que, datetype temp)
    
{
        que.date[que.rear
-1]=temp;
        que.rear
++;
    }
;

    datetype  outque(queue 
& que)
    
{
        datetype temp;
        
if (que.front==que.rear)cout<<"error , queue is empty"<<endl;
        
else 
        
{
            temp
=que.date[que.front];
        
            que.front
++;
        }

        
        
return temp;
    }

    
bool Empty(queue que)
    
{
        
if(que.front==que.rear)return true ;
        
else return false ;
    }


}
arrqueue;



#endif 



#ifndef stack_h
#define stack_h
#include
<string>
using namespace std;
 
//-------
# define stack_init_size 1000
# define increment  
100
# define datetype  
string//date type 

typedef 
class Stack
{
public:
     datetype 
*base;  //栈底
     datetype *top;   //栈顶 

   
int stacksize;

 
void Init()//== 构造~~其实写个构造就行
   {
     
//Stack temp;
    
//temp.base= (datetype *)malloc(stack_init_size*sizeof(datetype));
    this->base=new datetype [stack_init_size]; //可以替换
     this->top= this->base;
     
this->stacksize=stack_init_size;
     
return ;
   }
;

   
void Destroy()
   
{
     
//使用默认析构函数
   }
;
  
   
void Clear(Stack &sta)
   
{
       sta.
base=sta.top=NULL;
       
return  ;
   }
;

   
bool Empty(Stack sta)
   
{
     
if (sta.base==sta.top)return true ;
     
else return false ;
   }
;
   
int length(Stack sta)
   
{
      
return sta.top-sta.base;//因为是顺序栈~~ 链栈就麻烦了
   }
;

   datetype GetTop(Stack sta )
   
{
       datetype temp;
       
       
if (sta.Empty(sta))  cout<<"Error,Stack is empty"<<endl;
       
       
else  temp=*(sta.top-1); //取栈顶 -1 是因为top为最后一个元素+1; 没删除顶元素
       
       
return temp;
   }
;
   
   datetype  Pop(Stack 
&sta)
   
{
       datetype temp ;   
//删除顶元素
       
       
if (sta.Empty(sta)) cout<<"Error,Stack is empty"<<endl;
       
       
else  temp=*--sta.top ;  //下次再top插入的时候
           
      
return  temp;
           
   }
;

void Push(Stack &sta,datetype temp )
   
{
       
if (sta.top-sta.base>=sta.stacksize)
       
{
           sta.
base=(datetype *)realloc(sta.base,(sta.stacksize+increment)*sizeof(datetype)) ;
           
//动态追加内存
           sta.top=sta.base+sta.stacksize;//原来的最后一个位置
           sta.stacksize+=increment;

       }
;

       (
*sta.top ++)=temp;
return  ;   
   }
;
   


}
sqstack;


#endif stack_h

///----------主函数----------------
#include<iostream>
#include
<string>
#include 
"stack.h"
#include
"queue.h"
using namespace std;
 

int main()
{    

    
string  CarName,CarNameo;
    
    sqstack a;      a.Init();
      queue b;        b.init(); 
//缓冲队

    
int NumberCar=0;
    
int i=0;
    cout
<<"输入车辆数"; cin>>NumberCar;
    
while (i<NumberCar)
    
{
        cin
>>CarName;
        a.Push(a,CarName);
        i
++;
    }
;
    
while (1)
{
    cout
<<"输出开出车辆名,OVER 结束"<<endl; 
    
    cin
>>CarNameo; if (CarNameo ="OVER")break;

    
while ( !a.Empty(a)&&(CarNameo!=a.GetTop(a)  ) )
    
{
        b.Inseart(b,a.Pop(a));
    }
;
    
    cout
<<a.Pop(a)<<" "<<"终于出来了。。。";
    
    
while (!b.Empty(b)){ a.Push( a, b.outque(b) ) ;}

}
    
    system(
"PAUSE");
    
    
return 0;
}