顺序栈的简单实现


queue.h头文件

#ifndef QUEUE_H
#define QUEUE_H 1

typedef int INT32;
typedef unsigned int UINT32;
typedef unsigned char UCHAR8;
typedef char CHAR8;
typedef long long int LONG64;
typedef unsigned long long int ULONG64;
typedef double DOUBLE64;
typedef float FLOAT32;


struct Queue
{
	INT32 *arr;
	INT32 tail;
	INT32 head;
	INT32 max;

};
typedef struct Queue *Pqueue;

Pqueue Create(INT32 n);
void Pop(Pqueue q);
INT32 Front(Pqueue q);
void Push(Pqueue q,INT32 data);
INT32 Empty(Pqueue q);
INT32 Full(Pqueue q);


#endif

queue.c源文件

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

Pqueue Create(INT32 n)
{
	Pqueue q = (Pqueue)malloc(sizeof(struct Queue));
	if(q == NULL)
		return NULL;
	else
	{
		q->arr = (INT32 *)malloc(sizeof(INT32)*n);
		q->head = 0;
		q->tail = 0;
		q->max = n;
	}

	return q;
}

INT32 Empty(Pqueue q)
{
	if(q->tail == q->head)
		return 1;
	else
		return 0;
}

INT32 Full(Pqueue q)
{
	if((q->tail+1)%(q->max) == q->head)
		return 1;
	else
		return 0;
}

void Push(Pqueue q,INT32 data)
{
	if(Full(q) == 0)
		q->arr[(q->tail++)%q->max] = data;
	else
		printf("队列已满\n");

}

void Pop(Pqueue q)
{
	if(Empty(q) == 0)
		q->head = (q->head+1)%(q->max);
	else
		printf("队列为空\n");
}

INT32 Front(Pqueue q)
{
	if(Empty(q) == 0)
		return q->arr[q->head];
	else
	{
		printf("队列为空\n");
		exit(0);
	}

}


main测试文件


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

#include"queue.h"

int main()
{
	INT32 n,temp;
	Pqueue q;
	printf("输入要创建的队列大小\n");
	scanf("%d",&n);
	q = Create(n);
	printf("输入队列元素:\n");
	while(scanf("%d",&temp) != EOF)
	{
			Push(q,temp);
	}
	while(Empty(q) != 1)
	{
		printf("%d ",Front(q));
		Pop(q);
	
	}

	printf("\n");
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值