栈、循环队列的基本操作

队列的基本操作

queue.h
#include <malloc.h>
#include <string.h>

#define TRUE	1
#define FALSE	0

#define MAX		100

typedef int status;

typedef int ElemType;
typedef struct _queue
{
	ElemType *arr;
	int rear;
	int length;
}queue;

int initqueue(queue *s);
int enqueue(queue *s, ElemType x);
int dequeue(queue *s, ElemType *x);
int destroyqueue(queue *s);
status IsEmptyQueue(queue *s);

#endif

queue.c
#include "queue.h"

status initqueue(queue *s)
{
	s->arr = (ElemType *)malloc(sizeof(ElemType) * MAX);
	s->rear = 0;
	s->length = 0;
	memset(s->arr, 0, sizeof(ElemType) * MAX);
	return 1;
}

status enqueue(queue *s, ElemType x)
{
	int head;
	head = ((s->rear + MAX) - s->length) % MAX;
	if((s->rear + 1 % MAX) == head)
	{
		printf("队满, head = %d, rear = %d, length = %d\n", head, s->rear, s->length);
		return 0;
	}
	s->arr[s->rear] = x;
	s->rear = (s->rear + 1) % MAX;
	s->length++;
	return 1;
}

status dequeue(queue *s, ElemType *x)
{
	int head;
	head = ((s->rear + MAX) - s->length) % MAX;

	if(head == s->rear)
	{
		printf("队空, head = %d, rear = %d, length = %d\n", head, s->rear, s->length);
		return 0;
	}
	*x = s->arr[head];
	s->length--;
	return 1;
}

status IsEmptyQueue(queue *s)
{
	int head;
	head = ((s->rear + MAX) - s->length) % MAX;

	if(head == s->rear)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

status destroyqueue(queue *s)
{
	free(s->arr);
	return 1;
}

栈的基本操作

stack.h

#ifndef __STACK_H__
#define __STACK_H__

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "BiTree.h"

#define STACKINCREMENT 10

typedef struct BiTNode *elemtype;
typedef int status;
typedef struct _stack
{
	elemtype *base;
	elemtype *top;
	int stacksize;
}stack;

#endif
stack.c
#include "stack.h"

status initStack(stack *s)
{
	s->base = (elemtype *)malloc(sizeof(elemtype) * MAX);
	memset(s->base, 0, sizeof(elemtype) * MAX);
	s->top = s->base;
	s->stacksize = MAX;
	return TRUE;
}

status destroyStack(stack *s)
{
	free(s->base);
	return TRUE;
}

status clearStack(stack *s)
{
	s->top = s->base;
	return TRUE;
}

status StackEmpty(stack *s)
{
	return (s->top == s->base) ? TRUE : FALSE;
}

status Push(stack *s, elemtype x)
{
	if((s->top - s->base) >= s->stacksize)
	{
		s->base = (elemtype *)realloc(s->base, s->stacksize + STACKINCREMENT);
		s->top = s->base + s->stacksize;
		s->stacksize += STACKINCREMENT;
		return FALSE;
	}

	*s->top++ = x;
	return TRUE;
}

status Pop(stack *s, elemtype *x)
{
	if(s->top == s->base)
	{
		printf("栈空\n");
		return FALSE;
	}
	*x = *--s->top;
	return TRUE;
}

status GetTop(stack *s, elemtype *x)
{
	if(s->top == s->base)
	{
		printf("栈空\n");
		return FALSE;
	}
	*x = *(s->top - 1);
	return TRUE;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值