TiCRACK
2020-03-13 11:20
#include"车.h"
#include<iostream>
using namespace std;
car::car( int capacity, string kind)
{
m_Capacity = capacity;
m_Kind = kind;
}
void car::printinfo()
{
cout << "载客量:" <<m_Capacity << endl;
cout << "车型:" << m_Kind << endl;
}
#ifndef CAR_H_
#define CAR_H_
#include<iostream>
using namespace std;
class car
{
public:
car(int capacity=0,string kind=" ");
void printinfo();
private:
int m_Wheel;
int m_Capacity;
string m_Kind;
};
#endif // !CAR_H_
#pragma once
#include"MyQueue.h"
#include<iostream>
#include"车.h"
using namespace std;
template <typename T>
Myqueue<T>::Myqueue(int queueCapacity)
{
m_queueCapacity = queueCapacity;
Clearqueue();
m_pQueue = new T[m_queueCapacity];
}
template <typename T>
Myqueue<T>::~Myqueue()
{
delete []m_pQueue;
m_pQueue = NULL;
}
template <typename T>
void Myqueue<T>::Clearqueue()
{
m_iHead = 0;
m_iTail = 0;
m_Queuelength = 0;
}
template <typename T>
bool Myqueue<T>::QueueEmpty() const
{
if (m_Queuelength == 0)
{
return true;
}
else
{
return false;
}
}
template <typename T>
int Myqueue<T>::Queuelength() const
{
return m_Queuelength;
}
template <typename T>
bool Myqueue<T>::QueueFull()const
{
if (m_Queuelength == m_queueCapacity)
{
return true;
}
else
{
return false;
}
}
template <typename T>
bool Myqueue<T>::EnQueue(T element)
{
if (QueueFull())
{
return false;
}
else
{
m_pQueue[m_iTail%m_queueCapacity] = element;
m_iTail++;
m_Queuelength++;
return true;
}
}
template <typename T>
bool Myqueue<T>::DeQueue(T& element)
{
if (QueueEmpty())
{
return false;
}
else
{
element = m_pQueue[m_iHead % m_queueCapacity];
m_iHead++;
m_Queuelength--;
return true;
}
}
template <typename T>
void Myqueue<T>::QueueTraverse()
{
for (int i = m_iHead;i < m_Queuelength + m_iHead;i++)
{
m_pQueue[i % m_queueCapacity].printinfo();
}
}
#ifndef MYQUEUE_H
#define MYQUEUE_H
#include"车.h"
template <typename T>
class Myqueue
{
public:
Myqueue(int queueCapacity);
virtual~Myqueue();
void Clearqueue();
bool QueueEmpty() const;
int Queuelength()const;
bool QueueFull() const;
bool EnQueue(T element);
bool DeQueue(T& element);
void QueueTraverse();
private:
T* m_pQueue;
int m_Queuelength;
int m_queueCapacity;
int m_iHead;
int m_iTail;
};
#endif // !MYQUEUE_H
#pragma once
#include"MyQueue.h"
#include<iostream>
#include"车.h"
using namespace std;
int main(void)
{
Myqueue<car>* p = new Myqueue<car>(3);
car c1(4, "奔驰");
car c2(2, "路虎");
car c3(7, "7皮马");
p->EnQueue(c1);
p->EnQueue(c2);
p->EnQueue(c3);
p->QueueTraverse();
delete p;
p = NULL;
return 0;
}
全在.h里
解决了
数据结构探险—队列篇
110035 学习 · 170 问题
相似问题