#include <iostream>
using namespace std;
template <class DataType>
struct node{
DataType data;
node<DataType>* next;
};
template <class DataType>
class cirLinkQueue{
public:
cirLinkQueue();
cirLinkQueue(DataType a[],int n);
void enQueue(DataType elem);
bool deQueue();
void printQueue();
private:
node<DataType>* rear;
};
template <class DataType>
cirLinkQueue<DataType>::cirLinkQueue()
{
rear = NULL;
}
template <class DataType>
cirLinkQueue<DataType>::cirLinkQueue(DataType a[],int n)
{
if(n == 1){
rear = new node<DataType>;
rear->data = a[0];
rear->next = rear;
}
else{
rear = new node<DataType>;
rear->data = a[0];
rear->next = rear;
for(int i=1;i<n;++i){
node<DataType>* s = new node<DataType>;
s->data = a[i];
s->next = rear->next;
rear->next = s;
rear = s;
}
}
}
template <class DataType>
void cirLinkQueue<DataType>::enQueue(DataType elem)
{
node<DataType> *s = NULL;
s = new node<DataType>;
s->data=elem;
s->next=rear->next;
rear->next=s;
rear=s;
}
template <class DataType>
bool cirLinkQueue<DataType>::deQueue()
{
DataType x;
node<DataType> *p=NULL;
if(rear ==rear->next) throw"下溢";
p = rear->next->next;
x=p->data;
rear->next->next=p->next;
if(p==rear)rear = rear->next;
delete p ;
return x;
}
template <class DataType>
void cirLinkQueue<DataType>::printQueue()
{
if(rear == NULL)
cout<<"Empty"<<endl;
else{
node<DataType>* p = rear->next;
while(p != rear){
cout<<p->data<<" ";
p = p->next;
}
cout<<p->data<<endl;
}
}
int main()
{
cirLinkQueue<int> Q;
int m,n;
cin>>m;
while(m--){
int elem;
cin>>elem;
Q.enQueue(elem);
}
cin>>n;
while(n--){
bool res = Q.deQueue();
if(!res) break;
}
Q.printQueue();
return 0;
}
队列入队总出现Segmentation fault,不知道这段代码该怎么改
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- 拂面清风三点水 2021-10-02 23:56关注
使用默认构造函数的时候,rear没有初始化,后面访问肯定段错误撒
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
- ¥500 52810做蓝牙接受端
- ¥15 基于PLC的三轴机械手程序
- ¥15 多址通信方式的抗噪声性能和系统容量对比
- ¥15 winform的chart曲线生成时有凸起
- ¥15 msix packaging tool打包问题
- ¥15 finalshell节点的搭建代码和那个端口代码教程
- ¥15 Centos / PETSc / PETGEM
- ¥15 centos7.9 IPv6端口telnet和端口监控问题
- ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作