[ --> C Language<-- ] 随机数链表排序
来源:互联网 发布:java 获取月第一天 编辑:程序博客网 时间:2024/06/03 09:45
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct node{
int key;
node *next;
};
void printChain(node *head){
node *p=head->next;
while(p!=NULL){
printf("%d/n",p->key);
p=p->next;
}
}
void composeChain(node *head, node *narray[],int pos){
int mol;
int denominator;
switch(pos){
case 0:
mol=10;
denominator=1;
break;
case 1:
mol=100;
denominator=10;
break;
case 2:
mol=1000;
denominator=100;
break;
default:
mol=0;
}
//split chain
node *p=head->next;
int index;
node *q;
while(p!=NULL){
index=(p->key%mol)/denominator;
q=narray[index];
while(q->next!=NULL){
q=q->next;
}
q->next=p;
q=p;
p=p->next;
q->next=NULL;
}
//combine chain
p=head;
for(int i=0;i<10;i++){
// printf("Array[%d]: %s/n",i,&"********");
// printChain(narray[i]);
node *branch=narray[i]->next;
if(branch!=NULL){
p->next=branch;
}
node *lastN=NULL;
while(branch!=NULL){
lastN=branch;
branch=branch->next;
}
if(lastN!=NULL){
p=lastN;
}
narray[i]->next=NULL;
}
}
void main(){
int i;
node *head=(node *)malloc(sizeof(node));
//init chain
node *p=head;
time_t t;
srand((unsigned)time(&t));
for(i=0;i<10;i++){
node *n=(node *)malloc(sizeof(node));
n->key=int(rand()%1000);
n->next=NULL;
p->next=n;
p=n;
}
node *narray[10];
for(i=0;i<10;i++){
narray[i]=(node *)malloc(sizeof(node));
narray[i]->next=NULL;
}
printf("Init chain:/n");
printChain(head);
for(i=0;i<3;i++){
composeChain(head, narray,i);
// printf("%s%d:/n",&"###### ",i);
// printChain(head);
}
//print the chain
printf("/nSorted chain:/n");
printChain(head);
}
- [ --> C Language<-- ] 随机数链表排序
- [ --> C Language<-- ] 产生随机数矩阵并排序
- 链表建立 c language
- 希尔排序——《C programming language》
- eclipse C language-桶排序quiz
- 快速排序c实现(代码来自c programming language)
- c语言产生随机数并排序
- C language
- C LANGUAGE
- 随机数排序
- 【Unity&C#&随机数】随机数
- [C++]封装排序二叉树&随机数生成(c++11)
- C语言 程序 取随机数再排序去重
- 【C++】随机数
- C 链表排序
- 040921 R C language
- MAZE(C Language)
- C language standard headers
- 真实感图形学算法(2)
- The template file for selected project type is missing的解决
- Friends
- 聚族索引、非聚族索引、组合索引的含义和用途
- 上路
- [ --> C Language<-- ] 随机数链表排序
- http://groups.google.com/group/bitfall/browse_thread/thread/fddc24e6475879e1/80cb2c57657fadba?hl=en#80cb2c57657fadba
- finalize()的一个好用处
- [pb]从excel导入数据到datawindow
- delphi问题集锦
- 成功实现Windows Mobile 5.0二维码拍摄和解码程序
- 新发现
- 编写的一个.bat文件,用于清除系统中的垃圾
- java多线程