数据结构经典算法 C语言版

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
push(a*b);
break;
case '/':
a = pop();
b = pop();
printf("结果是a/b = %d\n",(a/b));
push(a/b);
break;
default:
printf("请输入正确操作符\n");
}
}
//队列的基本应用
void SetNull(int *front, int *rear)
printf("性别:%s\n",point->sex);
printf("年龄:%s\n",point->age);
printf("工作:%s\n",point->job);
}
//快速排序
void quickSort(int a[],int left,int right)
{
int i,j,temp;
void push(int i)
{
if(p > bos)
{
printf("堆栈以满\n");
return;
}
*p = i;
p++;
}
/*丛堆栈顶端取出一个数据*/
int pop(void)
{
p--;
if(p < tos)
{
printf("堆栈下溢\n");
return 0;
}
return *p;
}
void StackMain()
void BinarySearchMain()
{
char name[30];
int i;
printf("将原始数据排序\n");
quick_struct(SDatas,NUM);
printf("请输入要查找人的名字:\n");
scanf("%s",name);
i = BinarySeach(SDatas,name,NUM);
{
*front = 0;
*rear = 0;
}
int Empty(int *front,int *rear)
{
if(*front == *rear)
return(1);
else
return(0);
}
int EnQueue(int q[],int x,int *front,int *rear)
{
*rear = (*rear+1) % Max;
}
void heap(int n)
{
int i,j,t;
for(i =n/2;i>0;i--)
adjust(i,n);
printf("\n初始化成堆===> ");
for(i = 1;i < 8;i++)
printf("%5d",a[i]);
for(i = n-1;i>0;i--)
{
t = a[i+1];
push(a+b);
break;
case '-':
a = pop();
b = pop();
printf("结果是a-b = %d\n",(a-b));
push(a-b);
break;
case '*':
a = pop();
b = pop();
printf("结果是a*b = %d\n",(a*b));
if(i == -1)
{
printf("没有查找到该人信息\n");
return 0;
}
printf("查找结果:\n");
print_BinarySeachData(&SDatas[i]);
}
void quick_struct(struct Data items[],int count)
{
qs_struct(items,0,count-1);
{
int a,b;
char s[80];
p = (int *)malloc(Max*sizeof(int));
if(!p)
{
printf("分配内存失败");
exit(1);
}
tos = p;
bos = p + Max -1;
printf("请输入第一个数据:\n");
scanf("%d",&a);
}
void qs_struct(struct Data items[],int left,int right)
{
register int i,j;
char *x;
struct Data temp;
i = left;
j = right;
x = items[(left+right/2)].name;
do
}
printf("请输入待插入的数:");
scanf("%d",&m);
if (m>s[n-1]);
s[n]=m;
for (i=0; i<n; i++)
{
if (s[i]>m)
{
temp1=s[i];
s[i]=m;
for (j=i+1; j<n+1; ++j)
{
temp2=s[j];
s[j]=temp1;
else high = mid-1;
}
return -1;
}
void print_BinarySeachData(struct Data *point)
{
if(point ==NULL)
return;
printf("姓名:%s\n",point->name);
printf("城市:%s\n",point->city);
for(i=0;i<7;i++)
{
printf("请输入第%d个数:",i+1);
scanf("%d",&a[i]);
}
printf("排序前:");
for(i=0;i<6;i++)
{
printf("%4d",a[i]);
}
printf("\n");
quickSort(a,0,7);/**//*排好序的结果*/
low = 0;
high = n-1;
while(low<=high)
{
mid = (low+high)/2;
if((strcmp(items[mid].name,name)==0))
return mid;
else if((strcmp(items[mid].name,name)<0))
low = mid+1;
temp1=temp2;
}
break;
}
}
printf("排序后:");
for(i=0;i<n+1;i++)
{
printf("%4d",s[i]);
}
printf("\n");
}
//堆排序
static a[8] = {0,25,4,36,1,60,10,58,};
int count=1;
void adjust(int i,int n)
}
if(Empty(&f,&r) == 1)
printf("队列为空");
else
printf("队列中还有%d个数据",(m-n));
printf("\n");
}
//二分法查找
struct Data
{
char name[20];
char city[20];
char sex[10];
char age[10];
printf("排序后:");
for(i=0;i<6;i++)
{
printf("%4d",a[i]);
}
printf("\n");
}
//冒泡排序法
void BubbleSort()
//插入排序法
void InsertSort()
{
int s[100];
int n,m,j,i=0,temp1,temp2;
printf("请输入待排序的元素个数:");
scanf("%d",&n);
printf("请输入原序列:");
for (i=0; i<n; i++)
{
scanf("%d",&s[i]);
push(a);
printf("请输入第二个数据:\n");
scanf("%d",&b);
push(b);
printf("请输入操作符:\n");
scanf("%s",s);
switch (*s)
{
case '+':
a = pop();
b = pop();
printf("结果是a+b = %d\n",(a+b));
printf("%5d",a[i]);
heap(7);
printf("\n排序后的数据为:");
for(i = 1;i<8;i++)
printf("%5d",a[i]);
printf("\n");
}
//堆栈的基本应用
int *p;
int *tos;
int *bos;
/*添加一个数据放到堆栈对顶端*/
{
while((strcmp(items[i].name,x)<0)&&(i<right))
i++;
while((strcmp(items[j].name,x)>0)&&(j>left))
j--;
if(i<=j)
{
temp = items[i];
items[i] = items[j];
items[j] = temp;
a[i+1] = a[1];
a[1] = t;
adjust(1,i);
printf("\n第%2d步操作结果===>",count++);
for(j = 1;j<8;j++)
printf("%5d",a[j]);
}
}
void HeapSort()
{
int i;
printf("源数据为:");
for(i = 1;i<8;i++)
int i,x,m,n;
int a;
SetNull(&f,&r); /*清空队列*/
printf("请输入队列的元素个数:\n");
scanf("%d",&m);
printf("输入队列的数据:\n");
for (i=0; i<m; i++)
{
i=i;
scanf("%d",&x);
a = EnQueue(q,x,&f,&r);
{
int j,k,r,done=0;
k = r = a[i];
j = 2*i;wenku.baidu.com
while((j<=n)&&(done==0))
{
if(j<n)
{
if(a[j]<a[j+1])
j++;
}
if(k>=a[j])
done = 1;
else
{
a[j/2] = a[j];
j = 2*j;
}
}
a[j/2] = r;
if(a == -1)
break;
}
printf("要提出队列的元素个数:");
scanf("%d",&n);
printf("输出从队列中提取的数据:\n");
for (i = 0; i<n; i++)
{
if(DelQueue(q,&x,&f,&r) == -1)
break;
printf("%d\n",x);
i++;
j--;
}
}while(i<=j);
if(left<j)
qs_struct(items,left,j);
if(i<right)
qs_struct(items,i,right);
}
int BinarySeach(struct Data items[],char name[],int n)
{
int low,high,mid;
i=left;
j=right;
temp=a[left];
if(left>right)
return;
while(i!=j)/**//*找到最终位置*/
{
while(a[j]>=temp && j>i)
j--;
if(j>i)
a[i++]=a[j];
while(a[i]<=temp && j>i)
i++;
char job[10];
};
struct Data SDatas[NUM]=
{
"Sun","Weifang","Male","24","student",
"Tom","Beijing","Male","31","doctor",
"Marry","Shanghai","Female","19","techer",
if(*front == *rear)
{
printf("队列发生上溢\n");
return(-1);
}
else
{
q[*rear] = x;
return(0);
}
}
int DelQueue(int q[],int *y,int *front,int *rear)
{
*front = (*front +1)%Max;
"Willing","Tianjing","Female","21","worker"
};
void qs_struct(items,left,right);
void quick_struct(items,count);
int BinarySeach(items,name,n);
void print_BinarySeachData(point);
if(j>i)
a[j--]=a[i];
}
a[i]=temp;
quickSort(a,left,i-1);/**//*递归左边*/
quickSort(a,i+1,right);/**//*递归右边*/
}
void quickSortMain()
{
int a[7];
int i;
printf("请输入7个待排序的整数:\n");
if(*front == *rear)
{
printf("队列发生下溢\n");
return(-1);
}
else
{
*y = q[*front];
return(0);
}
}
void QueueMain()
{
int q[Max];
int f = 0, r = 0; /*f和r分别对应队列的头和尾在整个队列存储区域的位置*/
相关文档
最新文档