队列ADT-CPrimerPlusV6-20211102

开始学习队列ADT,这次弄个明晰的菜单出来。

1.问题(solved):使用switch选择执行,但读取添加、删除、全删除三个选项后就程序错误,也许是相关的三个函数有错误。

定义变量的时候错误,将变量定义成指针:

Student *temp_student=NULL;

Queue *man=NULL;

造成内存无法读取,换成:

Student temp_student={0};

Queue man={0};

程序已经可以运行。现在开始试着发现运行中的具体问题。

2.问题(solved):数据存储错误,最后一项会漏掉,进行删除等操作后甚至会报错。

显示函数:

void ShowQueue(Queue *pqueue)
{
	Queue pscan=*pqueue;
	fputs("*********DataBase*********\n",stdout);
	if(!pscan.nodes)
		fputs("No Data Found!\n",stdout);
	else
	{
		printf("(%d)Data Found.\n",pscan.nodes);
		while(pscan.front)//!!!原错为(pscan.front->next)
		{
			printf("NO.%d:Name:%s,Score:%hd.\n",
        pscan.nodes,pscan.front->student.name,pscan.front->student.score);
			pscan.front=pscan.front->next;		
		}
	}
}

将原错while(pscan.front->next)改为while(pscan.front);

这个问题莫名其妙自己解决了。

3.问题(solved):显示函数的次序数有问题。总是显示最后一项。

后来终于发现:结构定义时,queue结构中的nodes计数标签存储的就是最后一项,因为queue并不是链表项,node才是链表项,所以如果要对每一项进行编码,必须在node结构里添加编码计数标签。

typedef struct student
{
	char name[LEN_NAME];
	short score;
}Student;

typedef struct node
{
	Student student;
	struct node *next;
}Node;

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

4.问题(solved):全部删除函数的显示有错

short DeQueue(Student *pstudent,Queue *pqueue)
{
	if(IsQueueEmpty(pqueue))
		return 0;
	Node *ptemp=pqueue->front;
	*pstudent=ptemp->student;//错误原:pstudent=&(ptemp->student);
	pqueue->front=pqueue->front->next;
	free(ptemp);
	pqueue->nodes--;
	if(!(pqueue->nodes))
		pqueue->rear=NULL;
return 1;}

void EmptyQueue(Student *pstudent,Queue *pqueue)
{
	int i=pqueue->nodes;
	while(DeQueue(pstudent,pqueue))
		printf("Data->name:%s,score:%hd has cleaned.\n",
    pstudent->name,pstudent->score);
	printf("Over!%d data has cleaned.\n",i);
}

发现:错误原:pstudent=&(ptemp->student);这句代码有问题,改成:*pstudent=ptemp->student;就能顺利显示被删除数据的内容

个人理解:指向Student结构类型的指针pstudent指向ptemp->student的地址,但是ptemp内存被free(ptemp)清理,造成指向错误。

CPrimerPlusV6-17.4(队列ADT)-自改代码

/*声明接口*/

/*声明接口*/
#define _CRT_SECURE_NO_WARNINGS
#ifndef QUEUE_H
#define QUEUE_H
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

#define MAX_QUEUE 10
#define LEN_NAME 20
#define MAX_SCORE 100

typedef struct student
{
	char name[LEN_NAME];
	short score;
}Student;

typedef struct node
{
	Student student;
	struct node *next;
}Node;

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

void exit_normal(void);
void exit_abnormal(void);
char *s_gets(char *p,int n);
void InitializeQueue(Queue *pqueue);
short IsQueueFull(const Queue *pqueue);
short IsQueueEmpty(const Queue *pqueue);
void SearchQueue(const Student *pstudent,const Queue *pqueue);
short EnQueue(Student *pstudent,Queue *pqueue);
short DeQueue(Student *pstudent,Queue *pqueue);
void EmptyQueue(Student *pstudent,Queue *pqueue);
void ShowQueue(const Queue *pqueue);

#endif

/*定义接口*/

/*定义接口*/
#include"queue.h"

void exit_normal(void)
{
	fputs("ProgramOver!!!\n",stdout);
}

void exit_abnormal(void)
{
	fputs("ErrorHappened!Over!\n",stdout);
}

char *s_gets(char *p,int n)
{
	char *gets,*find;
	if(*(gets=fgets(p,n,stdin))!=10&&gets)
	{
		if(find=strchr(p,10))
			*find=0;
		else
			while(getchar()!=10);
	}
	else
		return 0;
return gets;}

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

short IsQueueFull(const Queue *pqueue)
{
	return((pqueue->nodes==MAX_QUEUE)?1:0);
}

short IsQueueEmpty(const Queue *pqueue)
{
	return(!(pqueue->nodes)?1:0);
}

void SearchQueue(const Student *pstudent,const Queue *pqueue)
{
	int i=0;
	Node *pscan=pqueue->front;
	while(pscan)
	{
		if(!strcmp(pstudent->name,pscan->student.name))
		{
			printf("count:%d,name:%s,score:%hd\n",i+1,pscan->student.name,pscan->student.score);
			i++;
		}
		pscan=pscan->next;
	}
	if(!i)
		fputs("No Data Found!\n",stdout);
}

