队列的插入、删除操作

 
#include<iostream>
using namespace std;
typedef int ElemType;
typedef struct Node
{
 ElemType data;
 struct Node *next;
}QNode,*QueuePtr;
typedef struct
{
 QueuePtr front;
 QueuePtr tail;
}LinkQueue;
void InitQueue(LinkQueue *q)
{
 q->front = q->tail = NULL;
}
int IsEmpty(LinkQueue *q)
{
 return q->front == NULL;
}
void EnQueue(LinkQueue *q,ElemType x)
{
 QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
 p->data = x;
 p->next = NULL;
 if(IsEmpty(q))
  q->front = p;
 else
  q->tail->next = p;
 q->tail = p;
}
void DeQueue(LinkQueue *q)
{
 QueuePtr p;
 if(IsEmpty(p))
 {
  cout<<"队列为空,溢出!"<<endl;
  return;
 }
 p = q->front;
 if(q->front == q->tail)
 {
  q->front = NULL;
  q->tail = NULL;
 }
 else
 {
  q->front = q->front->next;
  free(p);
 }
}
void PrintQueue(LinkQueue *q)
{
 if(q->front == NULL)
 {
  cout<<"队列为空!"<<endl;
 }else
 {
  QueuePtr p = q->front;
  while(p != q->tail)
  {
   cout<<p->data<<endl;
   p = p->next;
  }
  cout<<p->data<<endl;//最后一个数据
 }
}

int main()
{
 LinkQueue lq;
 LinkQueue *l = &lq;
 InitQueue(l);
 EnQueue(l,3);
 PrintQueue(l);
 DeQueue(l);
 PrintQueue(l);
 return 1;
}

你可能感兴趣的:(struct,null)