poj 2003 数据结构+先序遍历

#include<iostream>
#include<list>
#include<vector>
#include<map>
#include<cstring>
#include<cstdio>
#include<string>
using namespace std;
struct stu
{
    string name;
    stu *parent;
    list<stu *> son;
    stu(){parent=NULL;}
};
map<string,stu*> hassh;
stu *root;
void hires(string n1,string n2)
{
    stu *neww=new stu();
    neww->name=n2;
    neww->parent=hassh[n1];
    hassh[n1]->son.push_back(neww);
    hassh[n2]=neww;
}
void fire(string n)
{
    stu *now=hassh[n];
    stu *parent=now->parent;
    hassh.erase(n);
    while(now->son.size())//实在是太巧妙了,哈哈哈哈哈,今天最大的收获,换形不换神
    {
        now->name=now->son.front()->name;
        hassh[now->name]=now;
        now=now->son.front();
    }
    now->parent->son.remove(now);
    delete now;
}
void print(int deep,stu *now)
{
    if(now==NULL)
        return;
    for(int i=0;i<deep;i++)
        putchar('+');
    cout<<now->name<<endl;
    for(list<stu*>::iterator it=now->son.begin();it!=now->son.end();++it)
        print(deep+1,*it);
}
int main()
{
    string s1,s2;
    cin>>s1;
    root=new stu();
    hassh[s1]=root;
    root->name=s1;
    while(cin>>s1)
    {
        if(s1=="fire")
        {
            cin>>s2;
            fire(s2);
        }
        else if(s1=="print")
        {
            print(0,root);
            for(int i=0;i<=59;i++)
                putchar('-');
            putchar('\n');
        }
        else
        {
            cin>>s2;
            cin>>s2;
            hires(s1,s2);
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值