随机产生个整数在计算机内建立有序链表并输出该链表#include ioh
#include stdlibh
#include stdioh
#include mathh
typedef struct nodetest
{
int data;
struct nodetest * next;
}node;
void sort(node * * head)
{
node *p*q*r*s*h;
h=p=(node *)malloc(sizeof(node));
p>next=*head;
while(p>next!=NULL)
{
q=p>next;
r=p;
while(q>next!=NULL)
{
if(q>next>datanext>data)r=q;
q=q>next;
}
if(r!=p)
{
s=r>next;
r>next=s>next;
s>next=p>next;
p>next=s;
}
p=p>next;
}
*head=h>next;
free(h);
}
void main()
{
node * h;
node *p;
node *s;
int xcycle;
h=(node *)malloc(sizeof(node));
p=h;
srand();
for(cycle=;cycle<;cycle++)
{
x=rand();
s=(node *)malloc(sizeof(node));
s>data=x;
p>next=s;
p=s;
}
h=h>next;
p>next=NULL;
s=h;
for(cycle=;cycle<;cycle++)
{
printf(%d\ns>data);
s=s>next;
}
sort(&h);
s=h;
for(cycle=;cycle<;cycle++)
{
printf(%d\ns>data);
s=s>next;
}
}
随机产生个整数利用冒泡法排序
#include stdlibh
#include stdioh
#include mathh
int n[];
void sort()
{
int ijw;
for(i=;i<;i++)
for(j=;j>=i+;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';
<