#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<map>
#define ElemType int
#define MaxSize 100
using namespace std;
typedef struct LinkNode{
ElemType data;
struct LinkNode *next;
}LinkNode;
typedef struct{
LinkNode *front,*rear;
}LinkQueue;
//也就是说点的左边必须为实体,箭头左边必须为指针
void InitQueue(LinkQueue &Q){
Q.front=Q.rear=new LinkNode;
Q.front->next=NULL;
}
bool isEmpty(LinkQueue Q){
if(Q.front==Q.rear)
return true;
else
return false;
}
void EnQueue(LinkQueue &Q,ElemType x){
LinkNode *s=new LinkNode;
s->next=NULL;
s->data=x;
Q.rear->next=s;
Q.rear=s;
}
void DeQueue(LinkQueue &Q,ElemType &x){
if(Q.front==Q.rear){
return ;
}
LinkNode *s=Q.front->next;
Q.front->next=s->next;
x=s->data;
if(Q.rear==s)
Q.rear=Q.front;
free(s);
}
void PrintQueue(LinkQueue Q){
LinkNode *p,*q;
p=Q.front->next;
q=Q.rear;
while(1)
{
printf("%d\n",p->data);
p=p->next;
if(p==NULL) break;
}
}
int main()
{
ElemType x;
LinkQueue Q;
InitQueue(Q);
EnQueue(Q,1);
EnQueue(Q,2);
EnQueue(Q,3);
EnQueue(Q,4);
DeQueue(Q,x);
PrintQueue(Q);
}
数据结构(队列链式存储)
最新推荐文章于 2024-07-14 01:31:01 发布