一17 2021-04-17 18:26 采纳率: 100%
浏览 668
已采纳

程序运行中,提示指针为nullptr,写入访问权限冲突

问题截图,明明已经通过InitializeQueue初始化了pq—>rear,不知道为什么。

 

完整代码段

#ifndef _QUEUE_H_
#define _QUEUE_H_
#include<stdbool.h>

typedef int Item;

#define MAXQUEUE 10

typedef struct node
{
    Item item;
    struct node* next;
} Node;

typedef struct queue
{
    Node* front;
    Node* rear;
    int items;
} Queue;

/*操作            初始化队列*/
/*前置条件:      pq指向一个队列*/
/*后置条件:      队列被初始化为空*/
void InitializeQueue(Queue* pq);

/*操作            检验队列是否已满*/
/*前置条件:      pq指向之前已被初始化的队列*/
/*后置条件:      队列已满返回true,否则返回false*/
bool QueueIsFull(const Queue* pq);

/*操作            检验队列是否为空*/
/*前提条件:      pq指向之前被初始化的队列*/
/*后置条件:      队列为空返回true,否则返回false*/
bool QueueIsEmpty(const Queue* pq);

/*操作:          确定队列中的项数*/
/*前提条件:      pq指向之前被初始化的队列*/
/*后置条件:      返回队列中的项数*/
int QueueItemCount(const Queue* pq);

/*操作:          在队列末尾添加项*/
/*前提条件:      pq指向之前被初始化的队列*/
/*                item是要被添加在队列末尾的项*/
/*后置条件:      若队列不为空,item会被添加在队列末尾*/
/*                函数返回true,否则返回false*/
bool EnQueue(Item item, Queue* pq);

/*操作:          从队列开头删除项*/
/*前提条件:      pq指向之前被初始化的队列*/
/*后置条件:      如果队列不为空,队列首段的item将被拷贝到*pitem */
/*                并被删除,返回true;                           */
/*                如果该操作使队列为空,则重置队列为空           */
/*                如果队列在操作前已经为空,则返回false          */
bool DeQueue(Item* pitem, Queue* pq);

/*操作:          清空队列*/
/*前提条件:      pq指向之前被初始化的队列*/
/*后置条件:      队列被清空*/
void EmptyTheQueue(Queue* pq);

#endif





#include <stdio.h>
#include<stdlib.h>
#include"queue.h"

static void CopyToNode(Item item, Node* pn);
static void CopyToItem(Node* pn, Item* pi);

void InitializeQueue(Queue* pq)
{
	pq->front = pq->rear= NULL;
	pq->front = 0;
}

bool QueueIsFull(const Queue* pq)
{
	return pq->items == MAXQUEUE;
}

bool QueueIsEmpty(const Queue* pq)
{
	return pq->items == 0;
}

int QueueItemCount(const Queue* pq)
{
	return pq->items;
}

bool EnQueue(Item item, Queue* pq)
{
	Node* pnew=NULL;

	if (QueueIsFull(pq))
		return false;
	pnew = (Node*)malloc(sizeof(Node));
	if (pnew == NULL)
	{
		fprintf(stderr, "Unable to allocate memory!\n");
		exit(1);
	}
	CopyToNode(item,pnew);
	pnew->next = NULL;
	if (QueueIsEmpty(pq))
		pq->front = pnew;
	else
		pq->rear->next = pnew;
	pq->rear = pnew;
	pq->items++;

	return true;
}

bool DeQueue(Item* pitem, Queue* pq)
{
	Node* pt;

	if (QueueIsEmpty(pq))
		return false;
	CopyToItem(pq->front, pitem);
	pt = pq->front;
	pq->front = pq->front->next;
	free(pt);
	pq->items--;
	if (pq->items == 0)
		pq->rear = NULL;

	return true;
}

void EmptyTheQueue(Queue* pq)
{
	Item dummy;
	while (!QueueIsEmpty(pq))
		DeQueue(&dummy, pq);
}

static void CopyToNode(Item item, Node* pn)
{
	pn->item = item;
}

static void CopyToItem(Node* pn, Item* pi)
{
	*pi = pn->item;
}






#include<stdio.h>
#include"queue.h"

int main(void)
{
	Queue line;
	Item temp;
	char ch;

	InitializeQueue(&line);
	puts("Testing the Queue interface.Type a to add a value,");
	puts("type d to delete a value,and type q to quit.");
	while ((ch = getchar()) != 'q')
	{
		if (ch != 'a' && ch != 'd')
			continue;
		if (ch == 'a')
		{
			printf("Integer to add:");
			scanf_s("%d", &temp);
			if (!QueueIsFull(&line))
			{
				printf("Putting %d into queue\n", temp);
				EnQueue(temp, &line);
			}
			else
				puts("Nothing to delete!");
		}
		else
		{
			if (QueueIsEmpty(&line))
				puts("Nothing to delete");
			else
			{
				DeQueue(&temp, &line);
				printf("Removing %d from queue\n", temp);
			}
		}
		printf("%d items in queue\n", QueueItemCount(&line));
		puts("Type a to add,d to delete,q to quit");
	}

	EmptyTheQueue(&line);
	puts("Bye!");

	return 0;
}
  • 写回答

4条回答 默认 最新

  • soar3033 2021-04-18 07:53
    关注

    你的初始化函数把pq->rear初始化成NULL了,也就是说pq->rear是空的,既然是空的,也就不存在pq->rear->next。初始化函数改成

    void InitializeQueue(Queue* pq)
    {
    	pq->front =  NULL;
    	pq->front = 0;
        Node* rear = (Node*)malloc(sizeof(Node));
        pq->rear=rear;
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了