#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std ;
#define MAXSIZE 100
#define OK true
#define ERROR false
#define LEN 5
#define OVERFLOW 0
typedef bool Status ;
typedef int QElemType ;
typedef struct QNode
{
QElemType data ;
QNode * next ;
}QNode,*pQNode;
typedef struct
{
pQNode front , rear ;
}LinkQueue;
void initQueue(LinkQueue* Q)
{
Q->front = new QNode;//front是头结点;rear仅仅是一个指针
Q->rear = Q->front ;//尾指针指向头结点
}
Status enQueue(LinkQueue* Q , QElemType e )
{
pQNode s = new QNode ;
if(!s)
exit(OVERFLOW);
s->data = e ; // 初始化s节点
s->next = NULL ;
if(Q->front == Q->rear)//判断是否为第一个插入节点
Q->front->next = s ; //将头指针的next指向s
Q->rear->next = s ;//移动rear节点
Q->rear = s ;
return OK ;
}
Status deQueue(LinkQueue* Q , QElemType* e )
{
if(Q->front == Q->rear)
return ERROR ;
pQNode p = Q->front->next ;//p为删除节点
*e = p->data ;
if(Q->rear == p ) //p为rear表示删除队列中最后一个节点
Q->rear = Q->front ;//将rear指向front头结点
else
Q->front->next = p->next ; //否则指向删除节点的next节点
delete p ;
return OK ;
}
void printQ(LinkQueue Q)
{
cout<<"打印:"<<endl;
pQNode pfront = Q.front->next;
while(pfront)
{
cout<<pfront->data<<" " ;
pfront = pfront->next ;
}
cout<<endl;
}
int main()
{
LinkQueue Q ;
QElemType e ;
initQueue(&Q);
srand((unsigned)time(0));
for(int i = 0 ; i < LEN ;i ++)
{
e = rand() % 10 + 1 ;
enQueue(&Q,e);
}
printQ(Q);
cout<<"删除一个元素";
deQueue(&Q,&e);
cout<<" "<<e<<" 后,";
printQ(Q);
system("pause");
return 0 ;
}
转载于:https://www.cnblogs.com/dot-dot-123/archive/2013/05/29/3106227.html