数据结构作业(C语言版的)牛人知道一下哈 不胜感激

回答好加分
第一 二叉树及其应用
作业目的:熟悉二叉树的特征,掌握二叉树的基本算法实现。
作业要求:
(1)对未实现的程序部分给出设计的基本思想、原理和算法描述,并对代码给出注释。
(2)对给出的作业程序认真阅读,再将其补充完整后运行。
(3)保存程序的运行结果,并结合程序进行分析。
注意事项:
源程序代码在运行前注意存盘;运行时注意输入数据的格式要求。
作业内容:
要求采用二叉链表作为存储结构,完成二叉树的建立算法、二叉树的非递归遍历的操作;
1、二叉树的递归创建算法:
void createbintree(bintree *t)
{
char ch;
if ((ch=getchar())==' ')*t=NULL;
else
{
*t=(bintnode *)malloc(sizeof(bintnode));
(*t)->data=ch;
createbintree(&(*t)->lchild);
createbintree(&(*t)->rchild);
}
}
2、二叉树前序遍历的非递归函数
void preorder1(bintree t)
{
seqstack s;
s.top=-1;
while ((t) || (s.top!=-1))
{
while (t)
{
printf("%c ",t->data);
s.top++;
s.data[s.top]=t;
t=t->lchild;
}
if (s.top>-1)
{
t=pop(&s);
t=t->rchild;
}
}
}
3、二叉树中序遍历的非递归函数
void inorder1(bintree t)
{
seqstack s;
s.top=-1;
while((t!=NULL) || (s.top!=-1))
{
while (t)
{
push(&s,t);
t=t->lchild;
}
if (s.top!=-1)
{
t=pop(&s);
printf("%c ",t->data);
t=t->rchild;
}
}
}
4、二叉树后序遍历的非递归函数
void postorder1(bintree t) /*非递归实现二叉树的后序遍历*/
{
seqstack s;
s.top=-1;
while ((t)||(s.top!=-1))
{
while (t)
{
s.top++;
s.data[s.top]=t;
s.tag[s.top]=0;
t=t->lchild;
}
while ((s.top>-1)&& (s.tag[s.top]==1))
{
t=s.data[s.top];
printf("%c ",t->data);
s.top--;
}
if (s.top>-1)
{
t=s.data[s.top];
s.tag[s.top]=1;
t=t->rchild;
}
else t=NULL;
}
}
5、补充并运行二叉树的递归前序遍历、递归中序遍历、递归后序遍历的函数。
(1)前序遍历二叉树的递归算法
void preorder(bintree t)
{
if (t)
{
printf("%c",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
(2) 中序遍历二叉树的递归算法
void inorder(bintree t)
{
if (t)
{
inorder(t->lchild);
printf("%c",t->data);
inorder(t->rchild);
}
}
(3)后序遍历二叉树的递归算法
void postorder(bintree t)
{
if (t)
{
postorder(t->lchild);
postorder(t->rchild);
printf("%c",t->data);
}
}
作业四 排序
作业目的:掌握排序的各种算法思想,并能通过其解决应用问题。
作业要求:
(1)给出程序设计的基本思想、原理和算法描述。
(2)对源程序给出注释。
(3)保存程序的运行结果,并结合程序进行分析。
作业内容:
1、给定一含20个整型数据的数组,利用堆排序的思想将其进行非降序排列。
2、给定一含20个整型数据的数组,利用快速排序方法将其进行升序排列。
3、给定一含20个整型数据的数组,利用希尔排序方法将其进行非降序排列。
作业五 查找
作业目的:掌握数据结构中查找的基本算法思想,并能进行应用。
作业要求:
(1)给出程序设计的基本思想、原理和算法描述。
(2)对源程序给出注释。
(3)保存程序的运行结果,并结合程序进行分析。
作业内容:
1、建立一颗含有10个结点的二叉查找树;
2、在已建立的二叉查找树上查找一符合给定元素值的结点是否存在。
*3、利用二分查找法在一个含有20个整数的有序表中插入一个元素,并保持表的有序性。

第1个回答  推荐于2016-03-04
这是我做的两个程序的代码:要是需要更多的排序算法的代码或者其他数据结构实现就跟我联系[email protected]
先给你复制这两个代码:
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define NULL 0
typedef struct bitnode{
char data;
struct bitnode *lchild,*rchild;
}bitnode,*bitree;
/*创建一个二杈树以#号结束*/
bitree create(bitree t){
char ch;
ch=getchar();
if(ch=='#')
t=NULL;
else{
t=(bitree)malloc(sizeof(bitnode));
t->data=ch;
t->lchild=create(t->lchild);
t->rchild=create(t->rchild);
}
return t;
}
/*递归遍历*/
void preorder(bitree t){
if(t){
printf("%c",t->data); /*先序*/
preorder(t->lchild);
/*printf("%c",t->data); 中序*/
preorder(t->rchild);
/*printf("%c",t->data); 后序*/
}
}
/*求深度*/
int depth(bitree t){
int depthval,depl,depr;
if(!t)
depthval=0;
else{
depl=depth(t->lchild);
depr=depth(t->rchild);
depthval=1+(depl>depr?depl:depr);
}
return depthval;
}

/*求叶子数*/
int countleaf(bitree t){
int count=0;
if(!t)
count=0;
else if((!t->lchild)&&(!t->rchild))
count++;
else
count=countleaf(t->lchild)+countleaf(t->rchild);
return count;

}
/*主函数*/
main(){
bitree t=NULL;
printf("\nplease input a tree:");
t=create(t);
preorder(t);
printf("\ndepth:%d\nleave:%d\n",depth(t),countleaf(t));
system("pause");
}

排序:

/******************************************************/
/*链表的建立,插入,删除,查找,倒置,直接插入排序,选择排序*/
/******************************************************/

#include "stdio.h"
#include "stdlib.h"
#define NULL 0
#define TRUE 1
#define FALSE 0
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*Linklist;
/*创建一个带头结点含有n个结点的单链表*/
Linklist create(){
Linklist L,p;
int i;
L=(Linklist)malloc(sizeof(LNode));
L->next=NULL;
printf("\nPlease input the count of node:");
for(scanf("%d",&i);i>0;i--){
p=(Linklist)malloc(sizeof(LNode));
printf("Please input the data of node:");
scanf("%d",&p->data);
p->next=L->next;
L->next=p;
}
return L;
}
/*显示个结点信息*/
void show(Linklist L){
Linklist p;
p=L->next;
while(p){
printf("%-5d",p->data);
p=p->next;
}
printf("\n");
}
/*链表倒置*/
void rebuid(Linklist L){
Linklist p,q;
p=L->next;
L->next=NULL;
while(p){
q=p;
p=p->next;
q->next=L->next;
L->next=q;
}
}
/*链表的插入*/
void insert(Linklist L){
Linklist p;
p=(Linklist)malloc(sizeof(LNode));
printf("Please input the data of node:");
scanf("%d",&p->data);
p->next=L->next;
L->next=p;

}
/*查找并删除*/
int search(Linklist L,int n){
Linklist p,q;
q=L;
p=L->next;
while(p){
if(p->data==n){
/*删除*/
q->next=p->next;
free(p);
return TRUE;
}
q=p;
p=p->next;
}
return FALSE;
}
/*直接插入排序*/
void insertsort(Linklist L){
Linklist p,q,r,t;
p=L->next->next;
t=L->next; /*p为无序表开头*/
while(p){
q=L->next;
r=L;
while(q!=p){
if(p->data<q->data){
t->next=p->next;
p->next=r->next;
r->next=p;
break;
}
r=q; /*r跟随q*/
q=q->next;
}
t=p; /*t跟随p*/
p=p->next;
}

}
/*简单选择排序*/
void selectsort(Linklist L){
int tmp;
Linklist p,q,r;
p=L->next;
while(p){
q=p->next;
r=p;
while(q){
if(q->data<r->data){
r=q;
}
q=q->next;
}
if(r!=p){
tmp=r->data;
r->data=p->data;
p->data=tmp;
}
p=p->next;
}

}
/*主函数*/
main(){
Linklist L=NULL;
L=create();
show(L);
/*添加需要调用的函数*/
system("pause");
}本回答被提问者采纳
第2个回答  2008-11-17
不得不说你是人才了~~~
第3个回答  2008-11-16
bup
相似回答