qq_53241762 2021-05-29 23:30 采纳率: 100%
浏览 34
已采纳

链队为什么插入不进去,显示异常

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct QNode {
	int data;                 //数据域
	struct QNode* next;       //指针域
}QNode;                       //队节点类型定义
typedef struct {
	QNode* front;                //对头指针
	QNode* rear;                //队尾指针
}LiQueue;                        //链队类型定义

int count = 0;
void initQueue(LiQueue* L);               //初始化一个链表
int isQueueEmpty(LiQueue* L);             //判断队列是否为空
void enQueue(LiQueue* L, int x);             //入队
int deQueue(LiQueue* L, int x);          //出队
//初始化链队算法
void
initQueue(LiQueue* L) {
	L = (LiQueue*)malloc(sizeof(LiQueue));
	if (NULL == L)
		printf("the malloc is error\n");
	L->front = L->rear = NULL;
	count = 0;
	printf("初始化成功!!!\n");
}
int
isQueueEmpty(LiQueue* L) {
	if (L->rear == NULL || L->front == NULL)
		return 0;
	else
		return 1;
}
//入队
void
enQueue(LiQueue* L, int x) {

	QNode* p;
	p = (QNode*)malloc(sizeof(QNode));
	p->data = x;
	p->next = NULL;
	printf("come!\n");  //第一次打印come
	if (L->rear == NULL)
	{
		L->front = L->rear = p;
		printf("come!\n");      //第二次打印come
	}
	else
	{
		L->rear->next = p;
		L->rear = p;
		printf("come!\n"); //第三次打印come
	}
	printf("入队成功!\n");
	count++;
	printf("%d", count);
}
//出队
int
deQueue(LiQueue* L, int x) {
	QNode* p;
	if (L->rear == NULL)
	{
		printf("wring!");
		return 0;
	}
	else
		p = L->front;
	if (L->front == L->rear)
		L->front == L->rear == NULL;      //只有有个节点时要特殊处理
	else
		L->front = L->front->next;
	x = p->data;
	free(p);
	count--;
	return 1;
}
int
main() {
	int doing, x, d;
	LiQueue List, * L;
	L = &List;
	doing = 1;
	while (doing) {
		printf("\n请选择您需要的功能:\n");
		scanf_s("%d", &doing);
		if (doing > 5 || doing < 0)
			printf("输入指令错误!\n");
		else
		{
			switch (doing)
			{
			case 1:
				initQueue(L);
				break;
			case 2:
				printf("请输入选择插入的人员:\n");
				scanf_s("%d", &x);
				printf("this\n");
				enQueue(L, x);     //插入队列算法         
				break;
			case 3:
				//	scanf_s("%d", &x);
				deQueue(L, x);
				break;
			case 4:
				if (L == NULL)
					printf("队列为空!\n");
				else
					printf("%d\n", count);
				break;

			}
		}
	}printf("后");
}

调用入队函数后 输入x值后就异常了

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-05-30 04:03
    关注

    初始化函数写的不对。两种改法,一是将L作为返回值,不要作为参数;二是将参数改为**L,否则函数无法修改传入的L指针地址

    void initQueue(LiQueue** L) {
    	*L = (LiQueue*)malloc(sizeof(LiQueue));
    	if (NULL == *L)
    		printf("the malloc is error\n");
    	*L->front = *L->rear = NULL;
    	count = 0;
    	printf("初始化成功!!!\n");
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算