百度2016届网页搜索面试题 队列单链表的实现(队列大小有限制)
2015-08-14 14:51
639 查看
今天上午网页搜索部门的编程题
改进版请看:面试题 多线程安全队列的实现(改进版)
#include<iostream>
#define MAX 4
using namespace std;
struct Node
{
int data;
struct Node *next;
};
class MyQueue
{
public:
MyQueue();
bool Get(int *&p);
bool Put(int a);
int GetLength();
void Print();
~MyQueue();
private:
struct Node * rear;
struct Node * front;
int length;
};
MyQueue::MyQueue()
{
struct Node *tmp = new struct Node;
tmp->next = NULL;
rear=front=tmp;
length=0;
}
bool MyQueue::Put(int a)
{
struct Node *tmp;
if(length < MAX)
{
rear->data=a;
struct Node *tmp = new struct Node;
tmp->next=NULL;
rear->next = tmp;
rear = tmp;
length++;
return true;
}
else
return false;
}
bool MyQueue::Get(int *&p)
{
if(rear == front)
{
return false;
}
else
{
struct Node *tmp = front;
*p = tmp->data;
front = front->next;
--length;
delete tmp;
tmp = NULL;
return true;
}
}
MyQueue::~MyQueue()
{
if(rear!=front)
{
while(front !=NULL)
{
struct Node *tmp = front;
front = front->next;
delete tmp;
tmp= NULL;
}
rear = front =NULL;
}
}
int MyQueue::GetLength()
{
return length;
}
void MyQueue::Print()
{
struct Node *p = front;
int count = 0;
while(count++ < length)
{
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
}
int main()
{
MyQueue q;
q.Put(43);
q.Put(12);
q.Put(34);
q.Put(56);
q.Put(45); //超过队列大小的不放入队列
q.Print();
cout <<"队列大小:"<<q.GetLength() << endl;;
int *elem = new int;
q.Get(elem);
cout<<"删除一个元素"<<endl;
q.Print();
cout<<"队列大小:"<<q.GetLength()<<endl;
return 0;
}
改进版请看:面试题 多线程安全队列的实现(改进版)
#include<iostream>
#define MAX 4
using namespace std;
struct Node
{
int data;
struct Node *next;
};
class MyQueue
{
public:
MyQueue();
bool Get(int *&p);
bool Put(int a);
int GetLength();
void Print();
~MyQueue();
private:
struct Node * rear;
struct Node * front;
int length;
};
MyQueue::MyQueue()
{
struct Node *tmp = new struct Node;
tmp->next = NULL;
rear=front=tmp;
length=0;
}
bool MyQueue::Put(int a)
{
struct Node *tmp;
if(length < MAX)
{
rear->data=a;
struct Node *tmp = new struct Node;
tmp->next=NULL;
rear->next = tmp;
rear = tmp;
length++;
return true;
}
else
return false;
}
bool MyQueue::Get(int *&p)
{
if(rear == front)
{
return false;
}
else
{
struct Node *tmp = front;
*p = tmp->data;
front = front->next;
--length;
delete tmp;
tmp = NULL;
return true;
}
}
MyQueue::~MyQueue()
{
if(rear!=front)
{
while(front !=NULL)
{
struct Node *tmp = front;
front = front->next;
delete tmp;
tmp= NULL;
}
rear = front =NULL;
}
}
int MyQueue::GetLength()
{
return length;
}
void MyQueue::Print()
{
struct Node *p = front;
int count = 0;
while(count++ < length)
{
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
}
int main()
{
MyQueue q;
q.Put(43);
q.Put(12);
q.Put(34);
q.Put(56);
q.Put(45); //超过队列大小的不放入队列
q.Print();
cout <<"队列大小:"<<q.GetLength() << endl;;
int *elem = new int;
q.Get(elem);
cout<<"删除一个元素"<<endl;
q.Print();
cout<<"队列大小:"<<q.GetLength()<<endl;
return 0;
}
相关文章推荐
- 网友整理的Android面试题集-附网友提供的参考答案
- 女程序员获得成功的技巧
- 要做Java程序员 需要知道那些技术 重点有那些
- 黑马程序员——java复习总结——其他类和IO流
- 黑马程序员--Java集合框架(3)
- Android面试过程描述
- android面试经验谈
- 笔试面试知识点转载
- 黑马程序员--java技术blog---第十一篇:反射
- Android面试题精选,自己收藏下
- 黑马程序员--Java多线程
- 我想问面试官的话(更新中)
- 静态链接的一点小总结(二) 《程序员的自我修养》·笔记
- 多线程经典面试题
- 面试需要注意的十二个得分细节
- 设计模式面试大集锦
- 黑马程序员——java复习总结——泛型和Map
- 【黑马程序员-学习笔记】OC-协议与分类
- 黑马程序员——26,基本数据操作流,字节数组操作流,转换流,编码表
- java面试题总结