栈与队列

      栈与队列,,,其实把链表,顺序表掌握了,就很easy。就是去掉了顺序表,链表的一些操作而已。。。。。。。。。。。。。。。。

下面是我写的循环队列(顺序)。在此与大家分享吐舌头 

 

        今天看到一句话感觉不错(“write   once,  run   everywhere”)     

#ifndef  _MY_QUEUE_H_
#define  _MY_QUEUE_H_

 /*顺序队列之循环队列 */
#define MAX_SIZE  10

typedef int ElemType;

typedef  struct _queue
{
 ElemType a[MAX_SIZE];
 int head;
 int tail;
}queue;

//基本算法:
//初始化空队列:
void init_queue(queue *p);

//销毁队列:
void destory_queue(queue *p);

//判断是否为空队列:
bool is_empty(queue *p);

//获得队头元素:
void get_head(queue *p,ElemType *e);

//入队:
void push_queue(queue *p,ElemType e);

//出队:
void pop_queue(queue *p,ElemType *e);
#endif



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

//初始化空队列:
void init_queue(queue *p)
{
 if(p == NULL)
 {
  printf("the queue  is not exist\n");
  exit(1);
 }
 else
 {
  p->head = 0;
  p->tail = 0;
 }
}

//判断是否为空队列:
bool is_empty(queue *p)
{
 return p->head == p->tail;
}

//获得队头元素:
void get_head(queue *p,ElemType *e);

//入队:
void push_queue(queue *p,ElemType e)
{
 if(p->tail+1 == p->head )
 {
  printf("The queue is full,Please wait\n");
  exit(0);
 }
 else
 {
  p->a[p->tail] = e;
     p->tail = (p->tail+1)%MAX_SIZE;//
 }
}

//出队:
void pop_queue(queue *p,ElemType *e)
{
 if(!is_empty)
 {
  printf("error\n");
  exit(1);
 }
 else
 {
  *e = p->a[p->head];
  p->head = p->head +1 % MAX_SIZE ;//
 }
}

//销毁队列:
void destory_queue(queue *p)
{
 p->tail = 0;
 p->head = 0;
}



#include"my_queue.h"
#include<stdio.h>

int main()
{
 queue t;
 int x;
 init_queue(&t);
 for(int i = 1;i<=11;i++)
 {
   push_queue(&t,i);
 }
 for(int i = 1;i<=10;i++)
 {
   pop_queue(&t,&x);
   printf("%d\n",x);
 }
 destory_queue(&t);
 return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值