C/C + + week 7 stack and queue array item 4

/*     
*Copyright(c)2017, School of computer science, Yantai University     
*All right reserved.     
*File name: main.cpp sqqueue.h sqqueue.cpp     
*Author: Huang Shisheng    
*Completion date: October 20, 2017     
*Version number: v1.0     
*     
*Problem Description: see Item 4 - queue array 
*Input Description: input 70 59 90 72 67 88 80 64 29 97 18 83 40 13 0   
*Program output: see screenshot of operation result     
*/ 


Main function:

#include <stdio.h>
#include<malloc.h>
#include"../liqueue.h"
#define N 10
int main()
{
    int i,a;
    LiQueue *qu[N];//Define queue pointer array
    for(i=0;i<N;i++)
        InitQueue(qu[i]);
     //Add values to the queue
    printf("Enter positive integers, ending with 0: ");
    scanf("%d", &a);
    while(a)
    {
        enQueue(qu[a%10], a);
        scanf("%d", &a);
    }

    //Output each queue
    printf("After sorting by single digit into each queue, the result of each queue is: \n");
    for(i=0;i<N;i++)
    {
        printf("qu[%d]: ",i);
        while(!QueueEmpty(qu[i]))
        {
             deQueue(qu[i], a);
            printf("%d ",a);
        }
        printf("\n");
    }
    for(i=0;i<N;i++)
        DestroyQueue(qu[i]);
    return 0;

}

liqueue.cpp Code:

#include"liqueue.h"
#include<stdio.h>
#include<malloc.h>
void InitQueue(LiQueue *&q)  //Initialize chain team
{
    q=(LiQueue *)malloc(sizeof(LiQueue));
    q->front=q->rear=NULL;

}
void DestroyQueue(LiQueue *&q) //Destruction chain
{
    QNode *p=q->front,*r;
    if(p!=NULL)
    {
        r=p->next;
        while(r!=NULL)
        {
            free (p);
            p=r;
            r=p->next;
        }
    }
    free(p);
    free(q);
}
bool QueueEmpty(LiQueue *q)  //Judge whether the chain team is empty
{
    return(q->rear==NULL);
}
int QueueLength(LiQueue *q) //Return the number of data elements in the queue
{
    int n=0;
    QNode *p=q->front;
    while(p!=NULL)
    {
        n++;
        p=p->next;

    }
    return (n);

}
void enQueue(LiQueue *&q,ElemType e) //Join the team
{
    QNode *p;
    p=(QNode *)malloc(sizeof (QNode));
    p->data=e;
    p->next=NULL;
    if(q->rear==NULL)
        q->front=q->rear=p;
    else
    {
        q->rear->next=p;
        q->rear=p;

    }
}
bool deQueue(LiQueue *&q,ElemType &e)   //Team out
{
    QNode *t;
    if(q->rear==NULL)
        return false;
        t=q->front;
        if(q->front==q->rear)
            q->front=q->rear=NULL;
        else
            q->front=q->front->next;
        e=t->data;
        free(t);
        return true;

}
liqueue.h Code:

typedef int ElemType;
typedef struct qnode
{
    ElemType data;
    struct qnode *next;
} QNode;        //Chain data node type definition

typedef struct
{
    QNode *front;
    QNode *rear;
} LiQueue;          //Chain team type definition
void InitQueue(LiQueue *&q);  //Initialize chain team
void DestroyQueue(LiQueue *&q);  //Destruction chain
bool QueueEmpty(LiQueue *q);  //Judge whether the chain team is empty
int QueueLength(LiQueue *q);  //Return the number of data elements in the queue
void enQueue(LiQueue *&q,ElemType e);  //Join the team
bool deQueue(LiQueue *&q,ElemType &e);   //Team out

Screenshot of operation result:




Learn new:

Through this project, I learned how to use the queue array to complete the data queue. Using the queue array, I actually need to store the pointers of ten queues in one array.







Published 34 original articles· Zan Zan 9. 10000 visitors+
Private letter follow

Posted by whatsthis on Thu, 02 Apr 2020 03:32:44 -0700