Data structure of the queue __ data structure

Source: Internet
Author: User

First, one-way chained queues

#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <iostream> using namespace s

td
    typedef struct node{//definition queue int data;
struct node* next;

}queue;
    The typedef struct pointer{//defines the pointer to the queue head and the end of the queue queue* front;
queue* Rear;

}qpointer;
    void Queueinit (qpointer* qp) {//initialization queue queue* que;
    que = (queue*) malloc (sizeof (Queue));
    Que->next = NULL;
    Qp->front = que;
Qp->rear = que;
    } int IsEmpty (qpointer* qp) {//queue non-null if (Qp->front = = qp->rear) return 1;
else return 0;
    int Queuepush (qpointer* qp,int Element) {//Insert//sucess return 1,failure return 0 queue* que;
    que = (queue*) malloc (sizeof (Queue));

    if (que = NULL) return 0;
    Que->data = element;
    Que->next = NULL;
    Qp->rear->next = que;
    Qp->rear = que;
return 1;
    int Queuepop (qpointer* qp,int* Element) {//delete queue* que;
    if (IsEmpty (QP)) {return 0;
}    que = qp->front->next;
    *element = que->data;
    Qp->front->next = que->next;
    if (qp->rear = = que) {qp->rear = qp->front;
    Free (que);
return 1;
    int main () {qpointer* qp;

    int x;
    QP = (qpointer*) malloc (sizeof (qpointer));
    Queueinit (QP);
    cout << "Enter positive intergers:";
    CIN >> X;
        while (x > 0) {queuepush (qp,x);
        cout << x << "";
    x--;

    } cout << Endl;
    print queue queue* p = qp->front->next;
    if (p = NULL) return 0;
    cout << "queue element:";
        while (p) {cout << "" << p->data;
    p = p->next;

    } cout << Endl;
    Delete queue cout << "Delete queue:";
    while (Queuepop (qp,&x)) {cout << x;

    } cout << Endl;
    Free Memnory p = qp->front;
    Free (p);

    Free (QP);
return 0; }

Two, circular queues

 #include <iostream> #include <malloc.h> #define TRUE 1 #define FALSE 0 #define OK 1 #define Erro  
 R 0 #define INFEASIBLE-1 #define OVERFLOW-2 using namespace std; typedef int STATUS;  

 /* Status is the type of function whose value is the function result status code, such as OK/typedef int QELEMTYPE; #define MAXQSIZE 5//Maximum queue Length (up to 1 for loop queues) typedef struct {Qelemtype *base;//initialized dynamic allocated storage space int front ; The head pointer, if the queue is not empty, points to the queue head element int rear;  

 The trailing pointer, if the queue is not empty, points to the next position of the queue tail element}sqqueue; /* bo3-3.c cycle Queue (storage structure defined by C3-3.H) basic operation (9)/Status Initqueue (Sqqueue &q) {/* Constructs an empty queue Q/(q). base= (Qelemtyp  
   E *) malloc (maxqsize*sizeof (Qelemtype)); if (!) (  
   Q. Base)/* Storage allocation failure/exit (OVERFLOW);  
   (q). front= (q). rear=0;  
 return OK; Status queueempty (Sqqueue q) {/* If queue Q is empty queue, returns True, otherwise returns false */if (q.front==q.rear)/* Queue empty flag/RET  
   Urn TRUE;  
 else return FALSE; Status GetHead (sqqueue q,qelemtype &e) {/* If the queue is not empty, return Q's team head element with E and return OK, otherwise return error * * */if (q.front==q.rear)/* Queue null/back error;  
   e=* (Q.base+q.front);  
 return OK; } Status EnQueue (Sqqueue &q,qelemtype e) {/* New team TAIL element * * Insert element e Q/if (((q). rear+1)%maxqsize== (q). Front)/* Queue  
   Full */return ERROR;  
   (q). base[(q). rear]=e;  
   (q). rear= ((q). rear+1)%maxqsize;  
 return OK; Status dequeue (sqqueue &q,qelemtype &e) {/* If the queue is not empty, delete Q's team head element, return its value with E, return OK, otherwise return error */if (q). front=  
   = (Q). Rear/* Queue null/return ERROR;  
   E= (q). base[(q). Front];  
   (q). front= ((q). front+1)%maxqsize;  
 return OK;  
   Status Queuetraverse (Sqqueue q,void (*VI) (Qelemtype)) {/* Call Function VI () from Team head to team tail for each element in queue Q. Once vi fails, the operation fails/int i;  
   I=q.front;  
     while (i!=q.rear) {VI (* (q.base+i));  
   I= (i+1)%maxqsize;  
   printf ("\ n");  
 return OK;  
 } void Visit (Qelemtype i) {cout<<i<< "";  
   int main () {int i;  
   Qelemtype D;  
   Sqqueue Q; I=inItqueue (q);  
   EnQueue (q,1);  
   EnQueue (q,3);  
   EnQueue (q,5);  
   EnQueue (q,7);  
   EnQueue (q,9);  
   cout<< "has inserted 5 elements (1,3,5,7,9) \ n";  
   cout<< "The elements of the queue are:";  
   Queuetraverse (Q,visit);  
   cout<<endl;  
   I=gethead (Q,D);  
   if (I==ok) cout<< "Team head element is:" <<d<<endl;  
   Dequeue (Q,D);  
   cout<< "removed the team head element," <<d;  
   I=gethead (Q,D);  
   if (i==ok) cout<< "\ t The new team head element is:" <<d<<endl;  
   Dequeue (Q,D);  
   cout<< "removed the team head element," <<d;  
   I=gethead (Q,D);  
    if (i==ok) cout<< "\ t The new team head element is:" <<d<<endl;  
   Dequeue (Q,D);  
   cout<< "removed the team head element," <<d;  
   I=gethead (Q,D);  
   if (i==ok) cout<< "\ t The new team head element is:" <<d<<endl;  
   Dequeue (Q,D);  
   cout<< "removed the team head element," <<d;  
   I=gethead (Q,D);       
   if (i==ok) cout<< "\ t The new team head element is:" <<d<<endl;  
   Dequeue (Q,D);  
   cout<< "\ t deletes the last element" <<d<<endl; if (QueuEempty (q)) cout<< "Now queue is an empty queue";  
   else cout<< "Queue not empty";             
   cout<<endl;  
 return 0;   }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.