今天去一个软件公司面试,出的题,写不出,望达人解答一下

laohuchiren 2005-11-03 09:57:08
编写一个模板队列类,并实现以下方法:
(1) void add(Type elem) 向队列尾中添加元素;
(2) Type remove() 从队列头中删除元素;
(3) bool is_empty() 判断队列是否为空;
另外,定义一个重载的析取运算符operator <<,使得可以直接输出队列对象的值。

具体要求:

(1)用链表实现链表元素的存放;
(2)定义一个模板类QueueItem,存放模板类Queue的一个节点,并将Queue定义为QueueItem的友元类;
(3)将Queue模板类的定义放到头文件 queue.h中;
(4)以下是测试你所设计的类的程序:
#include <iostream.h>
#include "queue.h"
void main()
{
Queue<int> iq;
for(int i=0; i < 10; i++)
iq.add(i*5);
if (!iq.is_empty())
cout << iq.remove();
cout << endl;
if (!iq.is_empty())
cout << iq;
}
要求程序的输出为:
0
<5 10 15 20 25 30 35 40 4
...全文
655 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
laohuchiren 2005-11-04
  • 打赏
  • 举报
回复
非常感谢各位,分不多,一点心意
csucdl 2005-11-04
  • 打赏
  • 举报
回复

#include <iostream.h>

template<class Elem>
class Queue;

template<class Elem>
ostream& operator<<(ostream&, const Queue<Elem>&);


template<class Elem>
class QueueItem
{
friend class Queue<Elem>;
friend ostream& operator<<(ostream&, const Queue<Elem>&);
Elem value;
QueueItem<Elem> *next;
public:
QueueItem(const Elem &value, QueueItem<Elem> *const next = NULL):value(value), next(next){}
~QueueItem(){}
};



template<class Elem>
class Queue
{
friend ostream& operator<<(ostream&, const Queue<Elem>&);
QueueItem<Elem> *front;
QueueItem<Elem> *rear;
void clear(){
QueueItem<Elem> *temp = front;
while(temp != rear)
{
front = temp->next;
delete temp;
temp = front;
}
if(temp != NULL)
{
delete temp;
}
front = rear = NULL;
}
public:
Queue(){front = rear = NULL;}
~Queue(){clear();}
bool IsEmpty()
{
return (front == NULL);
}
Elem Remove()
{
if(front == NULL)
{
cout<<"error, the queue is empty!"<<endl;
return 0;
}
Elem temp = front->value;
QueueItem<Elem> *ptemp = front;
front = front->next;
delete ptemp;
ptemp = NULL;
return temp;
}
void Add(const Elem &item)
{
if(front == NULL)
{
front = rear = new QueueItem<Elem>(item);
}
else
{
rear = rear->next = new QueueItem<Elem>(item);
}
}

};

template<class Elem>
ostream& operator<<(ostream& out, const Queue<Elem> &queue)
{
QueueItem<Elem> *temp = queue.front;
out<<"<";
while(temp != queue.rear)
{
out<<temp->value<<" ";
temp = temp->next;
}
out<<temp->value<<">"<<endl;
return out;
}
int main()
{
Queue<int> iq;
for(int i=0; i < 10; i++)
iq.Add(i*5);
if (!iq.IsEmpty())
cout<<iq.Remove();
cout<<endl;
if (!iq.IsEmpty())
cout<<iq;
return 0;
}
laohuchiren 2005-11-04
  • 打赏
  • 举报
回复
谢谢了
bluejugar 2005-11-04
  • 打赏
  • 举报
回复
随便写了一下,双向链表,很久没写代码了,希烂的,希望别介意.
没有做内存泄露检查,也没有优化:

#include <iostream>
#include <string>
#include <stdexcept>
#include <cstdlib>

