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

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;
    }
     
    点赞 1 评论
  • 把push 函数的 head = newn;;语句改为 head.next =newn;

    点赞 评论
  • 这样改试试

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

    Node *head = *headRef;

    struct node *newn = malloc(sizeof(struct node)); //

    newn->data = newData;

    newn->next = head;

    if (head.next == null){

           head = newn;

    }else{

         head.next = newn

    }

    *headRef = head;

    }

    点赞 评论
  • 小白小白你好菜 2021-04-07 09:03

    点赞 评论
  • 小白小白你好菜 2021-04-07 09:11

    这种是保留头结点的做法。

    void RecursiveReverse(Node**headRef){
    	Node *p = *headRef;
    	Node *q;
    	*headRef = NULL;
    	while(p->next != NULL){
    		q = p->next;
    		p->next = *headRef;
    		*headRef = p;
    		p = q;
    	}
    	p->next = *headRef;
    	*headRef = p;
    }
     
    void printQueue(Node* l){
        while(l->next != NULL){//
        	l = l->next;
            printf("data is %d\n", l->data);
        }
        printf("\n");
    }
    点赞 评论
  • 小白小白你好菜 2021-04-07 09:42

    主要就是你的当前栈顶所在位置和头结点位置你没搞清楚。

    4 4 3 2 1 头结点 NULL

    你当前栈顶在4, l->next指向第二个4

    所以你的代码里面顺序输出时 4 3 2 1 头结点。把第一个4跳过了。我只能解释到这了。要靠你自己想明白了

    点赞 评论
  • 有问必答小助手 2021-04-07 09:51

    您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    点赞 评论

相关推荐 更多相似问题