C/C + + week 7 stack and queue item 2

/*   
*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 11, 2017   
*Version number: v1.0   
*   
*Problem Description: define chain storage structure and implement basic algorithm.  
*Enter Description: None 
*Program output: see screenshot of operation result   
*/ 

  

This project has been completed with reference to project 1.

Main function:

#include <stdio.h>
#include "../liqueue.h"

int main()
{
    ElemType e;
    LiQueue *q;
    printf("(1)Initialize chain team q\n");
    InitQueue(q);
    printf("(2)Team elements in turn a,b,c\n");
    enQueue(q,'a');
    enQueue(q,'b');
    enQueue(q,'c');
    printf("(3)Chain team for%s\n",(QueueEmpty(q)?"empty":"Not empty"));
    if (deQueue(q,e)==0)
        printf("Team empty,Can't leave the team.\n");
    else
        printf("(4)One element out of the team%c\n",e);
    printf("(5)Chain team q Number of elements of:%d\n",QueueLength(q));
    printf("(6)Team elements in turn d,e,f\n");
    enQueue(q,'d');
    enQueue(q,'e');
    enQueue(q,'f');
    printf("(7)Chain team q Number of elements of:%d\n",QueueLength(q));
    printf("(8)Outbound team sequence:");
    while (!QueueEmpty(q))
    {
        deQueue(q,e);
        printf("%c ",e);
    }
    printf("\n");
    printf("(9)Release chain team\n");
    DestroyQueue(q);
    return 0;
}

liqueue.cpp

#include <stdio.h>
#include <malloc.h>
#include "liqueue.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;   //p points to the queue header data node
    if (p!=NULL)            //Free data node space
    {
        r=p->next;
        while (r!=NULL)
        {
            free(p);
            p=r;
            r=p->next;
        }
    }
    free(p);
    free(q);                //Release the space occupied by chain nodes
}
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)      //If the chain team is empty, the new node is the team head node and the team tail node
        q->front=q->rear=p;
    else
    {
        q->rear->next=p;    //Chain the * p node to the end of the queue and point the rear at it
        q->rear=p;
    }
}
bool deQueue(LiQueue *&q,ElemType &e)   //Team out
{
    QNode *t;
    if (q->rear==NULL)      //Queue is empty
        return false;
    t=q->front;             //t points to the first data node
    if (q->front==q->rear)  //When there is only one node in the queue
        q->front=q->rear=NULL;
    else                    //When there are multiple nodes in the queue
        q->front=q->front->next;
    e=t->data;
    free(t);
    return true;
}
liqueue.h

#ifndef LIQUEUE_H_INCLUDED
#define LIQUEUE_H_INCLUDED

typedef char 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

#endif // LIQUEUE_H_INCLUDED

The screenshot of the operation is as follows:


Experience after learning:

This project has strengthened my basic ability of building a chain team and gained a lot.



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

Posted by del753 on Thu, 02 Apr 2020 23:27:31 -0700