数据结构_单链表实现选择排序
2014-07-10 22:30
232 查看
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode
{
int data;
struct LNode *next;
}LNode;
void InitList(LNode *&L)
{
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
}
void CreateListR(LNode *L,int a[],int n)
{
int i;
LNode *r=L,*s=NULL;
for(i=0;i<n;i++)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void VisitList(LNode *L)
{
LNode *p=L->next;
while(NULL!=p)
{
printf("%d\n",p->data);
p=p->next;
}
}
void Swap(int &a, int &b)
{
int t=a;
a=b;
b=t;
}
void Sort(LNode *L)
{
LNode *p=L->next;
LNode *t=NULL;
LNode *q=NULL;
while(NULL!=p)
{
t=p;
q=p->next;
while(NULL!=q)
{
if(q->data < t->data)
{
t=q;
}
q=q->next;
}
if(t!=p)
{
Swap(t->data,p->data);
}
p=p->next;
}
}
int main(void)
{
const int N=5;
int a[]={7,5,3,9,4};
LNode *L;
InitList(L);
CreateListR(L,a,N);
VisitList(L);
Sort(L);
printf("the seqoence after being sorted is as follows:\n");
VisitList(L);
system("pause");
return 0;
}
#include<stdlib.h>
typedef struct LNode
{
int data;
struct LNode *next;
}LNode;
void InitList(LNode *&L)
{
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
}
void CreateListR(LNode *L,int a[],int n)
{
int i;
LNode *r=L,*s=NULL;
for(i=0;i<n;i++)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void VisitList(LNode *L)
{
LNode *p=L->next;
while(NULL!=p)
{
printf("%d\n",p->data);
p=p->next;
}
}
void Swap(int &a, int &b)
{
int t=a;
a=b;
b=t;
}
void Sort(LNode *L)
{
LNode *p=L->next;
LNode *t=NULL;
LNode *q=NULL;
while(NULL!=p)
{
t=p;
q=p->next;
while(NULL!=q)
{
if(q->data < t->data)
{
t=q;
}
q=q->next;
}
if(t!=p)
{
Swap(t->data,p->data);
}
p=p->next;
}
}
int main(void)
{
const int N=5;
int a[]={7,5,3,9,4};
LNode *L;
InitList(L);
CreateListR(L,a,N);
VisitList(L);
Sort(L);
printf("the seqoence after being sorted is as follows:\n");
VisitList(L);
system("pause");
return 0;
}
相关文章推荐
- 算法和数据结构-选择排序、插入排序及希尔排序 java实现
- [数据结构]用插入排序和选择排序的思想实现优先级队列
- (数据结构排序的实验四)快速,冒泡,简单选择,直接插入排序的c语言实现!!
- 给定一个无序单链表头节点head,实现单链表的选择排序,要求:额外空间复杂度为O(1)
- c++实现数据结构中的各种排序方法:直接插入、选择,归并、冒泡、快速、堆排序、shell排序
- Python实现各类数据结构和算法---直接选择排序
- 经典算法与数据结构的c++实现——直接选择排序
- (九)数据结构之简单排序算法实现:冒泡排序、插入排序和选择排序
- 【数据结构】Java实现各类经典排序算法——选择排序、冒泡排序
- 数据结构复习之–“选择排序”-JAVA实现
- 【算法和数据结构】排序(二)选择排序(C++实现)
- 数据结构 C语言实现选择排序
- 一步步学习数据结构和算法之选择排序效率分析及java实现
- 数据结构与算法——三种基础排序算法C#实现(冒泡排序、选择排序、插入排序)
- C语言实现 排序源程序(包括直接插入、希尔、冒泡、快速、简单选择、堆排序)
- Javascript实现三种排序:冒泡排序、选择排序、插入排序
- 数据结构-排序: 选择排序(直接选择排序法)
- 选择排序的递归实现
- 排序算法复习(Java实现):插入,冒泡,选择,Shell,快速排序, 归并排序,堆排序,桶式排序,基数排序
- 冒泡排序、选择排序、插入排序、快速排序算法的时间性能分析(java实现)