您的位置:首页 > 产品设计 > UI/UE

uva133 The Dole Queue

2010-10-23 11:42 429 查看
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define LOCAL

#define MAXN 30

typedef struct node

{

    int data;

    struct node  *pre, *next;

}*Node;

Node link[MAXN] ;

void init(int n, Node *head, Node *rear);

int main()

{

    int n, k, m;

    Node head, rear;

    int count1, count2;

    Node p, q, r, s;

    int first;

    head = (Node)malloc(sizeof(struct node));

    rear = (Node)malloc(sizeof(struct node));

    #ifdef LOCAL

        freopen("c://uva_in.txt", "r", stdin);

    #endif

    while (scanf("%d%d%d", &n, &k, &m) == 3 && !(n == 0 && k == 0 && m == 0))

    {

        init(n, &head, &rear);

        p = head;

        q = rear;

        first = 1;

        count1 = count2 = 1;

        while(n > 0)

        {

            while (count1 < k)

            {

                p = p->next;

                count1++;

            }

            while (count2 < m)

            {

                q = q->pre;

                count2++;

            }

            if (p != q)

            {

                if (first)

                    first = 0;

                else

                    printf(",");

                printf("%3d%3d", p->data, q->data);

                if (n > 2)

                {

                    if (p->next != q)

                    {

                        p->pre->next = p->next;

                        p->next->pre = p->pre;

                        r = p;

                        p = p->next;

                        free(r);

                        q->pre->next = q->next;

                        q->next->pre = q->pre;

                        r = q;

                        q = q->pre;

                        free(r);

                    } else

                    {

                        p->pre->next = q->next;

                        q->next->pre = p->pre;

                        r = p;

                        s = q;

                        p = q->next;

                        q=r->pre;

                        free(r);

                        free(s);

                    }

                } else if (n == 2)

                {

                    free(p);

                    free(q);

                }

                n -= 2;

            }else

            {

                if (first)

                    first = 0;

                else

                    printf(",");

                printf("%3d", p->data);

                if (n > 1)

                {

                    p->pre->next= p->next;

                    p->next->pre = p->pre;

                    r = p;

                    p = p->next;

                    q = q->pre;

                    free(r);

                } else if (n == 1)

                {

                    free(p);

                }

                n -= 1;

            }

            count1 = count2 = 1;

        }

        printf("/n");

    }

    return 0;

}

void init(int n, Node *head, Node *rear)

{

    int i;

    Node p, q;

    for (i = 0; i < n; i++)

    {

        p = (Node)malloc(sizeof(struct node));

        p->data = i + 1;

        if (i == 0)

        {

            *head = p;

            p->next = NULL;

            q = p;

        } else

        {

            p->pre = q;

            p->next = NULL;

            q->next = p;

            q = p;

        }

        if (i == n - 1)

            *rear = q;

    }

    (*head)->pre = *rear;

    (*rear)->next = *head;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struct include null c