改写要求1:动态生成单链表存储

#include <cstdlib>
#include <iostream> using namespace std;
struct LinkNode
{
int data;
struct LinkNode *next;
};
class NUM
{
int n;
public:
NUM(int x)
{
n=x;
}
struct LinkNode * descrease();
void show(LinkNode* pHead)
{
LinkNode* p;
p=pHead;
p=p->next;
cout<<"n= "<<n<<endl;
while(p)
{
cout<<p->data;
p=p->next;
}
cout<<endl;
}
}; struct LinkNode * NUM::descrease()
{
LinkNode* pHead=new LinkNode;
pHead->next=NULL;
LinkNode* p;
LinkNode* q;
p=pHead;
int temp;
int x=n;
while(x)
{
LinkNode* newLinkNode=new LinkNode;
newLinkNode->next=NULL;
newLinkNode->data=x%10;
x=x/10;
p->next=newLinkNode;
p=newLinkNode;
}
for(p=pHead;p!=NULL;p=p->next)
for(q=p->next;q!=NULL;q=q->next)
{
if(p->data<q->data)
{
temp=q->data;
q->data=p->data;
p->data=temp;
}
}
return pHead;
} int main(int argc, char *argv[])
{
int n;
LinkNode* pHead;
cout<<"Input n: ";
cin>>n;
NUM num(n);
pHead=num.descrease();
num.show(pHead);
system("PAUSE");
return EXIT_SUCCESS;
}

  改写要求2:以最大、最小、次最大、次最小等间隔排序

#include <cstdlib>
#include <iostream> using namespace std;
struct LinkNode
{
int data;
struct LinkNode *next;
};
class NUM
{
int n;
int sum;
public:
NUM(int x)
{
n=x;
}
struct LinkNode * descrease();
void show(LinkNode* pHead)
{
LinkNode* p;
p=pHead;
p=p->next;
cout<<"n= "<<n<<endl;
cout<<"sum= "<<sum<<endl;
while(p)
{
cout<<p->data;
p=p->next;
}
cout<<endl;
}
}; struct LinkNode * NUM::descrease()
{
LinkNode* pHead=new LinkNode;
pHead->next=NULL;
LinkNode* p;
LinkNode* q;
p=pHead;
int temp;
int x=n;
bool flag=true;
sum=;
while(x)
{
LinkNode* newLinkNode=new LinkNode;
newLinkNode->next=NULL;
newLinkNode->data=x%;
sum+=newLinkNode->data;
x=x/;
p->next=newLinkNode;
p=newLinkNode; }
for(p=pHead->next;p!=NULL;p=p->next)
{
if(flag)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(p->data<q->data)
{
temp=q->data;
q->data=p->data;
p->data=temp;
}
} }else{
for(q=p->next;q!=NULL;q=q->next)
{
if(p->data>q->data)
{
temp=q->data;
q->data=p->data;
p->data=temp;
}
} }
flag=!flag;
}
return pHead;
} int main(int argc, char *argv[])
{
int n;
LinkNode* pHead;
cout<<"Input n: ";
cin>>n;
NUM num(n);
pHead=num.descrease();
num.show(pHead);
system("PAUSE");
return EXIT_SUCCESS;
}
04-25 04:17