双循环链表

//nyoj511移动小球

#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
    int data;
    struct node *pre, *rear;
}Lnode;//结点
Lnode *creat_list(Lnode *head, int num)//创建不带头结点的双循环链表
{
    Lnode *current, *phead;
    phead = head;
    int i;
    for(i = 2; i <= num; i++)//从第二个结点创建,可以避免第一个结点单独处理的问题
    {
        current = (Lnode *)malloc(sizeof(Lnode));
        if(current == NULL)
        {
            printf("no memory available\n");
            exit(1);
        }
        current->data = i;
        current->rear = current->pre = NULL;
        phead->rear = current;
        current->pre = phead;
        phead = current;
    }
    current->rear = head;//创建完成后将链表的首和尾链接起来
    head->pre = current;
    return head;
}
Lnode *move_a_list(Lnode *head, int x, int y)
{
    Lnode *phead_x, *phead_y, *move_node;
    phead_x = head;
    phead_y = head;
    while(phead_x->data != x)
        phead_x = phead_x->rear;
    move_node = phead_x;
    phead_x->pre->rear = phead_x->rear;//不需要free只需要移动位置就行
    phead_x->rear->pre = phead_x->pre;
    while(phead_y->data != y)
        phead_y = phead_y->rear;
    phead_y->pre->rear = move_node;
    move_node->pre = phead_y->pre;
    move_node->rear = phead_y;
    phead_y->pre = move_node;
    return head;
}
Lnode *move_b_list(Lnode *head, int x, int y)
{
    Lnode *phead_x, *phead_y, *move_node;
    phead_x = head;
    phead_y = head;
    while(phead_x->data != x)
        phead_x = phead_x->rear;
    move_node = phead_x;
    phead_x->pre->rear = phead_x->rear;
    phead_x->rear->pre = phead_x->pre;
    while(phead_y->data != y)
        phead_y = phead_y->rear;
    phead_y->rear->pre = move_node;
    move_node->rear = phead_y->rear;
    move_node->pre = phead_y;
    phead_y->rear = move_node;
    return head;
}
void print_q_list(Lnode *head, int x, int y)
{
    Lnode *phead;
    phead = head;
    while(phead->data != y)
        phead = phead->rear;
    if(x == 1)
        printf("%d\n", phead->rear->data);
    else
        printf("%d\n", phead->pre->data);
}
/*void print_list(Lnode *head)
{
    Lnode *phead;
    phead = head;
    printf("%d\t", phead->data);
    phead = phead->rear;
    while(phead != head)
    {
        printf("%d\t", phead->data);
        phead = phead->rear;
    }
}*/
int main()
{
    int n, num, m, x, y;
    scanf("%d", &n);
    Lnode *head = NULL;
    char ch;
    head = (Lnode *)malloc(sizeof(Lnode));
    if(head == NULL)
    {
        printf("no memory available\n");
        exit(1);
    }
    head->data = 1;
    head->rear = head->pre = NULL;//在主函数里创建第一个结点,注意这里是赋为NULL
    while(n--)
    {
        scanf("%d%d", &num, &m);
        head = creat_list(head, num);
        while(m--)
        {
            getchar();
            scanf("%c%d%d", &ch, &x, &y);
            if(ch == 'A')
                head = move_a_list(head, x, y);
            else if(ch == 'B')
                head = move_b_list(head, x, y);
            else
                print_q_list(head, x, y);
        }
    }
   // print_list(head);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值