pat(乙级)1025 反转链表

//1025 反转链表
    //结点地址  结点对应的数   下一结点地址
    //Address        Data          Next
    //add[]          L[][0]        L[][1]
#include 
struct node
{
    int address;
    int data;
    int next;
}list[100000], t;
int main()
{
    int n, k, i, j,l,r, first;
    //首结点地址   //结点总个数(n<=100000)  //需反转结点个数( k<=n)        
    // first             n                           k
    // 00100             6                           4
    scanf("%d%d%d", &first, &n, &k);
    for (i = 0; i < n; i++)//输入各结点
    {
        scanf("%d%d%d", &list[i].address, &list[i].data, &list[i].next);
    }
    
    //调整链表
    for (i = 0; i < n; i++)
    {
        for (j = i; j < n; j++)
        {
            if (list[j].address == first)
            {
                t = list[i];
                list[i] = list[j];
                list[j] = t;
                first = list[i].next;
                if (first == -1)
                    n = i + 1;
                {
                    break;
                }
            }   
        }
    }
    //反转链表
    for (i = 0; i < n; i += k) //k=4.    
    {
        l = i;
        r = i + k - 1;        //目标结点对应数组下标 
        if (r < n)
        {
            while (l < r)
            {
                t = list[l];
                list[l] = list[r];
                list[r] = t;
                l++;
                r--;
            }
        }
        else
        {
            break;
        }
    }
    //打印链表
    for (i = 0; i < n; i++)
    {
        i == n - 1 ? printf("%05d %d -1\n", list[i].address, list[i].data) : printf("%05d %d %05d\n", list[i].address, list[i].data, list[i + 1].address);
    }
    return 0;
}

你可能感兴趣的:(C语言经典题目,c语言)