手写链表RE,求求帮看哪里出了问题
查看原帖
手写链表RE,求求帮看哪里出了问题
61101
hfnan楼主2020/12/30 19:57
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstring>
#include <map>
using namespace std;
map<int,int> team;//每个人的编号 到 团队号 的映射

typedef struct lst{
    int data;
    struct lst *next;
}lst;
//手写链表模拟队列
map<int,lst*> pos;//团队号 到 最后一名队员的位置 的映射,用于插入队员
lst *head=(lst*)malloc(sizeof(lst));//队首
lst *tail=head;//队尾

void gointo(int x)//入队
{
    if(pos.count(team[x]))//曾经有相同团队队员入队 
    {
        lst *nod=(lst*)malloc(sizeof(lst));
        nod->data=x;
        nod->next=pos[team[x]]->next;
        if(pos[team[x]]->next==NULL) tail=nod;
        pos[team[x]]->next=nod;
        pos[team[x]]=nod;
    }
    else
    {
        lst *nod=(lst*)malloc(sizeof(lst));
        tail->next=nod;
        nod->data=x;
        tail=nod;
        tail->next=NULL;
        pos[team[x]]=tail;
    }
}
void goout()//出队
{
    int x= head->next->data;
    cout<<x<<endl;
    lst *p=head->next->next;
    if(p==NULL) tail=head;
    free(head->next);
    head->next=p;
}
int main()
{
    
    int t,cnt=0;
    while(cin>>t,t!=0)
    {
        cnt++;
        cout<<"Scenario #"<<cnt<<endl;
        team.clear();
        pos.clear();
        tail=head;
        tail->next=NULL;
        for(int i=1;i<=t;i++)
        {
            int n;
            cin>>n;
            for(int j=1;j<=n;j++)
            {
                int tem;
                cin>>tem;
                team[tem]=i;//同一团队的队员有相同团队号
            }
        }
        string s;
        while(cin>>s,s!="STOP")
        {
            if(s[0]=='E') 
            {
                int x;
                cin>>x;
                gointo(x);//入队
            } 
            if(s[0]=='D') goout();//出队
        }
    }
    free(head);
    return 0;
}

思路是 手写链表模拟队列,用map实现查找

2020/12/30 19:57
加载中...