文档库 最新最全的文档下载
当前位置:文档库 › 10以内按队列号从小到大排序

10以内按队列号从小到大排序

/*由用户输入n个10以内的数,每输入i(0=非空队列,按队列号从小到大的顺序串成一个链,并输出该链的所有元素。*/
#include
#include
using namespace std;

class node
{
public:
int data;
node *next;
node(const int &el,node *ptr=0)
{
data=el;
next=ptr;
}
};
class queue
{
private:
int size[10];

node *front[10];

node *rear[10];
public:
queue()
{
int i;

for(i=0;i<10;i++){
size[i]=0;
front[i]=rear[i]=NULL;
}
}

bool enqueue(const int item)
{
int i=item;
if(rear[i]==NULL)
{
front[i]=rear[i]=new node(item,NULL);
}
else
{
rear[i]->next = new node(item,NULL);
rear[i]=rear[i]->next;
}
size[i]++;
return true;
}
void link()
{
int i,first=1;
node *head,*tail;
for(i=0;i<10;i++)
{
if(front[i]!=NULL&&first)
{
head=front[i];
tail=rear[i];
first=0;
}
if(front[i]!=NULL&&!first)
{
tail->next=front[i];
tail=rear[i];
}
}
cout << "输出所有元素:";
while(head!=NULL)
{
cout <data << " ";
head=head->next;
}
cout <}
};

int main()
{
queue a;
int i,b,j=1;

while(j)
{
cout <<"请输入10以内的数:";
cin>>i;
a.enqueue(i);
cout << " %%%%%%%%%%%%%%%%%%%%%%\n";
cout << " %1、继续 %\n";
cout << " %2、退出 %\n";
cout << " %%%%%%%%%%%%%%%%%%%%%%\n";
cout << "请选择:" << endl;
cin >> b;
if(b==2)
j=0;
if(b==1)
system("cls");
}
system("cls");
a.link();
return 0;
}
/*由用户输入n个10以内的数,每输入i(0=非空队列,按队列号从小到大的顺序串成一个链,并输出该链的所有元素。*/
#include
#include
using namespace std;

class node
{
public:
int data;
node *next;
node(const int &el,node *ptr=0)
{
data=el;
next=ptr;
}
};
class queue
{
private:
int size[10];

node *front[10];

node *rear[10];
public:
queue()
{
int i;

for(i=0;i<10;i++){
size[i]=0;
front[i]=rear[i]=NULL;
}
}

bool enqueue(const int item)
{
int i=item;
if(rear[i]==N

ULL)
{
front[i]=rear[i]=new node(item,NULL);
}
else
{
rear[i]->next = new node(item,NULL);
rear[i]=rear[i]->next;
}
size[i]++;
return true;
}
void link()
{
int i,first=1;
node *head,*tail;
for(i=0;i<10;i++)
{
if(front[i]!=NULL&&first)
{
head=front[i];
tail=rear[i];
first=0;
}
if(front[i]!=NULL&&!first)
{
tail->next=front[i];
tail=rear[i];
}
}
cout << "输出所有元素:";
while(head!=NULL)
{
cout <data << " ";
head=head->next;
}
cout <}
};

int main()
{
queue a;
int i,b,j=1;

while(j)
{
cout <<"请输入10以内的数:";
cin>>i;
a.enqueue(i);
cout << " %%%%%%%%%%%%%%%%%%%%%%\n";
cout << " %1、继续 %\n";
cout << " %2、退出 %\n";
cout << " %%%%%%%%%%%%%%%%%%%%%%\n";
cout << "请选择:" << endl;
cin >> b;
if(b==2)
j=0;
if(b==1)
system("cls");
}
system("cls");
a.link();
return 0;
}

相关文档
相关文档 最新文档