// LinkSQueue.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//c链式队列的实现
//简单的基于单链表操作。多了两个指针。一个队头front,一个队尾指针rear
typedef int QElemType;
typedef struct node{
QElemType data;
struct node *link;
}LinkNode;
typedef struct{
struct node *front;
struct node *rear;
}LinkQueue;
//初始化
bool InitQueue( LinkQueue & Q )
{
Q.front = Q.rear = new LinkNode();
if( Q.front == NULL ) return false;
}
//清空队列
void ClearQueue( LinkQueue & Q )
{
LinkNode *p;
while(Q.front->link != NULL )
{
p = Q.front->link;
Q.front->link = p->link;
delete p;
}
Q.rear = Q.front;
}
bool IsEmpty( LinkQueue & Q )
{
return ( Q.front->link == NULL )?true:false;
}
//入队
bool EnQueue( LinkQueue & Q,QElemType &x )
{
Q.rear->link = new LinkNode;
if( Q.rear->link == NULL ) return false;
Q.rear = Q.rear->link;
Q.rear->data = x;
Q.rear->link = NULL;
printf("入队=%d\n",x);
return false;
}
//出队
bool DeQueue( LinkQueue &Q, QElemType &x)
{
if(IsEmpty(Q))return false;
LinkNode *p = Q.front->link;
x = p->data;
Q.front->link = p->link;
delete p;
printf("出队=%d\n",x);
return true;
}
//得到头
bool GetFront( LinkQueue &Q, QElemType &x )
{
if(IsEmpty(Q))return false;
LinkNode *p = Q.front->link;
x = p->data;
return true;
}
//得到大小
int QueueSize( LinkQueue &Q )
{
LinkNode *p = Q.front->link;
int k = 0;
while( p !=NULL )
{
p = p->link;
k++;
}
return k;
}
int _tmain(int argc, _TCHAR* argv[])
{
LinkQueue q;
InitQueue(q);
for( int i = 0;i<20;i++)
{
EnQueue(q,i);
}
for( int i = 0;i<20;i++)
{
DeQueue(q,i);
}
ClearQueue(q);
getchar();
return 0;
}
C链式队列的实现
最新推荐文章于 2023-05-17 13:50:58 发布