shround_ 2020-05-12 09:19 采纳率: 50%
浏览 278
已采纳

结构体变量初始化失败

main()函数中的结构体变量q调用initqueue()初始化失败了,不知道为什么,我明明用了地址来调用初始化函数的

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int elemtype;
struct Node {
    struct Node* next;
    elemtype data;
};
typedef struct Node node;
node* getnode(elemtype x, node* nextptr) {
    node* n;
    n = (node*)malloc(sizeof(node));
    n->data = x;
    n->next = nextptr;
    return n;
}

typedef struct {
    node* front;
    node* rear;
    int length;

}swlqueue;

void  initqueue(swlqueue* qd) {
    qd = (swlqueue*)malloc(sizeof(swlqueue));
    qd->front = getnode(0, NULL);
    qd->rear = qd->front;
    qd->length = 0;
}

void insertafter(node* n, node* p) {
    p->next = n->next;
    n->next = p;
}



void in(swlqueue* q, elemtype x) {
    node* newnode;
    newnode = getnode(x, NULL);
    if (q->length == 0)
        insertafter(q->front, newnode);
    insertafter(q->rear, newnode);
    q->rear = newnode;
    q->length++;
}

elemtype out(swlqueue* q) {
    node* n;
    elemtype x;
    x = ((q->front)->next)->data;
    n = q->front;
    q->front = (q->front)->next;
    free(n);
    q->length--;
    return x;
}

int main() {
    swlqueue q;
    initqueue(&q);
    int array[20];
    memset(array, 0, sizeof(array));
    for (int i = 0; i <= 50; i++)
        in(&q, i);
    for (int j = 0; j < 20; j++)
        array[19 - j] = out(&q);
    for (int k = 0; k <= 19; k++)
        printf("%d\n", array[k]);
    system("pause");
    return 0;


}
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-05-12 09:53
    关注

    需要双指针

    问题解决的话,请点采纳

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef int elemtype;
    struct Node {
        struct Node* next;
        elemtype data;
    };
    typedef struct Node node;
    node* getnode(elemtype x, node* nextptr) {
        node* n;
        n = (node*)malloc(sizeof(node));
        n->data = x;
        n->next = nextptr;
        return n;
    }
    
    typedef struct {
        node* front;
        node* rear;
        int length;
    
    }swlqueue;
    
    void  initqueue(swlqueue** qd) {
        *qd = (swlqueue*)malloc(sizeof(swlqueue));
        (*qd)->front = getnode(0, NULL);
        (*qd)->rear = (*qd)->front;
        (*qd)->length = 0;
    }
    
    void insertafter(node* n, node* p) {
        p->next = n->next;
        n->next = p;
    }
    
    
    
    void in(swlqueue* q, elemtype x) {
        node* newnode;
        newnode = getnode(x, NULL);
        if (q->length == 0)
            insertafter(q->front, newnode);
        insertafter(q->rear, newnode);
        q->rear = newnode;
        q->length++;
    }
    
    elemtype out(swlqueue* q) {
        node* n;
        elemtype x;
        x = ((q->front)->next)->data;
        n = q->front;
        q->front = (q->front)->next;
        free(n);
        q->length--;
        return x;
    }
    
    int main() {
        swlqueue *q;
        initqueue(&q);
        int array[20];
        memset(array, 0, sizeof(array));
        for (int i = 0; i <= 50; i++)
            in(q, i);
        for (int j = 0; j < 20; j++)
            array[19 - j] = out(q);
        for (int k = 0; k <= 19; k++)
            printf("%d\n", array[k]);
        system("pause");
        return 0;
    
    
    }
    

    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题