这是关于单链表的分段逆转的题?我是打算着分段输出,但是这个代码好像逻辑有问题,大佬帮帮忙!

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
    int data;
    struct node *next;
}LNode,*Linklist;
Linklist Creat_list(Linklist head)
{
    head=(Linklist)malloc(sizeof(LNode));
    LNode *node=NULL;
    int count=0;
    head->next=NULL;
    node=head->next;
    scanf("%d",&count);
    for(int i=0;i<count;i++)
    {
        node=(Linklist)malloc(sizeof(LNode));
        node->data=i;
        node->next=head->next;
        head->next=node;
    }
    return head;
}
void K_Reverse(Linklist head,int K)
{
    Linklist p,newhead,H,node,head2[10],G[10];
    int count=0;
    G[1]=H=head->next;
    newhead=NULL;
    while(H)
    {
        count++;
        H=H->next;
    }
    head=head->next;
    if(K>1&&K<=count)
    {
        for(int i=1;i<=count/K;i++)
        {
            for(int j=0;j<K;j++)
            {
                node=head;
                head=head->next;
                node->next=newhead;
                newhead=node;
            }
            head2[i]=node;
            G[i+1]=head;
            G[i-1]->next=head2[i];
        }
    }
    G[count/K]->next=G[count/K+1];
    for(int i=1;i<=count/K;i++)
    {
        printf("%d %d",head2[i],G[i]);
    }
    for(p=G[count/K+1];p<H;p++)
        printf("%d",*p);
}
int main()
{
    Linklist head;
    int K;
    scanf("%d",&K);
    Creat_list(head);
    K_Reverse(head,K);
    return 0;
}


我这个比较低端,要求数据是有序的输入的

阅读 717
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进