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

发布于 2022-09-13 01:13:00 字数 1563 浏览 15 评论 0

#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;
}


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文