資料結構---佇列---鏈式儲存
阿新 • • 發佈:2019-01-02
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 10
typedef int Status;
typedef int QElemType;
typedef struct QNode
{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front,rear;
}LinkQueue;
Status EnQueue(LinkQueue *Q,QElemType e)
{
QueuePtr s = (QueuePtr)malloc(sizeof(QNode));
if(!s)
exit(1);
s->data = e;
s->next = NULL;
Q->rear->next = s;
Q->rear = s;
return OK;
}
Status DeQueue(LinkQueue *Q,QElemType *e)
{
QueuePtr p;
if(Q->front == Q->rear)
return ERROR;
p=Q->front->next;
*e = p->data;
Q->front->next = p->next;
if(Q->rear == p)
Q->rear = Q->front;
free(p);
return OK;
}
int main()
{
LinkQueue *Q = (LinkQueue *)malloc(sizeof(LinkQueue));
(Q->front) = (QNode *)malloc(sizeof(QNode ));
Q->rear = Q->front;
EnQueue(Q,43);
EnQueue(Q,6);
EnQueue(Q,1);
EnQueue(Q,2);
EnQueue(Q,3);
QElemType *e = (QElemType *)malloc(sizeof(QElemType));
DeQueue(Q,e);
printf("%d\n",*e);
free(e);
QElemType *e1 = (QElemType *)malloc(sizeof(QElemType));
DeQueue(Q,e1);
printf("%d\n",*e1);
free(e1);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 10
typedef int Status;
typedef int QElemType;
typedef struct QNode
{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front,rear;
}LinkQueue;
Status EnQueue(LinkQueue *Q,QElemType e)
{
QueuePtr s = (QueuePtr)malloc(sizeof(QNode));
if(!s)
exit(1);
s->data = e;
s->next = NULL;
Q->rear->next = s;
Q->rear = s;
return OK;
}
Status DeQueue(LinkQueue *Q,QElemType *e)
{
QueuePtr p;
if(Q->front == Q->rear)
return ERROR;
p=Q->front->next;
*e = p->data;
Q->front->next = p->next;
if(Q->rear == p)
Q->rear = Q->front;
free(p);
return OK;
}
int main()
{
LinkQueue *Q = (LinkQueue *)malloc(sizeof(LinkQueue));
(Q->front) = (QNode *)malloc(sizeof(QNode ));
Q->rear = Q->front;
EnQueue(Q,43);
EnQueue(Q,6);
EnQueue(Q,1);
EnQueue(Q,2);
EnQueue(Q,3);
QElemType *e = (QElemType *)malloc(sizeof(QElemType));
DeQueue(Q,e);
printf("%d\n",*e);
free(e);
QElemType *e1 = (QElemType *)malloc(sizeof(QElemType));
DeQueue(Q,e1);
printf("%d\n",*e1);
free(e1);
return 0;
}