主题:停车场问题
我们作业题,可能对有些同学会帮上一些忙由于时间有限没写注释,看不懂可以邮箱问我hahazhoul@yahoo.com.cn
#include<iostream.h>
enum error_code{success,underflow,overflow};
const int maxstack=2;
struct car{
float intime,outtime;
int num;
};
typedef car stack_entry;
typedef car queue_entry;
typedef car node_entry;
struct node{
node_entry entry;
node *next;
node();
node(node_entry item,node *add_on=NULL);
};
node::node()
{
next=NULL;
}
node::node(node_entry item,node *add_on)
{
entry=item;
next=add_on;
}
class queue{
public:
queue();
bool empty() const ;
error_code append(const queue_entry &item);
error_code serve();
error_code retrieve(queue_entry &item)const;
error_code gett(int m,queue_entry &item);
// ~queue();
// queue(const queue& original);
// void operator =(const queue &original);
protected:
node *front,*rear;
};
queue::queue()
{
front=rear=NULL;
}
error_code queue::append (const queue_entry &item)
{
node *new_rear =new node(item);
if(new_rear==NULL)return overflow;
if(rear==NULL)front=rear=new_rear;
else{
rear->next=new_rear;
rear=new_rear;
}
return success;
}
error_code queue::retrieve(queue_entry &item)const
{
if(front==NULL)
return underflow;
item=front->entry;
return success;
}
error_code queue::serve ()
{
if(front==NULL)return underflow;
node *old_front=front;
front=old_front->next;
if(front==NULL)rear=NULL;
delete old_front;
return success;
}
error_code queue::gett(int m,queue_entry &item)
{
node *old_front=front;
for(int x=0;x<m;x++)
{
old_front=front;
if(front->entry.num ==item.num){break;}
front=old_front->next;
}
if(x==m)front=old_front;
if(m==0)
return overflow;
if(front->entry.num!=item.num)
return overflow;
else
return success;
}
#include<iostream.h>
enum error_code{success,underflow,overflow};
const int maxstack=2;
struct car{
float intime,outtime;
int num;
};
typedef car stack_entry;
typedef car queue_entry;
typedef car node_entry;
struct node{
node_entry entry;
node *next;
node();
node(node_entry item,node *add_on=NULL);
};
node::node()
{
next=NULL;
}
node::node(node_entry item,node *add_on)
{
entry=item;
next=add_on;
}
class queue{
public:
queue();
bool empty() const ;
error_code append(const queue_entry &item);
error_code serve();
error_code retrieve(queue_entry &item)const;
error_code gett(int m,queue_entry &item);
// ~queue();
// queue(const queue& original);
// void operator =(const queue &original);
protected:
node *front,*rear;
};
queue::queue()
{
front=rear=NULL;
}
error_code queue::append (const queue_entry &item)
{
node *new_rear =new node(item);
if(new_rear==NULL)return overflow;
if(rear==NULL)front=rear=new_rear;
else{
rear->next=new_rear;
rear=new_rear;
}
return success;
}
error_code queue::retrieve(queue_entry &item)const
{
if(front==NULL)
return underflow;
item=front->entry;
return success;
}
error_code queue::serve ()
{
if(front==NULL)return underflow;
node *old_front=front;
front=old_front->next;
if(front==NULL)rear=NULL;
delete old_front;
return success;
}
error_code queue::gett(int m,queue_entry &item)
{
node *old_front=front;
for(int x=0;x<m;x++)
{
old_front=front;
if(front->entry.num ==item.num){break;}
front=old_front->next;
}
if(x==m)front=old_front;
if(m==0)
return overflow;
if(front->entry.num!=item.num)
return overflow;
else
return success;
}