#include <stdio.h>
#include <stdlib.h>
typedef int ELEMTYPE;
/************************************************************************/
/* The following are 6 algorithms for queue-linked storage operations * *
/************************************************************************/
struct snode{
Elemtype data; /* Domain/
struct Snode *next; /* Link pointer/*
};
struct queuelk{
struct Snode *front; * * Team First pointer/
struct Snode *rear; /* Team Tail pointer * *
};
/* 1. INIT Chain Team * *
void Initqueue (struct queuelk *hq)
{
Hq->front = Hq->rear = NULL; * * Set the head and tail of the team to empty * *
Return
}
/* 2. Insert an element x to the chain team
void EnQueue (struct queuelk *hq, Elemtype x)
{
/* Get a new node that is pointed by the NEWP pointer * *
struct Snode *newp;
NEWP = malloc (sizeof (struct snode));
if (NEWP = = NULL) {
printf ("Memory space allocation failed!") ");
Exit (1);
}
/* Assign the value of x to the domain of the new node, and place the pointer field of the new node in the space.
Newp->data = x;
Newp->next = NULL;
/* If the chain is empty, then the new node is the team's first knot is the team's tail node.
if (hq->rear = = NULL) {
Hq->front = Hq->rear = NEWP;
}else{/* If the chain team is not empty, then modify the team tail node pointer field and team tail pointer, make it point to the new team tail node.
Hq->rear = Hq->rear->next = NEWP; /* Note Assignment Order Oh * *
}
Return
}
/* 3. Delete an element from the queue * *
Elemtype outqueue (struct queuelk *hq)
{
struct Snode *p;
Elemtype temp;
/* If the chain team is empty then stop running *
if (Hq->front = = NULL) {
printf (the queue is empty and cannot be deleted!) ");
Exit (1);
}
temp = hq->front->data; /* Temporary team tail element to return * *
p = hq->front; /* Temporary team tail pointer for recovery team tail node
Hq->front = p->next; /* Make team head pointing to the next node.
/* If deleted after the chain team is empty, you need to make the tail of the team at the same time the pointer is empty * *
if (Hq->front = = NULL) {
Hq->rear = NULL;
}
Free (p); * * Recovery of the original team first node * *
return temp; /* Return the deleted team first element value * *
}
/* 4. Read the team first element * *
Elemtype peekqueue (struct queuelk *hq)
{
/* If the chain team is empty then stop running *
if (Hq->front = = NULL) {
printf (the queue is empty and cannot be deleted!) ");
Exit (1);
}
Return hq->front->data; /* Return to Team first element * *
}
* 5 Check whether the chain team is empty, if empty then return 1, otherwise return 0 * * *
int emptyqueue (struct queuelk *hq)
{
* * To determine whether the team head or the end of any one pointer is empty can be/
if (Hq->front = = NULL) {
return 1;
}else{
return 0;
}
}
/* 6. Clear all elements of the chain team * *
void Clearqueue (struct queuelk *hq)
{
struct Snode *p = hq->front; /* Team's first pointer to p * *
/* Delete Each node in the queue in turn, and finally make the team first pointer empty * *
while (P!= NULL) {
Hq->front = hq->front->next;
Free (p);
p = hq->front;
}//* After the end of the loop the team's first pointer is empty * *
Hq->rear = NULL; /* Set tail pointer is empty * *
Return
}
/************************************************************************/
int main (int argc, char* argv[])
{
struct Queuelk q;
int a[8] = {3, 8, 5, 17, 9, 30, 15, 22};
int i;
Initqueue (&Q);
for (i = 0; i < 8; i++) {
EnQueue (&q, a[i]);
}
printf ("%d", Outqueue (&q)); printf ("%d", Outqueue (&q));
EnQueue (&q, 68);
printf ("%d", Peekqueue (&q)); printf ("%d", Outqueue (&q));
while (!emptyqueue (&q)) {
printf ("%d", Outqueue (&q));
}
printf ("");
Clearqueue (&Q);
System ("pause");
}