数据结构操作复习

数据结构操作复习

[toc] 1.3 2.4 2.3 2.5

1.8删除节点
int List::Remove(int i)
{
    LinkNode *del, *current = first->link;
    int k = 1;
    if(k == 1){
        del = first->link;
        first->link = del->link;
        delete del;
        return 1;
    }
    while(k < i-1)
    {
        if(current == NULL)
            break;
        else
            current = current->link;
        k++;
    }
    if(current == NULL || current->link == NULL)
    {
        cout << "链表太短";
        return 0;
    }
    del = current->link;
    current->link = del->link;
    delete del;
    return 1;
}
2.1二叉树的先序输入
void BinaryTree::input(LinkNode*& p){
    char C;
    cin >> C;
    if(C != '@'){
        p = new LinkNode(C);
        input(p->leftchild);
        input(p->rightchild);
    }
    else
    	p = NULL;		//不能省
}
2.3删除跟为p的子树
void BinaryTree::destroy(LinkNode *p){
    if(p != NULL){
        destroy(p->leftchild);
        destroy(p->rightchild);
        delete p;
    }
}
2.4给定一个subtree节点,从他开始找current节点的父节点
LinkNode* BinaryTree::Parent(LinkNode* subtree, LinkNode* current){
    if(subtree == NULL)     return NULL;
    if(subtree->leftchild == current || subtree->rightchild == current){ return subtree;cout << "2";}
    LinkNode *p;
    if((p = Parent(subtree->leftchild, current)) != NULL) return p;
    else return Parent(subtree->rightchild, current);
}
2.5递归计算二叉树的表达式
int BinaryTree::cal(LinkNode *p){
    if(val(p) != -1){ cout << val(p); return val(p);}
    else{
        int a;
        if(p->leftchild->op>1 &&p->leftchild->op < p->op){
            cout<<"(";
            a=cal(p->leftchild);
            cout<<")";
        }else{
            a=cal(p->leftchild);
        }

        cout<<p->data;

        int b;
        if(p->rightchild->op>1 && p->rightchild->op < p->op){
            cout<<"(";
            b=cal(p->rightchild);
            cout<<")";
        }else{
            b=cal(p->rightchild);
        }

        if(p->data=='+'){
            return a+b;
        }else if(p->data==&#
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值