c实现顺序队列基 ***

c实现顺序队列基 *** ,第1张

c实现顺序队列基 ***
#include
#include
#define maxsize 10
typedef struct Data
{
	int num;
	char name[20];
}Data;

typedef struct sqqueue
{
	struct Data data[maxsize];
	int front, rear;
}sqqueue;

void init(sqqueue* queue)//初始化
{
	queue->front = 0;
	queue->rear = 0;
	int i;
	for (i =0; i data[i].num = 0;
		strcpy_s(queue->data[i].name, 20, "无");
	}
}

void print(sqqueue* queue)//打印数据
{
	int i;
	printf("*存储的数据:n");
	for (i = queue->front; i <=(queue->rear-queue->front+maxsize)%maxsize; i++)
	{
		printf("%2d)num:%d  ",i+1, queue->data[i].num);
		printf("name:");
		puts(queue->data[i].name);
	}
	printf("n");
}

void insert(sqqueue* queue, Data new)//入列
{
	queue->data[queue->rear].num = new.num;
	strcpy_s(queue->data[queue->rear].name, 20, new.name);
	queue->rear = (queue->rear + 1) % maxsize;
}

Data delete(sqqueue* queue)//出列
{
	Data n;
	n.num = queue->data[queue->front].num;
	strcpy_s(n.name, 20, queue->data[queue->front].name);
	queue->data[queue->front].num = 0;
	strcpy_s(queue->data[queue->front].name, 20, "无");
	queue->front = (queue->front + 1) % maxsize;
	return n;
}

Data gethead(sqqueue* queue)//返回队头元素
{
	return queue->data[queue->front];
}
int main()
{
	sqqueue queue;
	init(&queue);
	int n;
	printf("*请输入存储的数据个数:");
	scanf_s("%d", &n);
	for (int i = 0; i < n; i++)
	{
		Data b;
		printf("%d)请输入第%d个num:",i+1,i+1);
		scanf_s("%d", &b.num);
		printf("      请输入name:");
		getchar();
		gets(b.name);
		insert(&queue, b);
	}
	printf("n");
	delete(&queue);
	print(&queue);
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5702466.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存