uva540 team queue

简介: uva540 team queue
#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAXN 1000000#define LOCALtypedefstructnode{
structnode*next;
intdata;
} node;
typedefstructteam_node{
node*head;
node*rear;
structteam_node*next;
}team_node;
intteam[MAXN];
intnum_team;
voidinit(team_node**head);
voidenqueue(team_node**head, team_node**tail, intelement);
intsearch_team(int*team, intelement);
intdequeue(team_node**head);
intmain()
{
team_node*head=NULL;
team_node*tail=NULL;
intnum;
intno;
inti, j;
charbuf[10];
intelement;
inttests=0;
#ifndef LOCALfreopen("c://uva_in.txt", "r", stdin);
#endifwhile(scanf("%d", &num_team) &&num_team!=0)
    {
memset(team, -1, sizeof(team));
for(i=0; i<num_team; i++)
        {
scanf("%d", &num);
for(j=0; j<num; j++)
            {
scanf("%d", &no);
team[no] =i;
            }
        }
head= (team_node*)malloc(sizeof(structteam_node));
init(&head);
tail=NULL;
printf("Scenario #%d/n", ++tests);
while(scanf("%s", buf) &&strcmp(buf, "STOP") !=0)
        {
if(strcmp(buf,"ENQUEUE") ==0)
            {
scanf("%d", &element);
enqueue(&head, &tail, element);
            } 
else            {
element=dequeue(&head);
printf("%d/n", element);
            }
        }
printf("\n");
    }
return0;
}
voidinit(team_node**head)
{
    (*head)->next=NULL;
    (*head)->head= (*head)->rear=NULL;
}
voidenqueue(team_node**head, team_node**tail, intelement)
{
team_node*p;
node*q;
intteam1, team2;
intdata;
intflag;
if ((*head)->next==NULL)
    {
q= (node*)malloc(sizeof(structnode));
q->data=element;
q->next=NULL;
p= (team_node*)malloc(sizeof(structteam_node));
p->next=NULL;
p->head=p->rear= (node*)malloc(sizeof(structnode));
p->rear->next=q;
p->rear=q;
        (*head)->next=p;
*tail=p;
    }
else    {
flag=0p= (*head)->next;
while(p)
        {
data=p->head->next->data;
team1=search_team(team, data);
team2=search_team(team,  element);
if(team1==team2)
            {
q= (node*)malloc(sizeof(structnode));
q->data=element;
q->next=NULL;
p->rear->next=q;
p->rear=q;
flag=1;
break;
            }
elsep=p->next;
        }
if (!flag)
        {
q= (node*)malloc(sizeof(structnode));
q->data=element;
q->next=NULL;
p= (team_node*)malloc(sizeof(structteam_node));
p->next=NULL;
p->head=p->rear= (node*)malloc(sizeof(structnode));
p->rear->next=q;
p->rear=q;
            (*tail)->next=p;
*tail=p;
        }
    }
}
intsearch_team(int*team, intelement)
{
returnteam[element];
}
intdequeue(team_node**head)
{
team_node*p;
node*q;
intdata;
p= (*head)->next;
q=p->head->next;
data=q->data;
p->head->next=q->next;
free(q);
if(p->rear==q)
    {
p->rear=p->head;
        (*head)->next=p->next;
free(p->rear);
free(p);
    }
returndata;
}
目录
相关文章
|
9月前
uva133 The Dole Queue
uva133 The Dole Queue
42 0
|
9月前
UVa11076 - Add Again
UVa11076 - Add Again
40 0
|
C++
STL --- UVA 123 Searching Quickly
UVA - 123 Searching Quickly Problem's Link:   http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19296   Mean:  有一个字符串集合Ignore,还有一个文本集合TXT,在TXT中除了Ignore中的单词外其他的都是关键字,现在要你根据这些关键字来给TXT文本排序(根据关键字的字典)。
1071 0
uva 540 Team Queue
点击打开链接uva 540 思路: list+map模拟 分析: 1 题目的意思是有n个队伍,每个队伍的人数为m。 2 现在有三种操作,ENQUEUE x是插入x,如果队列里面已经有x这一队的成员那么直接插入到这一队的最后一个,否则插入到...
833 0
uva 10795 A Different Task
点击打开链接uva 10795 思路:递归 分析:(转载网友,写的不错) 点击打开链接 1  新汉诺塔:标准的汉诺塔上有n个大小各异的盘子。
798 0