File Structure:
On the head node:
Q.head : the first node itself;
Q.head-> next head node: the next node
main.cpp:
#include <iostream> #include<stdio.h> #include<stdlib.h> #include "function_for_LinkQueue.h" using namespace std; int main() { TestLinkQueue(); return 0; }
function_for_LinkQueue.h:
FUNCTION_FOR_LINKQUEUE_H_INCLUDED #ifndef #define FUNCTION_FOR_LINKQUEUE_H_INCLUDED #define the MAXSIZE 100 typedef char elemType; // node typedef struct Qnode { elemType Data; // data field struct Qnode * Next; // pointer field } Qnode, * QueuePtr; typedef struct { QueuePtr head; / / HOL pointer - team points located Qnode head node QueuePtr REAR; // queue tail pointer - the end point node positioned Qnode team } LinkQueue; // initialize void InitQueue (& LinkQueue Q); // destruction // begin queue head node, all the nodes sequentially released void DestroyQueue (LinkQueue & Q); // (the tail) insert elements void InsertQueue (LinkQueue & Q, E elemType); // (HOL ) dequeue - return value elemType DelQueue_with_elem (LinkQueue & Q); // (HOL) dequeued - does not return a value void DelQueue (LinkQueue & Q); // Get the head elements elemType GetHead (LinkQueue & Q); // print queue elements - from start to finish void the PrintQueue (LinkQueue & Q); // test void TestLinkQueue (); #endif // FUNCTION_FOR_LINKQUEUE_H_INCLUDED
function_for_LinkQueue.cpp:
#include <stdio.h> #include <stdlib.h> #include " function_for_LinkQueue.h " // Initialization void InitQueue (LinkQueue & Q) { Q.head = (QueuePtr) the malloc ( the sizeof (Qnode)); // allocate space, the queue head, the tail pointer points to the first team Q.rear = Q.head; Q.head -> Next = NULL; } // destruction // begin queue head node, all the nodes sequentially released void DestroyQueue (LinkQueue & Q) { the while (Q.head =! NULL) { QueuePtr P = Q.head-> Next; Free (Q.head); Q.head = P; } the printf ( " destruction Success \ n- " ); } // (the tail) insert elements void InsertQueue (LinkQueue & Q, elemType E) { QueuePtr P = (QueuePtr) the malloc ( the sizeof (Qnode)); // the new node is assigned a space iF Exit ((P!) 0 ); // determines whether the allocation is successful p-> data = E; // the data field assigned p-> Next = NULL ; Q.rear -> Next = P; //Key Algorithm - As can be seen, the presence of the head of this queue node, the head node on the first node, the first node on the second element nodes Q.rear = P; } // (HOL) dequeued - Returns value elemType DelQueue_with_elem (LinkQueue & Q) { iF (Q.head == Q.rear) Exit ( 0 ); // queue is empty is determined to elemType next- E = Q.head->> Data; QueuePtr P = ( QueuePtr) the malloc ( the sizeof (Qnode)); P = Q.head-> next; Q.head -> next = p-> next; // consider a special case, if the next node is the head pointer to a tail pointer, i.e., queue only one element IF (Q.rear == P) = Q.head Q.rear; // the head pointer pointing to the tail pointer position, i.e., the queue empty Free (P); return E; } // (HOL) dequeued - Does not return a value void DelQueue (LinkQueue & Q) { IF (Q.head == Q.rear) Exit ( 0 ); // queue first determining whether empty QueuePtr P = (QueuePtr) the malloc ( the sizeof (Qnode)); P = Q.head-> next; Q.head -> next = p-> next; // consider a special case, if the head pointer of the next a node is tail pointer, i.e., only one element in the queue IF (Q.rear == P) = Q.head Q.rear; // the head pointer pointing to the tail pointer position, i.e., the queue empty Free (P); } // get the head elements GetHead elemType (LinkQueue & Q) { IF (Q.head == Q.rear) Exit ( 0 ); // judgment empty return Q.head-> next-> Data; // queue head node does not store elements, the first element node second node } // print queue elements - from start to finish void the PrintQueue (LinkQueue & Q) { IF (Q.head == Q.rear) Exit ( 0 ); // first sentence empty QueuePtr p = Q .head-> Next; // build the pointer on the first element nodes do { the printf ( " % C " , p-> data); // sequentially output the data field of each element p = p-> next; // After Rollover } the while (P); } // test void TestLinkQueue () { LinkQueue Q; the printf ( " \ n-Initialization: \ n- " ); InitQueue (Q); the printf ( " \ n-insertion elements a, b , c, d, e (the tail): \ n- " ); InsertQueue (Q, ' A ' ); InsertQueue (Q, ' B ' ); InsertQueue (Q, ' C ' ); InsertQueue (Q, ' D ' ); InsertQueue (Q, ' E' ); The printf ( " \ n-obtaining head elements:% C \ n- " , GetHead (Q)); the printf ( " Print Queue: \ n- " ); the PrintQueue (Q); // the printf ( "Get the head elements :% C \ n-", GetHead (Q)); // the printf (" Print queue: \ n "); // the PrintQueue (Q); the printf ( " \ n-tail is inserted again from the queue elements: \ n- " ); InsertQueue (Q, ' F ' ); // the printf ( " Get the head elements:% C \ n- " , GetHead (Q)); the printf ( " Print queue: \ n- " ); PrintQueue(Q); the printf ( " \ data field of the next node point n At this time, the head node is: C% \ n " , Q.head-> next-> Data); the printf ( " \ n dequeue: \ n " ); DelQueue (Q); the printf ( " Get the head elements:% C \ n- " , GetHead (Q)); the printf ( " Print queue: \ n- " ); the PrintQueue (Q); the printf ( " \ n-dequeued and return teams header element:% C \ n- " , DelQueue_with_elem (Q)); the printf ( " Get the head elements:% C \ n- " , GetHead (Q)); the printf ( " Print queue: \ n- " ); the PrintQueue (Q) ; printf(" \ The n-destruction node: \ the n- " ); DestroyQueue (Q); printf ( " get the head elements:% c \ the n- " , GetHead (Q)); printf ( " Print Queue: \ the n- " ); PrintQueue (Q ); }
operation result: