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
|
Java
2017 Multi-University Training Contest - Team 9 1005&&HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】
FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1060    Accepted Submission(...
1178 0
|
Java
2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】
Ch’s gift Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1354    Accepted Submission(s): 496 Problem Description Mr.
1269 0
|
Java
[LeetCode]--225. Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. empty() –
1282 0
[LeetCode]--232. Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) – Push element x to the back of queue. pop() – Removes the element from in front of queue. peek() – Get the front elem
920 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文本排序(根据关键字的字典)。
1069 0