#include
#include
#include
typedef struct nodetest
{
int data;
struct nodetest * next;
}node;
void sort(node * * head)
{
node *p
h=p=(node *)malloc(sizeof(node));
p
while(p
{
q=p
r=p;
while(q
{
if(q
q=q
}
if(r!=p)
{
s=r
r
s
p
}
p=p
}
*head=h
free(h);
}
void main()
{
node * h;
node *p;
node *s;
int x
h=(node *)malloc(sizeof(node));
p=h;
srand(
for(cycle=
{
x=rand();
s=(node *)malloc(sizeof(node));
s
p
p=s;
}
h=h
p
s=h;
for(cycle=
{
printf(
s=s
}
sort(&h);
s=h;
for(cycle=
{
printf(
s=s
}
}
#include
#include
#include
int n[
void sort()
{
int i
for(i=
for(j=
if(n[j] { w=n[j]; n[j]=n[j-1]; n[j-1]=w; } } void main() { int x; srand(0.001); for(x=0;x<10;x++) n[x]=rand(); for(x=0;x<10;x++) printf("%d\n",n[x]); sort(); for(x=0;x<10;x++) printf("%d\n",n[x]); } #include "stdlib.h" #include "stdio.h" #include "math.h" typedef struct tnode{ char data; struct tnode * left,* right; }btree; void inord (btree *p){ if (p!=NULL){ printf("%c\n",p->data); inord(p->right); inord(p->left); } }; void main(){ btree *p,*h,*f; h=(btree *)malloc(sizeof(btree)); p=h; p->data='A'; p->left=NULL; p->right=NULL; f=(btree*)malloc(sizeof(btree)); f->data='B'; f->left=NULL; f->right=NULL; p->left=f; f=(btree*)malloc(sizeof(btree)); f->data='C'; f->left=NULL; f->right=NULL; p->right=f; p=p->left; f=(btree*)malloc(sizeof(btree)); f->data='D'; f->left=NULL; f->right=NULL; p->left=f; f=(btree*)malloc(sizeof(btree)); f->data='E'; f->left=NULL; f->right=NULL; p->right=f; p=h; inord(p); } 3.隨機產生10個整數,利用直接插入排序方法對序列進行升序排列,並輸出結果. #include "stdlib.h" #include "stdio.h" #include "math.h" int n[11]; void sort() { int i,j; for(i=2;i<=10;i++) if(n[i] { n[0]=n[i]; j=i-1; do { n[j+1]=n[j]; j--; }while(n[0] n[j+1]=n[0]; } } void main() { int x; srand(0.001); for(x=1;x<=10;x++) n[x]=rand(); n[x]=10-x; for(x=1;x<=10;x++) printf("%d\n",n[x]); sort(); printf("\n"); for(x=1;x<=10;x++) printf("%d\n",n[x]); } 4 隨機產生10個整數,利用直接選擇排序方法對序列進行升序排列,並輸出結果. #include "stdlib.h" #include "stdio.h" #include "math.h" int n[10]; void sort() { int i,j,k,temp; for(i=0;i<10;i++) { k=i; for(j=i+1;j<10;j++) if(n[j] temp=n[i]; n[i]=n[k]; n[k]=temp; } } void main() { int x; //srand(0.001); for(x=0;x<10;x++) // n[x]=rand(); n[x]=10-x; for(x=0;x<10;x++) printf("%d\n",n[x]); sort(); for(x=0;x<10;x++) printf("%d\n",n[x]); } 5 已知二叉樹如圖1 ,采用二叉鏈存儲,在計算機中建立起該二叉樹,並完成中序遍歷,輸出相應序列. #include "stdlib.h" #include "stdio.h" #include "math.h" typedef struct tnode { char data; struct tnode * left,* right; }btree; void inord(btree * p) { if(p!=NULL) { inord(p->left); printf("%c",p->data); inord(p->right); } } void main() { int i; btree * p,* h,* f,* f1; h=(btree *)malloc(sizeof(btree)); p=h; p->data='a'; p->left=NULL; p->right=NULL; f=(btree *)malloc(sizeof(btree)); f->data='b'; f->left=NULL; f->right=NULL; p->left=f; f1->data='c'; f1->left=NULL; f1->right=NULL; p->right=f1; p=p->left; f=(btree *)malloc(sizeof(btree)); f->data='d'; f->left=NULL; f->right=NULL; p->left=f; f=(btree *)malloc(sizeof(btree)); f->data='e'; f->left=NULL; f->right=NULL; p->right=f; p=h; inord(p); } 6 已知二叉樹如圖1,采用二叉鏈存儲,在計算機中建立起該二叉樹,並完成前序遍歷,輸出相應序列. #include "stdlib.h" #include "stdio.h" #include "math.h" typedef struct tnode { char data; struct tnode * left,* right; }btree; void inord(btree * p) { if(p!=NULL) { printf("%c",p->data); inord(p->left); inord(p->right); } } void main() { int i; btree * p,* h,* f,* f1; h=(btree *)malloc(sizeof(btree)); p=h; p->data='a'; p->left=NULL; p->right=NULL; f=(btree *)malloc(sizeof(btree)); f->data='b'; f->left=NULL; f->right=NULL; p->left=f; f1->data='c'; <
From:http://tw.wingwit.com/Article/program/sjjg/201311/23537.html