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;
}
#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;
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。