// SelectSortList.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
typedef struct List
{
int data;
List* pNext;
List(int data)
{
this->data=data;
pNext=NULL;
}
List(List* Other)
{
this->data=Other->data;
pNext=NULL;
}
List* InsertAfterMe(List* Other)
{
if(Other)
Other->pNext=this->pNext;
this->pNext=Other;
return Other;
}
}List,*PList;
PList InitAList()
{
PList pHead=new List(0);
return pHead;
}
PList& BuildList(PList& pHead)
{
int len,val;
printf("输入元素个数\n");
scanf("%d",&len);
PList pWalker=pHead;
for(int i=0;i<len;i++)
{
printf("输入元素%d的值:",i);
scanf("%d",&val);
pWalker=pWalker->InsertAfterMe(new List(val));
}
return pHead;
}
void Sort(PList pHead);
void TraverseList(PList pHead)
{
PList walker=pHead->pNext;
while(walker)
{
printf("%d ",walker->data);
walker=walker->pNext;
}
printf("\n");
}
int main(int argc, char* argv[])
{
PList pHead=InitAList();
pHead=BuildList(pHead);
TraverseList(pHead);
Sort(pHead);
TraverseList(pHead);
printf("Hello World!\n");
return 0;
}
void swap(int& a,int& b)
{
int aBak=a;
a=b;
b=aBak;
}
void Sort(PList pHead)//选择排序法
{
for(PList pi=pHead->pNext;pi!=NULL;pi=pi->pNext)
{
for(PList pj=pi->pNext;pj!=NULL;pj=pj->pNext)
{
if(pj->data<pi->data)
swap(pj->data,pi->data);
}
}
}
Sort(PList pHead)//选择排序法
最新推荐文章于 2021-06-24 22:42:43 发布