uva101 The Blocks Problem

本文提供了一个使用双向链表实现特定操作的示例代码,包括初始化链表、搜索节点和释放内存等基本功能。通过解析不同的命令字符串来执行如移动节点到另一个节点上方或上方空位等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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] ;
int n;

void init();
Node search(int data, int *pos);
void freespace();

int main()
{
    char str1[5], str2[5];
    int src, dst;
    Node a, b, p, q;
    int flag;
    int i;
    int pos1, pos2;

    #ifdef LOCAL
        freopen("c://uva_in.txt", "r", stdin);
    #endif

    scanf("%d", &n);

    init();

    while (scanf("%s", str1) && (strcmp(str1, "quit") != 0))
    {
        scanf("%d%s%d", &src, str2, &dst);
        a = search(src, &pos1);
        b = search(dst, &pos2);

        if (strcmp(str1, "move") == 0 && strcmp(str2, "onto") == 0)
            flag = 1;
        else if (strcmp(str1, "move") == 0 && strcmp(str2, "over") == 0)
            flag = 2;
        else if (strcmp(str1, "pile") == 0 && strcmp(str2, "onto") == 0)
            flag = 3;
        else if (strcmp(str1, "pile") == 0 && strcmp(str2, "over") == 0)
            flag = 4;

        if (flag == 1 && pos1 != pos2)
        {
            p = a->next;
            a->next = NULL;
            while (p)
            {
                q = p->next;
                p->next = NULL;
                p->pre = link[p->data ];
                link[p->data ]->next = p;
                p = q;
            }

            p = b->next;
            b->next = NULL;
            while (p)
            {
                q = p->next;
                p->next = NULL;
                p->pre = link[p->data];
                link[p->data]->next = p;
                p = q;
            }

            a->pre->next = NULL;
            a->pre = b;
            b->next = a;
        } else if (flag == 2 && pos1 != pos2)
        {
            p = a->next;
            a->next = NULL;
            while (p)
            {
                q = p->next;
                p->next = NULL;
                p->pre = link[p->data];
                link[p->data]->next = p;
                p = q;
            }

            while (b->next)
                b = b->next;

            a->pre->next = NULL;
            a->pre = b;
            b->next = a;
        } else if (flag == 3 && pos1 != pos2)
        {
            p = b->next;
            b->next = NULL;
            while (p)
            {
                q = p->next;
                p->next = NULL;
                p->pre = link[p->data];
                link[p->data]->next = p;
                p = q;
            }

            a->pre->next = NULL;
            a->pre = b;
            b->next = a;
        } else if (flag == 4 && pos1 != pos2)
        {
            while (b->next)
                b = b->next;

            a->pre->next = NULL;
            a->pre = b;
            b->next = a;
        }

    }

    for (i = 0; i < n; i++)
    {
        p = link[i]->next;
        printf("%d:", i);
        while (p)
        {
            printf(" %d", p->data);
            p = p->next;
        }
        printf("/n");
    }

    return 0;
}

void init()
{
    int i;
    Node p;

    for (i = 0; i < n; i++)
    {
        p = (Node)malloc(sizeof(struct node));
        p->data = i;
        p->next = NULL;
        link[i] = (Node)malloc(sizeof(struct node));
        link[i]->next = p;
        p->pre = link[i];
    }
}

Node search(int data, int *pos)
{
    int i;
    Node p;

    for (i = 0; i < n; i++)
    {
        p = link[i]->next;

        while(p)
        {
            if (p->data == data)
            {
                *pos = i;
                return p;
            }
            p = p->next;
        }

    }

    return NULL;
}

void freespace()
{
    int i;
    Node p, q;

    for (i = 0; i < n; i++)
    {
        p = link[i]->next;
        while(p)
        {
            q = p->next;
            free(p);
        }
        free(link[i]);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值