能不能这么初始化一个队列
能不能这样初始化一个队列?
如题:
问题我标在程序中了。
另外如果这一步
变成下面这样
还可以用pQueue = NULL; 这种形式吗?
------解决方案--------------------
pQueue->front = pQueue->tail = NULL;//一般用这种方法。
// pQueue = NULL; //这样行不行?这样是不是也意味着pQueue->front = pQueue->tail = NULL
这样显然不行,因为
1.你在内部改的参数值,是不会传到外面的。外面的
Queue one;
InitializeQueue(&one);
后,one的地址不会是NULL
2.你即使改成引用,也是错的,因为one已经是一个对象,你把它地址设为null,是非法的
------解决方案--------------------
pQueue是地址的形参,你可以改变地址指向的内容,但是改变地址本身是不会对外部有影响的
而且,就像你做了如下操作,你觉得给a初始化了吗?
个人觉得,你调用一下这个函数
还不如直接写个构造函数呢
如题:
问题我标在程序中了。
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#define SIZE 5
typedef int Item;
typedef struct node
{
Item items[SIZE];
struct stack *next;
}Node;
typedef struct queue
{
Node *front;
Node *tail;
}Queue;
void InitializeQueue(Queue *pQueue);
int main(void)
{
Queue one;
InitializeQueue(&one);
return 0;
}
void InitializeQueue(Queue *pQueue)
{
pQueue->front = pQueue->tail = NULL;//一般用这种方法。
// pQueue = NULL; //这样行不行?这样是不是也意味着pQueue->front = pQueue->tail = NULL;?
}
另外如果这一步
typedef struct queue;
{
Node *front;
Node *tail;
}Queue
变成下面这样
typedef struct queue
{
Node *front;
Node *tail;
int number;
}Queue;
还可以用pQueue = NULL; 这种形式吗?
------解决方案--------------------
pQueue->front = pQueue->tail = NULL;//一般用这种方法。
// pQueue = NULL; //这样行不行?这样是不是也意味着pQueue->front = pQueue->tail = NULL
这样显然不行,因为
1.你在内部改的参数值,是不会传到外面的。外面的
Queue one;
InitializeQueue(&one);
后,one的地址不会是NULL
2.你即使改成引用,也是错的,因为one已经是一个对象,你把它地址设为null,是非法的
------解决方案--------------------
// pQueue = NULL; //这样行不行?
pQueue是地址的形参,你可以改变地址指向的内容,但是改变地址本身是不会对外部有影响的
而且,就像你做了如下操作,你觉得给a初始化了吗?
int a;
int * pa = &a;
pa = NULL;
个人觉得,你调用一下这个函数
void InitializeQueue(Queue *pQueue);
还不如直接写个构造函数呢