template<typename T>
struct Point
{
T m_Value_;
Point<T>* m_before_;
Point<T>* m_after_;
Point()
: m_Value_(T() ), m_before_(NULL), m_after_(NULL)
{
}
Point(T a_Rhs_)
: m_Value_(a_Rhs_), m_before_(NULL), m_after_(NULL)
{
}
Point(const Point<T>& a_Rhs_)
: m_Value(a_Rhs_.m_Value_),
m_before(a_Rhs_.m_before_),
m_after_(a_Rhs_.m_after_)
{
}
friend std::ostream& operator<<(std::ostream& os, const Point& point)
{
return os<<m_Value_;
}
};



template<typename Type>
class Queue
{
public:
class ContainIsNULL : public std::exception
{
public:
const char* what() const
{
return "Contain is NULL!";
}
};
public:
void add(Type elem)
{
if(0 == m_size_)
{
m_begin_->m_Value_ = elem;
}
else if(1 == m_size_)
{
m_end_->m_Value_ = elem;
}
else
{
Point<Type>* l_pnewPoint_ = new Point<Type>(elem);
l_pnewPoint_->m_before_ = m_end_;
m_end_->m_after_ = l_pnewPoint_;
m_end_ = l_pnewPoint_;

}
++m_size_;
}
Type remove()
{
if(is_empty() )
{
throw ContainIsNULL();
}
else
{
Type l_tmpValue_ = m_begin_->m_Value_;
Point<Type>* l_ptmpPoint_ = m_begin_->m_after_;
delete m_begin_;
m_begin_ = l_ptmpPoint_;
m_begin_->m_before_ = NULL;

--m_size_;

return l_tmpValue_;
}
}
bool is_empty()
{
return (0 == m_size_);
}

public:
Queue()
: m_size_(0)
{
m_begin_ = new Point<Type>();
m_end_ = new Point<Type>();
m_begin_->m_after_ = m_end_;
m_end_->m_before_ = m_begin_;
}
virtual ~Queue()
{
if(NULL != m_begin_)
{
delete m_begin_;
m_begin_ = NULL;
}
if(NULL != m_end_)
{
delete m_end_;
m_end_ = NULL;
}
}
Point<Type>* begin() const
{
return m_begin_;
}
unsigned size()
{
return m_size_;
}
friend std::ostream& operator<<(std::ostream& os, const Queue& queue)
{
Point<Type>* l_iter_ = queue.begin();
while(NULL != l_iter_)
{
os<<l_iter_->m_Value_;
l_iter_ = l_iter_->m_after_;
}
return os;
}
private:
unsigned m_size_;
Point<Type>* m_begin_;
Point<Type>* m_end_;
};

int main()
{
using namespace std;

{
Queue<int> iq;
for(int i=0; i < 10; i++)
iq.add(i*5);
if (!iq.is_empty())
cout << iq.remove();
cout << endl;
if (!iq.is_empty())
cout << iq;
}

system("pause");
return 0;
}

K 2005-11-04
  • 打赏
  • 举报
回复
#include <iostream>
#include <windows.h>
using namespace std;

template <typename Type>
class queue
{
public:
queue():head(NULL),ass(NULL){};
~queue(){/*自己写吧*/};

void add(Type elem)
{
s *t=new s;
t->data=elem;
t->next=NULL;

if (NULL == head)
{
head=t;
ass=head;
}
else
{
ass->next=t;
ass=t;
}
};

Type remove()
{
s *t;

assert(!is_empty());

t=head;
Type ret=t->data;
if (head->next!=NULL)
{
head=head->next;
}
else
{
head=NULL;
}
delete t;
return ret;
};

bool is_empty(){return head==NULL;};

friend ostream &operator << (ostream & l,queue & r)
{
s *t;
t=r.head;

if (r.is_empty())
{
l<<"empty!";
return l;
}
l<<"<";
while ( t!=NULL )
{
l<<t->data<<" ";
t=t->next;
}
l<<endl;
return l;
}

private:

struct s
{
Type data;
s *next;
};

s *head;
s *ass;

};

int main()
{
queue<int> iq;
for(int i=0; i < 10; i++)
iq.add(i*5);
if (!iq.is_empty())
cout << iq.remove();
cout << endl;
if (!iq.is_empty())
cout << iq;
cin.get();
}

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