当前位置: 代码迷 >> C语言 >> 求!哪个大哥帮我做下题目
  详细解决方案

求!哪个大哥帮我做下题目

热度:316   发布时间:2008-06-05 13:13:05.0
求!哪个大哥帮我做下题目
1.排序算法比较
利用随机函数产生30000个随机整数,利用所学的排序方法进行排序,并统计每一种排序上机所花费的时间。

2.图的深度遍历
对任意给定的图(顶点数和边数自定),建立它的邻接表并输出,然后利用堆栈的五种基本运算(清空堆栈、压栈、弹出、取栈顶元素、判栈空)实现图的深度优先搜索遍历。

3.图的广度遍历     对任意给定的图(顶点数和边数自定),建立它的邻接表并输出,然后利用队列的五种基本运算(置空队列、进队、出队、取队头元素、判队空)实现图的广度优先搜索遍历。

4.二叉树的遍历
对任意给定的二叉树(顶点数自定)建立它的二叉链表存贮结构,并利用栈的五种基本运算(置空栈、进栈、出栈、取栈顶元素、判栈空)实现二叉树的先序、中序、后序三种遍历,输出三种遍历的结果。


随便选一题    我不会编程啊         要交课程设计     一个星期后就要交       93803021@163.com   本人邮箱  每次发的贴没一个人帮我解决  希望这次有个好心的帮我下忙
搜索更多相关的解决方案: 堆栈  遍历  深度  整数  

----------------解决方案--------------------------------------------------------
厉害厉害,哈哈



[color=white]
----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------
很想帮你,可惜我数据结构很烂.
----------------解决方案--------------------------------------------------------
/* Note:Your choice is C IDE */
#include "stdio.h"
#include<stdlib.h>
typedef struct bitnode
{
    int data;
    struct bitnode *lchild,*rchild;
}bitnode,*bittree;
bittree cre_tree()
{
    int x;
    bittree root;
    scanf("%d",&x);
    if(x==-1)root=NULL;
    else
    {
        root=(bittree)malloc(sizeof(bitnode));
        root->data=x;
        root->lchild=cre_tree();
        root->rchild=cre_tree();
    }
    return root;
}
void preorder(bittree root)
{
    if(root)
    {
        printf("%d",root->data);
        preorder(root->lchild);
        preorder(root->rchild);
    }
}
void inorder(bittree root)
{
    if(root)
    {
        inorder(root->lchild);
        printf("%d",root->data);
        inorder(root->rchild);
    }
}
void postorder(bittree root)
{
    if(root)
    {
        postorder(root->lchild);
        postorder(root->rchild);
        printf("%d",root->data);
    }
}
bittree exchang(bittree root)
{
    bittree t;
    if(root)
    {
        if(root->lchild!=NULL&&root->rchild!=NULL)
        {
            t=root->lchild;
            root->lchild=root->rchild;
            root->rchild=t;
        }
        root->lchild=exchang(root->lchild);
        root->rchild=exchang(root->rchild);
    }
    return root;
}
int hightree(bittree root)
{
    int H,H1,H2;
    if(root==NULL)H=0;
    else
    {
        H1=hightree(root->lchild);
        H2=hightree(root->rchild);
        H=(H1>H2?H1:H2)+1;
    }
    return H;
}
int fine=0;
void searchtree(bittree root,int x,bittree *p)
{
    if(root&&!fine)
    if(root->data==x)
    { fine=1;*p=root; }     //若找到,则通过p带回该结点的指针
    else
    {
        *p=NULL;
        searchtree(root->lchild,x,p);
        searchtree(root->rchild,x,p);
    }
}
int n=0;
void leafcount(bittree root)
{
    if(root)
    {
        if(root->lchild==NULL&&root->rchild==NULL)
        n++;
        leafcount(root->lchild);
        leafcount(root->rchild);
    }
}
void main()
{
    int H,x;
    bittree root,p;
    root=cre_tree();
    preorder(root);printf("\n\n");
    inorder(root);printf("\n\n");
    postorder(root);printf("\n\n");
    root=exchang(root);preorder(root);printf("\n\n");
    H=hightree(root);
    printf("二叉树的深度为:");printf("%d\n",H);
    scanf("%d",&x);searchtree(root,x,&p);
    printf("要找的数据为:");
    printf("%d",p->data);printf("\n");
    leafcount(root);
    printf("叶子的结点个数为:");printf("%d",n);
}
----------------解决方案--------------------------------------------------------
/* Note:Your choice is C IDE */
#include "stdio.h"
#include<stdlib.h>
typedef struct bitnode
{
    char ch;
    struct bitnode *lchild,*rchild;
}bitnode,*bittree;
typedef struct stacknode
{
    bitnode *data;
    struct stacknode *next;
}linkstack;
void initstack(linkstack *s)
{ s->next=NULL; }
void push(linkstack *s,bittree x)
{
    linkstack *p;
    p=(linkstack *)malloc(sizeof(linkstack));
    p->data=x;
    p->next=s->next;
    s->next=p;
}
bittree pop(linkstack *s)
{
    bittree x;
    linkstack *p;
    p=s->next;
    if(s->next==0)return 0;
    x=p->data;
    s->next=p->next;
    free(p);
    return x;
}
bittree cre_tree()
{
    bittree root;
    char ch;
    ch=getchar();
    if(ch=='#')root=NULL;
    else
    {
        root=(bittree)malloc(sizeof(bitnode));
        root->ch=ch;
        root->lchild=cre_tree();
        root->rchild=cre_tree();
    }
    return root;
}
void preorder(bittree root)
{
    linkstack s;
    bittree p;
    initstack(&s);
    push(&s,root);
    while(s.next)
    {
        p=pop(&s);
        while(p)
        {
            printf("%c",p->ch);
            if(p->rchild)push(&s,p->rchild);
            p=p->lchild;
        }
    }
}
void inorder(bittree root)
{
    bittree p;
    linkstack s;
    initstack(&s);
    p=root;
    while(p||s.next)   //注意还有右子树也要判断
    {
        while(p)
        {
            push(&s,p);
            p=p->lchild;
        }
        p=pop(&s);
        printf("%c",p->ch);
        p=p->rchild;
    }
}
void main()
{
    bittree root;
    root=cre_tree();
    preorder(root);printf("\n\n");
    inorder(root);printf("\n\n");
}

[[it] 本帖最后由 wangyinshiwo 于 2008-6-5 15:36 编辑 [/it]]
----------------解决方案--------------------------------------------------------
我估计你要第N次失望了
----------------解决方案--------------------------------------------------------
好程序
看到你们写的程序我不得不说一声“牛”
我不管怎么看都看不明白   可能是我的能力有限!!!
还请高手多点火请教!!!
----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------
看我签名
----------------解决方案--------------------------------------------------------
  相关解决方案