#include<stdio.h>
#include<malloc.h>
#define null 0
typedef struct QNode
{
int data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QNode *front;
QNode *rear;
}QLinkList;
void InitQueue(QLinkList &Q)
{
Q.front=Q.rear=(QNode *)malloc(sizeof(QNode));
if(!Q.front)
printf("ERROR\n");
Q.front->next=null;
}
void EnQueue(QLinkList &Q,int e)
{
QNode *p;
p=(QNode *)malloc(sizeof(QNode));
if(!p)
printf("ERROR\n");
p->data=e;
p->next=null;
Q.rear->next=p;
Q.rear=p;
}
void DeQueue(QLinkList &Q,int &e)
{
if(Q.front==Q.rear)
printf("ENTER ERROR\n");
QNode *p=Q.front->next;
Q.front->next=p->next;
e=p->data;
free(p);
}
int GetQueue(QLinkList Q,int i)
{
int j=0;
if(i<1)
printf("ENTER ERROR\n");
QNode *p=Q.front;
while(p!=null&&j<i)
{
p=p->next;
j++;
}
if(p==null || j>i)
printf("ERROR\n");
return p->data;
}
CreateQueue(QLinkList &Q)
{
int i,n;
QNode *p;
printf("请输入队列元素个数\n");
scanf("%d",&n);
printf("请输入队列元素\n");
for(i=0;i<n;i++)
{
p=(QNode *)malloc(sizeof(QNode));
if(!p)
printf("ERROR\n");
scanf("%d",&(p->data));
p->next=null;
Q.rear->next=p;
Q.rear=p;
}
}
void TraverseQueue(QLinkList Q)
{
QNode *p=Q.front->next;
while(p!=null)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
int main()
{
int e,s;
QLinkList Q;
InitQueue(Q);
CreateQueue(Q);
TraverseQueue(Q);
EnQueue(Q,100);
TraverseQueue(Q);
DeQueue(Q,e);
TraverseQueue(Q);
printf("%d\n",e);
s=GetQueue(Q,3);
printf("%d\n",s);
return 0;
}
#include<malloc.h>
#define null 0
typedef struct QNode
{
int data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QNode *front;
QNode *rear;
}QLinkList;
void InitQueue(QLinkList &Q)
{
Q.front=Q.rear=(QNode *)malloc(sizeof(QNode));
if(!Q.front)
printf("ERROR\n");
Q.front->next=null;
}
void EnQueue(QLinkList &Q,int e)
{
QNode *p;
p=(QNode *)malloc(sizeof(QNode));
if(!p)
printf("ERROR\n");
p->data=e;
p->next=null;
Q.rear->next=p;
Q.rear=p;
}
void DeQueue(QLinkList &Q,int &e)
{
if(Q.front==Q.rear)
printf("ENTER ERROR\n");
QNode *p=Q.front->next;
Q.front->next=p->next;
e=p->data;
free(p);
}
int GetQueue(QLinkList Q,int i)
{
int j=0;
if(i<1)
printf("ENTER ERROR\n");
QNode *p=Q.front;
while(p!=null&&j<i)
{
p=p->next;
j++;
}
if(p==null || j>i)
printf("ERROR\n");
return p->data;
}
CreateQueue(QLinkList &Q)
{
int i,n;
QNode *p;
printf("请输入队列元素个数\n");
scanf("%d",&n);
printf("请输入队列元素\n");
for(i=0;i<n;i++)
{
p=(QNode *)malloc(sizeof(QNode));
if(!p)
printf("ERROR\n");
scanf("%d",&(p->data));
p->next=null;
Q.rear->next=p;
Q.rear=p;
}
}
void TraverseQueue(QLinkList Q)
{
QNode *p=Q.front->next;
while(p!=null)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
int main()
{
int e,s;
QLinkList Q;
InitQueue(Q);
CreateQueue(Q);
TraverseQueue(Q);
EnQueue(Q,100);
TraverseQueue(Q);
DeQueue(Q,e);
TraverseQueue(Q);
printf("%d\n",e);
s=GetQueue(Q,3);
printf("%d\n",s);
return 0;
}