📄 queue.cpp
字号:
#include "stdlib.h"
#include <iostream>
using namespace std;
typedef struct node //定义结点结构
{
char data;
struct node *next;
}node;
class queue
{
private:
node *head;
node *tail;
public:
queue();
~queue();
void inqueue(); //进队
char outqueue(); //出队
int length(); //计算队长
void print(); //打印队中元素
void clean(); //清空队列
int find(char ); //查所找元素在队中的位置
};
queue::queue()
{
head=NULL;
tail=NULL;
}
queue::~queue()
{
clean();
}
void queue::inqueue(void)
{
char c;
node *temp=NULL;
temp=new node;
if(temp==NULL)
{
cout<<"内存分配失败!"<<endl;
}
cin>>c;
temp->next=NULL;
temp->data=c;
if(tail==NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=tail->next;
}
}
char queue::outqueue()
{
node *temp;
char c;
if(head==NULL)
{
return NULL;
}
else
{
c=head->data;
temp=head;
head=head->next;
delete temp;
return c;
}
}
int queue::length()
{
int ch=0;
node *temp;
temp=head;
while(temp!=NULL)
{
ch++;
temp=temp->next;
}
return ch;
}
void queue::print()
{
node *temp;
temp=head;
if(temp==NULL)
{
cout<<"……空队列!……";
}
while(temp!=NULL)
{
cout<<temp->data<<'\t';
temp=temp->next;
}
cout<<endl;
}
int queue:: find(char c)
{
node *temp=head;
int i=1;
if(temp==NULL)
{
cout<<"........空队列!........."<<endl;
}
while(temp)
{
if(temp->data==c)
{
return i;
}
else{
temp=temp->next;
}
i++;
}
return 0;
}
void queue::clean()
{
node *temp;
while(head!=NULL)
{
temp=head;
head=head->next;
delete temp;
}
tail=NULL;
}
int main()
{
queue Q;
char c;
while(1)
{
cout<<"0.退出\n1.进队\n2.出队\n3.打印\n4.查找\n5.清空\n6.计算队列长度"<<'\n';
cin>>c;
switch(c)
{
case '1':
{
cout<<"输入数据:";
Q.inqueue();
cout<<"现队列为:"<<endl;
Q.print();
break;
}
case '2':
{
Q.outqueue();
break;
}
case '3':
{ cout<<"现队列为:"<<endl;
Q.print();
break;
}
case '4':
{ int i;
cout<<"输入你要找的字符:";
cin>>c;
if(i=Q.find(c))
{
cout<<"你所要找的字符位置在:"<<Q.find(c)<<endl;
}
else
{
cout<<"找不到你所要找的字符!"<<endl;
}
break;
}
case '5':
{
Q.clean();
break;
}
case '6':
{
cout<<"队列长度为:"<<Q.length()<<endl;
break;
}
case '0':
{
return 0;
}
default:
{
cout<<"错误!请选择选项!"<<endl;break;
}
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -