#include<iostream>
using namespace std;
typedef struct Node
{
int data;
Node* next;
};
typedef struct Queue
{
Node* front;
Node* rear;
};
Queue* CreateQueue()
{
Queue* p = (Queue*)malloc(sizeof(Queue));
p->front = NULL;
p->rear = NULL;
return p;
}
void printQueue(Queue *q)
{
if(q->front == NULL)
return;
Node *temp = q->front;
while(temp != NULL)
{
printf("%d \n",temp->data);
temp = temp->next;
}
}
void enQueue(Queue* &q, int elem)
{
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = elem;
if(q->rear == NULL)
{
q->front = temp;
q->rear = temp;
temp->next =NULL;
}
else
{
q->rear->next = temp;
q->rear = temp;
temp->next = NULL;
}
}
int deQueue(Queue* &q)
{
Node *temp;
int retTemp;
if(q->rear == NULL)
return 0;
if(q->front==q->rear)
{
temp = q->front;
q->front = q->rear = NULL;
retTemp = temp->data;
free(temp);
}
else
{
retTemp = q->front->data;
temp = q->front;
q->front = q->front->next;
free(temp);
}
return retTemp;
}
int isEmptyQueue(Queue* q)
{
if(q->front == NULL || q->rear == NULL)
return 1;
else
return 0;
}
int main(int argc, char **argv)
{
Queue *L;
L = CreateQueue();
enQueue(L,1);
enQueue(L,2);
enQueue(L,3);
enQueue(L,4);
printQueue(L);
int temp = deQueue(L);
printf("出队数据:%d\n",temp);
printQueue(L);
return 0;
}
链队列的基本操作
最新推荐文章于 2022-06-01 16:15:04 发布