呦呦鹿鸣xyzc 2021-10-03 17:23 采纳率: 60%
浏览 129
已结题

带头结点单链表的就地倒置

reverse部分的节点是怎样变化的?


#include<stdio.h>
#include<stdlib.h>
struct Node{
    int data;
    struct Node *next;
};
const int sizeN=sizeof(struct Node);
void output(struct Node*head){
    while(head=head->next)
        printf("%d    ",head->data);
    printf("\n");
}
struct Node *reverse(struct Node*head){
    struct Node *p=head->next,*q=p->next;
    free(head);
    p->next=NULL;
    while(head=q->next){
        q->next=p;
        p=q;
        q=head;
    }
    q->next=p;
    head=malloc(sizeN);
    head->next=q;
    return head;
}
int main(){
    struct Node *head=malloc(sizeN),
        *p=head,*q;
    head->next=p;
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        q->data=n;
        q->next=NULL;
        p=p->next=q;
        q=malloc(sizeN);
    }
    p->next=NULL;
    free(q);
    output(head);
    head=reverse(head);
    output(head);
    return 0;
}
  • 写回答

1条回答 默认 最新

  • qzjhjxj 2021-10-03 17:47
    关注

    修改如下,见注释,供参考:

    #include<stdio.h>
    #include<stdlib.h>
    struct Node{
        int data;
        struct Node *next;
    };
    const int sizeN=sizeof(struct Node);
    void output(struct Node*head){
        while(head=head->next)
            printf("%d    ",head->data);
        printf("\n");
    }
    void  reverse(struct Node*head){  //struct Node *reverse(struct Node*head){//修改
        struct Node *p=head->next,*q=NULL; //struct Node *p=head->next,*q=p->next;//修改
                                           //free(head);  //修改
        head->next = NULL;                //p->next=NULL; //修改
        while(p){
            q = p;
            p = p->next;   //q->next=p;   //修改
            q->next = head->next;//p=q;   //修改
            head->next = q; //q=head;     //修改
        }
                           //q->next=p;    //修改
                           //head=(struct Node*)malloc(sizeN);
                            //head->next=q;
                            //return head;
    }
    int main(){
        struct Node *head=(struct Node*)malloc(sizeN);// ,修改
        struct Node *p=head,*q;   //修改
        head->next=NULL;  //head->next=p; 修改
        int t;
        scanf("%d",&t);
        while(t--){
                          //int n;   //修改
            q=(struct Node*)malloc(sizeN);//修改
            q->next=NULL;
            scanf("%d",&q->data);      //修改
                          //q->data=n; //修改
            p->next=q;     //修改
            p = q;         //修改
        }
        p->next=NULL;
                           //free(q); //修改
        output(head);
    
        reverse(head);    //修改
        output(head);
        
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月11日
  • 已采纳回答 10月3日
  • 创建了问题 10月3日

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果