C语言实现顺序队列
程序员文章站
2022-07-15 09:14:28
...
练习数据结构顺便提升一波C语言水平
主要区别在于初始化的时候,测试数据和测试结果一致,C语言指针真的是博大精深(照这样下去我可能要疯了)
第一种
#include<stdio.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20
typedef int Status;
typedef int ElemType;
typedef struct QNode
{
ElemType data[MAXSIZE];
int Front;
int Rear;
} Queue;
Queue InitQueue(Queue *Q)
{
Q->Front = 0;
Q->Rear = 0;
printf("Queue has been init###\n");
}
void ClearQueue(Queue *Q)
{
Q->Front = 0;
Q->Rear = 0;
printf("Queue has been empty!!!\n");
}
Status EmptyQueue(Queue Q)
{
if(Q.Front == Q.Rear)
{
printf("Queue is empty$$$\n");
return TRUE;
}
else
{
printf("Queue is not empty$$$\n");
return FALSE;
}
}
int LengthQueue(Queue Q)
{
return (Q.Rear-Q.Front+MAXSIZE)%MAXSIZE;
}
void AddQueue(Queue *Q,ElemType e)
{
if((Q->Rear+1)%MAXSIZE == Q->Front)
{
printf("Queue is full~~~\n");
return ;
}
Q->data[Q->Rear] = e;
Q->Rear = (Q->Rear+1)%MAXSIZE;
}
void DeleteQueue(Queue *Q,ElemType *e)
{
if(Q->Front == Q->Rear)
{
printf("Queue is empty~~~\n");
return ;
}
*e = Q->data[Q->Front];
Q->Front = (Q->Front+1)%MAXSIZE;
printf("Delete e = %d~~~\n",*e);
}
void QueueTraverse(Queue Q)
{
if(EmptyQueue(Q))
return ;
int i=Q.Front;
while((i+Q.Front)!=Q.Rear)
{
printf("%d ",Q.data[i]);
i = (i+1)%MAXSIZE;
}
printf("\n");
return ;
}
int main()
{
Queue Q = InitQueue(&Q);
EmptyQueue(Q);
int e;
for(int i=1; i<=20; i++)
{
printf("Adding~~~\n");
AddQueue(&Q,i);
}
int QLen = LengthQueue(Q);
printf("Queue length is %d\n",QLen);
printf("QueueTraverse:");
QueueTraverse(Q);
printf("\n\n");
for(int i=1; i<=20; i++)
{
DeleteQueue(&Q,&e);
}
QLen = LengthQueue(Q);
printf("Queue length is %d\n\n",QLen);
for(int i=1; i<=20; i++) //为了验证ClearQueue,再次添加
{
AddQueue(&Q,i);
}
QLen = LengthQueue(Q);
printf("Queue length is %d\n\n",QLen);
ClearQueue(&Q);
QLen = LengthQueue(Q);
printf("Queue length is %d\n",QLen);
EmptyQueue(Q);
return 0;
}
第二种
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20
typedef int Status;
typedef int ElemType;
typedef struct QNode
{
ElemType data[MAXSIZE];
int Front;
int Rear;
}QNode, *Queue;
Queue InitQueue()
{
Queue Q;
Q = (Queue)malloc(sizeof(QNode));
if(!Q)
exit(1);
Q->Front = 0;
Q->Rear = 0;
printf("Queue has been init###\n");
return Q;
}
void ClearQueue(Queue Q)
{
Q->Front = 0;
Q->Rear = 0;
printf("Queue has been empty!!!\n");
}
Status EmptyQueue(Queue Q)
{
if(Q->Front == Q->Rear)
{
printf("Queue is empty$$$\n");
return TRUE;
}
else
{
printf("Queue is not empty$$$\n");
return FALSE;
}
}
int LengthQueue(Queue Q)
{
return (Q->Rear-Q->Front+MAXSIZE)%MAXSIZE;
}
void AddQueue(Queue *Q,ElemType e)
{
if(((*Q)->Rear+1)%MAXSIZE == (*Q)->Front)
{
printf("Queue is full~~~\n");
return ;
}
(*Q)->data[(*Q)->Rear] = e;
(*Q)->Rear = ((*Q)->Rear+1)%MAXSIZE;
}
void DeleteQueue(Queue *Q,ElemType *e)
{
if((*Q)->Front == (*Q)->Rear)
{
printf("Queue is empty~~~\n");
return ;
}
*e = (*Q)->data[(*Q)->Front];
(*Q)->Front = ((*Q)->Front+1)%MAXSIZE;
printf("Delete e = %d~~~\n",*e);
}
void QueueTraverse(Queue Q)
{
if(EmptyQueue(Q))
return ;
int i=Q->Front;
while((i+Q->Front)!=Q->Rear)
{
printf("%d ",Q->data[i]);
i = (i+1)%MAXSIZE;
}
printf("\n");
return ;
}
int main()
{
Queue Q = InitQueue();
EmptyQueue(Q);
int e;
for(int i=1; i<=20; i++)
{
printf("Adding~~~\n");
AddQueue(&Q,i);
}
int QLen = LengthQueue(Q);
printf("Queue length is %d\n",QLen);
printf("QueueTraverse:");
QueueTraverse(Q);
printf("\n\n");
for(int i=1; i<=20; i++)
{
DeleteQueue(&Q,&e);
}
QLen = LengthQueue(Q);
printf("Queue length is %d\n\n",QLen);
for(int i=1; i<=20; i++) //为了验证ClearQueue,再次添加
{
AddQueue(&Q,i);
}
QLen = LengthQueue(Q);
printf("Queue length is %d\n\n",QLen);
ClearQueue(Q);
QLen = LengthQueue(Q);
printf("Queue length is %d\n",QLen);
EmptyQueue(Q);
return 0;
}