short EnQueue(Student *pstudent,Queue *pqueue)
{
	Node *pnew;
	if(IsQueueFull(pqueue))
		return 0;
	if(!(pnew=(Node *)calloc(1,sizeof(Node))))
	{
		fputs("No Memory Available!",stderr);
		return 0;
	}
	pnew->student=*pstudent;
	pnew->next=NULL;
	if(IsQueueEmpty(pqueue))
		pqueue->front=pnew;
	else
		pqueue->rear->next=pnew;
	pqueue->rear=pnew;
	pqueue->nodes++;
return 1;}

short DeQueue(Student *pstudent,Queue *pqueue)
{
	if(IsQueueEmpty(pqueue))
		return 0;
	Node *ptemp=pqueue->front;
	*pstudent=ptemp->student;
	pqueue->front=pqueue->front->next;
	free(ptemp);
	pqueue->nodes--;
	if(!(pqueue->nodes))
		pqueue->rear=NULL;
return 1;}

void EmptyQueue(Student *pstudent,Queue *pqueue)
{
	int i=pqueue->nodes;
	while(DeQueue(pstudent,pqueue))
		printf("Data->name:%s,score:%hd has cleaned.\n",pstudent->name,pstudent->score);
	printf("Over!%d data has cleaned.\n",i);
}

void ShowQueue(const Queue *pqueue)
{
	Queue pscan=*pqueue;
	fputs("*********DataBase*********\n",stdout);
	if(!pscan.nodes)
		fputs("No Data Found!\n",stdout);
	else
	{
		printf("(%d)Data Found.\n",pscan.nodes);
		for(int i=1;pscan.front;i++)
		{
			printf("NO.%d:Name:%s,Score:%hd.\n",i,pscan.front->student.name,pscan.front->student.score);
			pscan.front=pscan.front->next;
		}
	}
}

/*使用接口*/

/*使用接口*/
#include"queue.h"

int showmenu(void);
short executemenu(int choice,Student *pstudent,Queue *pqueue);

int main()
{
	Student temp_student={0};
	Queue man={0};

	atexit(exit_normal);
	InitializeQueue(&man);

	while(executemenu(showmenu(),&temp_student,&man));
    EmptyQueue(&temp_student,&man);
return 0;}
//
int showmenu(void)
{
	const char *pch=0;
	int choice=0;
	const char *menu="EFDCSQ";
	fputs("***************Menu***************\n",stdout);
	fputs("E)AddData        F)Search Data\n",stdout);
	fputs("D)Delete a Data  C)Delete all Data\n",stdout);
	fputs("S)Show all Data  Q)QuitProgram\n",stdout);
	for(fputs("Your Choice:",stdout),choice=fgetc(stdin);
		!strchr(menu,toupper(choice));
		fputs("Your Choice:",stdout),choice=fgetc(stdin))
	{
		fputs("Error!Again!\n",stdout);
		while(getchar()!=10);
	}
	while(getchar()!=10);
return toupper(choice);}

short executemenu(int choice,Student *pstudent,Queue *pqueue)
{
	printf("You choose '%c'.\n",choice);
	switch(choice)
	{
	case 'E':
		if(IsQueueFull(pqueue))
		{
			fputs("The Queue is FULL!\n",stderr);
			break;
		}
		for(printf("NO.%d name:",pqueue->nodes+1);s_gets(pstudent->name,LEN_NAME);printf("NO.%d name:",pqueue->nodes+1))
		{
			for(printf("NO.%d score:",pqueue->nodes+1),choice=scanf("%hd",&pstudent->score);
				choice!=1||pstudent->score<0||pstudent->score>MAX_SCORE;
				printf("NO.%d score:",pqueue->nodes+1),choice=scanf("%hd",&pstudent->score))
			{
				fputs("Error!Again!\n",stderr);
				while(getchar()!=10);
			}
			while(getchar()!=10);
			if(!EnQueue(pstudent,pqueue))
			{
				fputs("Error!Can't Add Data!\n",stderr);
				break;
			}
			if(IsQueueFull(pqueue))
			{
				fputs("The Queue is FULL!\n",stderr);
				break;
			}
		}
		break;
	case 'F':
		if(IsQueueEmpty(pqueue))
		{
			fputs("No Data Found!\n",stderr);
			break;
		}
		for(fputs("Input name(NothingStop):",stdout);s_gets(pstudent->name,LEN_NAME);fputs("Input name(NothingStop):",stdout))
		{
			SearchQueue(pstudent,pqueue);
		}
		break;
	case 'D':
		if(!DeQueue(pstudent,pqueue))
			fputs("No Data Found!\n",stderr);
		else
			fprintf(stdout,"The first Data has been deleted.\n");
		break;
	case 'C':
		EmptyQueue(pstudent,pqueue);
		break;
	case 'S':
		ShowQueue(pqueue);
		break;
	case 'Q':
		return 0;
	default:break;
	}
return 1;}

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fleet1126

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值