第十周 技术博客发表 数据结构

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "stdio.h"
#include "stdlib.h"

typedef int DataType;
const int MAX=5;

typedef struct Queue
{
 int length;
 DataType arr[MAX];
 int input,output;
}Queue;

void getOut(Queue* myQ);
void RemQ(Queue* myQ);
void InsQ(Queue* myQ, DataType data);
int isEmpty(Queue* myQ);
int isFull(Queue* myQ);


Queue* InitQ(Queue* myQ)
{
 myQ=(Queue *)malloc(sizeof(Queue));
 myQ->length=0;
 myQ->input=0;
 myQ->output=0;
 return myQ;
}

int isFull(Queue* myQ)
{
 if(myQ->length!=0 && myQ->input==myQ->output)
  return 1;
 else
  return 0;
}


int isEmpty(Queue* myQ)
{
 if(myQ->input==myQ->output&&myQ->length==0)
  return 1;
 else
  return 0;
}

void InsQ(Queue* myQ, DataType data)
{
 
 if(isFull(myQ))
 {
  printf("is full ");
  return;
 }
 
 myQ->arr[myQ->input]=data;
 myQ->input=(myQ->input+1)%MAX;
 myQ->length++;
// printf("   length is %d    ",myQ->length);
}

void RemQ(Queue* myQ)
{
 if(isEmpty(myQ))
 {
  printf("is empty ");
  return;
 }
 myQ->output=(myQ->output+1)%MAX;
 myQ->length--;

}

void getOut(Queue* myQ)
{
 if(isEmpty(myQ))
 {
  printf("is empty ");
  return;
 }
 printf("%d ", myQ->arr[myQ->output]);
}


int main(int argc, char const *argv[])
{
 /* code */
 Queue* myQ;
 myQ=InitQ(myQ);
 InsQ(myQ,53);
 getOut(myQ);
 InsQ(myQ,3);
 getOut(myQ);
 InsQ(myQ,66);
 getOut(myQ);
 InsQ(myQ,98);
 getOut(myQ);
 InsQ(myQ,46);
 getOut(myQ);
 InsQ(myQ,0);
 RemQ(myQ);
 getOut(myQ);
 RemQ(myQ);
 getOut(myQ);
 RemQ(myQ);
 getOut(myQ);

 return 0;
}

原文地址:https://www.cnblogs.com/youu/p/5444370.html