Teaching Purpose: grasping the type definition of queue, grasping the representation and realization method of chain queue
Teaching emphases: the representation and realization of chain queue
Teaching Difficulty: the representation and realization of chain queue
Teaching Content:
Definition of a queue:
A queue is an advanced first-out linear table. It only allows inserts at one end of the table, while the elements are deleted at the other end. As in daily life in line, the first team to leave the earliest.
In a queue, an end that is allowed to be inserted is called a team tail, and an end that allows deletion is called a team head.
Abstract data type queues:
ADT queue{
Data objects: d={ai| ai (-elemset,i=1,2,..., n,n>=0}
Data relationship: r1={<ai-1,ai> | Ai-1,ai (-d,i=2,..., n}
Basic operations:
Initqueue (&Q) constructs an empty queue Q
Destroyqueue (&q) queue Q exists then destroys Q
Clearqueue (&q) queue Q exists to clear Q as an empty queue
Queueempty (q) queue Q exists and returns true if Q is an empty queue, otherwise returns false
Queuelenght (q) Queue Q exists, returns the number of elements of Q, that is, the length of the queue
GetHead (q,&e) q is a non-empty queue, the team head element that returns Q with E
EnQueue (&q,e) queue Q exists, the team tail element that inserts element e as Q
Dequeue (&q,&e) q is a non-empty queue, delete Q's team head element and return its value with E
Queuetraverse (Q,vivsit ()) Q exists and is not empty, calling function visit () for each data element of Q, from the team head to the end of the team. Once visit () fails, the operation fails
}adt Queue
Chain-type representation and implementation of chain queue-queue
The queue represented by the list is a chain queue. A chain queue clearly requires two pointers that indicate the team head and the tail of the team respectively.
|
|
|
|
| Q.front-> |
|
| |
|
|
|
\|/ |
|
|
1 |
| |
Team Head |
|
|
\|/ |
|
|
2 |
| |
|
|
|
\|/ |
|
|
3 |
| |
|
|
|
\|/ \|/ |
|
| Q.rear-> |
9 |
/\ |
Team Tail |
|
|
|
|
|
| Q.front-> |
|
| |
|
|
|
\|/ |
|
|
1 |
| |
Team Head |
|
|
\|/ |
|
|
2 |
| |
|
|
|
\|/ |
|
|
3 |
| |
|
|
|
\|/ \|/ |
|
| Q.rear-> |
9 |
/\ |
Team Tail |
|
Chain queue representation and implementation:
Storage representation
typedef struct qnode{
Qelemtype data;
struct Qnode *next;
}qnode,*queueptr;
typedef struct{
Queueptr Front;
QUEUEPTR Rear;
}linkqueue;
Operation Instructions
Status initqueue (Linkqueue &q)
Construct an empty queue Q
Status destroyqueue (Linkqueue &q)
Queue Q exists then destroys Q
Status clearqueue (Linkqueue &q)
Queue Q is a null queue
Status queueempty (Linkqueue Q)
Queue Q exists, if q is empty queue returns True, otherwise returns false
Status queuelenght (Linkqueue Q)
Queue Q exists, returns the number of elements of Q, that is, the length of the queue
Status GetHead (linkqueue q,qelemtype &e)
Q is a non-empty queue, the team head element that returns Q with E
Status EnQueue (Linkqueue &q,qelemtype e)
Queue Q exists, insert element e as Q's team tail element
Status dequeue (linkqueue &q,qelemtype &e)
Q is a non-empty queue, delete Q's team head element and return its value with E
Status queuetraverse (Linkqueue q,qelemtype vivsit ())
Q exists and is not NULL, from the team head to the end of the team, sequentially to the Q of each data element call function visit (). Once visit () fails, the operation fails
Implementation of the operation
Status initqueue (Linkqueue &q) {
Construct an empty queue Q
Q.front=q.rear= (queueptr) malloc (sizeof (Qnode));
if (! Q.front) exit (OVERFLOW);
q.front->next=null;
return OK;}
Status destroyqueue (Linkqueue &q) {
Queue Q exists then destroys Q
while (Q.front) {
q.rear=q.front->next;
Free (Q.front);
Q.front=q.rear;
}
return OK;}
Status EnQueue (Linkqueue &q,qelemtype e) {
Queue Q exists, insert element e as Q's team tail element
p= (queueptr) malloc (sizeof (Qnode));
if (!p) exit (OVERFLOW);
p->data=e;p->next=null;
q.rear->next=p;
Q.rear=p;
return OK;}
Status dequeue (linkqueue &q,qelemtype &e) {
Q is a non-empty queue, delete Q's team head element and return its value with E
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;}
Third, summary
Storage representation of Chain queues
Operation and implementation of chain queue