
C++程序设计题,一堆猴子都有编号。编号为1,2,3……m。这群猴子按照1
C++程序设计题,一堆猴子都有编号。编号为1,2,3……m。这群猴子按照1到m的顺序围城一圈,从第一个开始数,每数到第N个,该猴子离开,依次下来圈中最后一只猴子为大王...
C++程序设计题,一堆猴子都有编号。编号为1,2,3……m。这群猴子按照1到m的顺序围城一圈,从第一个开始数,每数到第N个,该猴子离开,依次下来圈中最后一只猴子为大王
展开
1个回答
展开全部
#include <iostream>
#include <string>
using namespace std;
class User
{
private:
string name;
string code;
public:
void dispName() { cout << name ;}
void dispCode() { cout << code ;}
void setName() { cin >> name; }
void setCode() { cin >> code; }
string getName() { return name; }
string getCode() { return code; }
};
struct TelRecord
{
User user;
TelRecord* pre;
TelRecord* next;
};
void display(TelRecord* head)
{
TelRecord* p = head;
if (!p)
return;
while (p->next!=head)
{
p->user.dispName();
cout << " ";
p->user.dispCode();
cout << endl;
p = p->next;
}
p->user.dispName();
p->user.dispCode();
}
TelRecord* createTelBook()
{
TelRecord* head = new TelRecord;
TelRecord* p = head;
TelRecord* q = NULL;
p->user.setName();
p->user.setCode();
while (p->user.getName().compare("#")!=0) //键入#结束输入
{
q = p;
p = new TelRecord;
p->pre = q;
q->next = p;
p->user.setName();
p->user.setCode();
}
delete p;
q->next = head;
head->pre = q;
return head;
}
TelRecord* bubbleName(TelRecord* head) //按照名字排序,修改判断部分为电话就是按 //电话排序的
{
if (!head)
return NULL; //空本退出
TelRecord *p, *q;
TelRecord* tail = head->pre; //head指向每趟冒泡的第一个,tail指向每趟最后一个
while (head!=tail)
{
p = head;
while (p!=tail)
{
q = p->next;
if (p->user.getName().compare(q->user.getName())>0)//如果前面的比后面的大,交换
{
if (p==head) //如果head被调换了,要重新指向。
head = q;
if (q==tail) //如果tail被调换了,要重新指向
tail = p;
p->pre->next = q;
q->next->pre = p;
p->next = q->next;
q->pre = p->pre;
p->pre = q;
q->next = p;
}
else
p = p->next;
}
tail = tail->pre; //tail每冒泡一趟要向前移动一次
}
return head;
}
void main()
{
TelRecord* telebook = createTelBook();
display(telebook);
telebook = bubbleName(telebook);
display(telebook);
}
#include <string>
using namespace std;
class User
{
private:
string name;
string code;
public:
void dispName() { cout << name ;}
void dispCode() { cout << code ;}
void setName() { cin >> name; }
void setCode() { cin >> code; }
string getName() { return name; }
string getCode() { return code; }
};
struct TelRecord
{
User user;
TelRecord* pre;
TelRecord* next;
};
void display(TelRecord* head)
{
TelRecord* p = head;
if (!p)
return;
while (p->next!=head)
{
p->user.dispName();
cout << " ";
p->user.dispCode();
cout << endl;
p = p->next;
}
p->user.dispName();
p->user.dispCode();
}
TelRecord* createTelBook()
{
TelRecord* head = new TelRecord;
TelRecord* p = head;
TelRecord* q = NULL;
p->user.setName();
p->user.setCode();
while (p->user.getName().compare("#")!=0) //键入#结束输入
{
q = p;
p = new TelRecord;
p->pre = q;
q->next = p;
p->user.setName();
p->user.setCode();
}
delete p;
q->next = head;
head->pre = q;
return head;
}
TelRecord* bubbleName(TelRecord* head) //按照名字排序,修改判断部分为电话就是按 //电话排序的
{
if (!head)
return NULL; //空本退出
TelRecord *p, *q;
TelRecord* tail = head->pre; //head指向每趟冒泡的第一个,tail指向每趟最后一个
while (head!=tail)
{
p = head;
while (p!=tail)
{
q = p->next;
if (p->user.getName().compare(q->user.getName())>0)//如果前面的比后面的大,交换
{
if (p==head) //如果head被调换了,要重新指向。
head = q;
if (q==tail) //如果tail被调换了,要重新指向
tail = p;
p->pre->next = q;
q->next->pre = p;
p->next = q->next;
q->pre = p->pre;
p->pre = q;
q->next = p;
}
else
p = p->next;
}
tail = tail->pre; //tail每冒泡一趟要向前移动一次
}
return head;
}
void main()
{
TelRecord* telebook = createTelBook();
display(telebook);
telebook = bubbleName(telebook);
display(telebook);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询