.T21 2021-04-07 07:12 采纳率: 50%
浏览 204
已采纳

C语言反向输出链表.

无法正确反向输出链表

#include <stdio.h>
#include "string.h"
#include "stdlib.h"
typedef struct node {
    int data;
    struct node* next;
}Node;

int main() {
    struct node *newn = malloc(sizeof(struct node));
    struct node *newn2 = malloc(sizeof(struct node));
    newn->next = NULL;
    Push(&newn, 1);
    Push(&newn, 2);
    Push(&newn, 3);
    Push(&newn, 4);
    Push(&newn, 4);

    
    RecursiveReverse(&newn);//?
    printQueue(newn);
    return 0;

}

void Push(Node**headRef, int newData){


    Node *head = *headRef;
    struct node *newn = malloc(sizeof(struct node)); //
    newn->data = newData;
    newn->next = head;
    head = newn;

    *headRef = head;

}
void RecursiveReverse(Node**headRef){

    struct node* head = (*headRef);
    if(head->next->next !=NULL){
        RecursiveReverse(&(head->next));
    }
    while(head->next !=NULL){
        head = head->next;
    }
    struct node* newn2 = (*headRef);
    (*headRef) = (*headRef)->next;
    head->next = newn2;
    newn2->next =NULL;



}

void printQueue(Node* l){
    while(l->next != NULL){//
        printf("data is %d\n", l->data);
        l = l->next;
    }
    printf("\n");
}

推入的数据应该是1,2,3,4,4,结果应该是12344

但是这个输出的是

data is 11950480
data is 1
data is 2
data is 3
data is 4

不知道哪里出问题了

  • 写回答

7条回答 默认 最新

  • 小白小白你好菜 2021-04-07 09:02
    关注

    改好了,题主可以看一下,栈逆置有点特殊

    #include <stdio.h>
    #include "string.h"
    #include "stdlib.h"
    typedef struct node {
        int data;
        struct node* next;
    }Node;
    
    void Push(Node**headRef, int newData){
     
     
        Node *head = *headRef;
        struct node *newn = (Node *)malloc(sizeof(struct node)); //
        newn->data = newData;
        newn->next = head;
        head = newn;
        *headRef = head;
    }
    
    
    void RecursiveReverse(Node**headRef){
    	Node *p = *headRef;
    	Node *q;
    	*headRef = NULL;
    	while(p->next->next != NULL){
    		q = p->next;
    		p->next = *headRef;
    		*headRef = p;
    		p = q;
    	}
    	p->next = *headRef;
    	*headRef = p;
    }
     
    void printQueue(Node* l){
        while(l != NULL){//
            printf("data is %d\n", l->data);
            l = l->next;
        }
        printf("\n");
    }
    
    int main() {
        struct node *newn = (Node *)malloc(sizeof(struct node));
        newn->next = NULL;
        Push(&newn, 1);
        Push(&newn, 2);
        Push(&newn, 3);
        Push(&newn, 4);
        Push(&newn, 4);
        RecursiveReverse(&newn);//?
        printQueue(newn);
        return 0;
    }
     
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab