第2关:基于双向链表的双向冒泡排序法
#include<iostream>
using namespace std;
typedef struct DuLNode
{
int data;
struct DuLNode *prior,*next;
}DuLNode,*DuLinkList;
void CreateList(DuLinkList &L,int n)
{//建立双向循环链表
L=new DuLNode; //初始化链表L的头结点
L->prior=L;
L->next=L;
DuLinkList r=L; //工作指针r初始化指向头结点
while(n--)
{
DuLinkList p=new DuLNode;
cin>>p->data;
p->next=r->next;
r->next=p;
p->prior=r;
p->next->prior=p;
r=p;
}
}
void DuplexSort(DuLinkList L)
{//对存储在带头结点的双向链表L中的元素进行双向冒泡排序
/**************begin************/
DuLinkList cur = L->next,prev;
while(cur -> next != L){// 尾结点最终的next域指向L
cur = cur ->next;
}
DuLinkList tail = cur;// 得到尾部指针
prev = L->next;
while(L){
if(prev != tail){
for(cur = prev;cur != tail;cur = cur->next){
if(cur->data > cur->next->data){
// 交换
int temp = cur->data;
cur->data = cur->next->data;
cur->next->data = temp;
}
}
tail = tail->prior;// 每次将最大值移到最后,尾指针向前移一格(表示最大值已确定,确定次大值...)
}else{
break;
}
}
/**************end************/
}
void PrintList(DuLinkList L)
{//依次输出链表中的数据
DuLinkList p=L->next;
while(p->next&&p->next!=L)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<p->data<<endl;
}
int main()
{
int n;
while(cin>>n)
{
if(n==0) break;
DuLinkList L;
CreateList(L,n);
DuplexSort(L); //双向冒泡排序
PrintList(L);
}
return 0;
}
第3关:砾石的交换排序
#include<iostream>
using namespace std;
void Exchange(char &x,char &y)
{//交换操作
char t;
t=x;
x=y;
y=t;
}
void QkSort(char r[],int n)
{//砾石的交换排序。(红色在前,白色居中,蓝色最后)
/**************begin************/
int red = 0,white = 0,blue = n -1;
while(white <= blue){
if(r[white] == 'R'){
Exchange(r[white],r[red]);
red ++;
white ++;
}else if(r[white] == 'W'){
white ++;
}else if(r[white] == 'B'){
Exchange(r[white],r[blue]);
blue --;
}
}
/**************end************/
}
void PrintC(char c[],int n)
{//输出数据
for(int i=0;i<n-1;i++)
cout<<c[i]<<" ";
cout<<c[n-1]<<endl;
}
int main()
{
int n; //砾石的数量n
while(cin>>n)
{
if(n==0) break;
char c[n];
for(int i=0;i<n;i++)
cin>>c[i]; //输入砾石的编号
QkSort(c,n); //砾石的交换排序
PrintC(c,n);
}
return 0;
}
第4关:数组的正负排序
#include<iostream>
using namespace std;
void Exchange(int &x,int &y)
{//交换操作
int z;
z=x;
x=y;
y=z;
}
void Process(int a[],int n)
{//数组的正负排序,使所有关键字为负值的记录排在关键字为非负值的记录之前
/**************begin************/
int j = 0;
for(int i = 0; i < n; i++){
if(a[i] < 0){
Exchange(a[i],a[j]);
j++;
}
}
/**************end************/
}
void PrintA(int a[],int n)
{//输出数据
for(int i=0;i<n-1;i++)
cout<<a[i]<<" ";
cout<<a[n-1]<<endl;
}
int main()
{
int n;
while(cin>>n)
{
if(n==0) break;
int i,a[n];
for(i=0;i<n;i++) //输入数据
cin>>a[i];
Process(a,n); //数组的正负排序
PrintA(a,n); //输出数据
}
return 0;
}
第5关:计数排序
#include<iostream>
using namespace std;
void CountSort(int a[],int b[],int n)
{//计数排序,将包括n个数据的数组a中的记录排序存入到数组b中
/**************begin************/
int c = 0;
for(int i = 0;i < n;i++){
int temp = a[i];
for(int j = 0;j < n;j++){
if(a[j] < temp){
c++;
}
}
b[c] = temp;
c = 0;// 重置
}
/**************end************/
}
int main()
{
int n;
while(cin>>n)
{
if(n==0) break;
int i,a[100],b[100];
for(i=0;i<n;i++)
cin>>a[i];
CountSort(a,b,n); //计数排序
for(i=0;i<n-1;i++)
cout<<b[i]<<" ";
cout<<b[n-1]<<endl;
}
}
为什么可以这样写就不说明了,如果有任何不理解都可以评论提问