Implementation of basic operation of queue

Posted by R_P on Tue, 04 Jan 2022 17:51:04 +0100

Implementation of basic operation of queue

  1. Experimental purpose
    Master the abstract data type of queue, correctly select it in corresponding application problems, master the implementation method of queue (sequential and chained), the implementation algorithms of two storage structures and basic operations, pay attention to the judgment conditions of empty and full and their description methods, and master the differences and solutions between the implementation of circular queue and other sequential structures, Be familiar with the basic operations of various queues and the implementation of circular queues

  2. Experimental content
    Realize the following menu functions, call the above functions according to the user's choice, and check the correctness of the basic operation for future use. In addition to initialization and destruction, the other seven queue basic operations and "exit" correspond to one menu item respectively. After initializing the queue, this menu will appear, execute the corresponding queue operation according to the user's selection, and display the results. The menu can be cycled until the user selects "exit". At this time, the queue is destroyed and the process is over.

  3. Data structure definition
    I set up a queue. Each node in the queue is Qnode. There is a pointer that only wants Qnode, and there is a rear and front as pointers. Queue is a first in first out data structure. It can use the linear structure of static storage or single linked list for storage.

  4. Algorithm idea and algorithm design
    First, design a menu interface, in which you can call the functions of other parts, and provide the interface of each function to facilitate subsequent calls. For each function call, the specific algorithm is as follows:

Status InitQueue(Queue &q)
{
   if (!q.base)
       free(q.base);
   q.base = new Elemtype[maxsize];
   if (!q.base)
       exit(OVERFLOW);
   q.front = q.rear = 0;
   return OK;
}
Status DestrotQueue(Queue &q)
{
   q.rear = q.front = 0;
   delete q.base;
   q.base = NULL;
   return OK;
}
Status ClearQueue(Queue &q)
{
   q.rear = q.front = 0;
   return OK;
}
Status QueueEmpty(Queue q)
{
   if (q.front == q.rear)
       return OK;
   else
       return FALSE;
}
Status QueueLength(Queue q)
{
   return (q.rear - q.front + maxsize) % maxsize; //Learn how to find the number of elements
}
Status GetHead(Queue q, Elemtype &e)
{
   if (QueueEmpty(q))
       return FALSE;
   e = q.base[q.front];
   return OK;
}
Status EnQueue(Queue &q, Elemtype &e)
{
   if (q.front == (q.rear + 1) % maxsize)
       return OVERFLOW;
   q.base[q.rear] = e; //Considering the closed-loop situation
   q.rear = (q.rear + 1) % maxsize;
   return OK;
}
Status Dequeue(Queue &q, Elemtype &e)
{
   if (QueueEmpty(q))
       return FALSE;
   e = q.base[q.front];
   q.front = (q.front + 1) % maxsize;
   return OK;
}
Status visit(Elemtype e)
{
   printf(" %d", e);
   return OK;
}

Status QueueTraverse(Queue Q, Status (*visit)(Elemtype e))
{

   for (int i = Q.front; i != Q.rear; i = (i + 1) % maxsize)
   {
       visit(Q.base[i]);
   }
   printf("\n");

   return OK;
} 
  1. Experimental code
#include <iostream>
#include "queue.hpp"
#include "constants.h"
void pt(Queue &Q);
using namespace std;
Status InitQueue(Queue &q)
{
   if (!q.base)
       free(q.base);
   q.base = new Elemtype[maxsize];
   if (!q.base)
       exit(OVERFLOW);
   q.front = q.rear = 0;
   return OK;
}
Status DestrotQueue(Queue &q)
{
   q.rear = q.front = 0;
   delete q.base;
   q.base = NULL;
   return OK;
}
Status ClearQueue(Queue &q)
{
   q.rear = q.front = 0;
   return OK;
}
Status QueueEmpty(Queue q)
{
   if (q.front == q.rear)
       return OK;
   else
       return FALSE;
}
Status QueueLength(Queue q)
{
   return (q.rear - q.front + maxsize) % maxsize; //Learn how to find the number of elements
}
Status GetHead(Queue q, Elemtype &e)
{
   if (QueueEmpty(q))
       return FALSE;
   e = q.base[q.front];
   return OK;
}
Status EnQueue(Queue &q, Elemtype &e)
{
   if (q.front == (q.rear + 1) % maxsize)
       return OVERFLOW;
   q.base[q.rear] = e; //Considering the closed-loop situation
   q.rear = (q.rear + 1) % maxsize;
   return OK;
}
Status Dequeue(Queue &q, Elemtype &e)
{
   if (QueueEmpty(q))
       return FALSE;
   e = q.base[q.front];
   q.front = (q.front + 1) % maxsize;
   return OK;
}
Status visit(Elemtype e)
{
   printf(" %d", e);
   return OK;


Status QueueTraverse(Queue Q, Status (*visit)(Elemtype e))
{

   for (int i = Q.front; i != Q.rear; i = (i + 1) % maxsize)
   {
       visit(Q.base[i]);
   }
   printf("\n");

   return OK;
}

void hanshu2(Queue &q)
{
   printf("Please enter the element to be queued\n");
   int x;
   scanf("%d", &x);
   EnQueue(q, x);
}
void hanshu3(Queue &Q)
{
   int e;
   Dequeue(Q, e);
   printf("The out of queue elements are:%d\n", e);
}
Status init(Queue &Q)
{
   int num = 0;

   do
   { //Using a dowile loop
       printf("menu\n");
       printf("1----Initialize queue\n");
       printf("2----Join the team\n");
       printf("3----Out of the team\n");
       printf("4----Find the team head element\n");
       printf("5----Find the length of the team\n");
       printf("6----Destroy queue\n");
       printf("7----Empty queue\n");
       printf("8----Judge whether the queue is empty\n");
       printf("9----Traverse the queue\n");
       printf("0----sign out\n");
       scanf("%d", &num);
       switch (num)
       {
       case 1:
           InitQueue(Q);
           break;
       case 2:
           hanshu2(Q);
           break;
       case 3:
           hanshu3(Q);
           break;
       case 4:
       {
           int e;
           GetHead(Q, e);
           printf("The team head elements are:%d\n", e);
       }
       break;
       case 5:
       {
           printf("The team length is:%d\n", QueueLength(Q));
       }
       break;
       case 6:
       {
           printf("Are you sure you want to destroy the queue??\n1---YES\n2---NO\n");
           int x;
           scanf("%d", &x);
           if (x == 1)
               DestrotQueue(Q);
       }
       break;
       case 7:
       {
           printf("Are you sure you want to empty the queue??\n1---YES\n2---NO\n");
           int x;
           scanf("%d", &x);
           if (x == 1)
               ClearQueue(Q);
       }
       break;
       case 8:
       {
           if (QueueEmpty(Q))
               printf("The queue is empty\n");
           else

               printf("The queue is not empty\n");
       }
       break;
       case 9:
       {
           QueueTraverse(Q, visit);
       }
       break;
       default:
           return 0;
       }

   } while (num != 0);
   return OK;
}
void pt(Queue &Q)
{
   for (int i = Q.front; i != Q.rear; i = (i + 1) % maxsize)
       printf("%d ", Q.base[i]);
   printf("\n");
}
int main(int argc, char *argv[])
{
   Queue Q;
   InitQueue(Q);
   //int x=4;
   //EnQueue(Q, x);
   //pt(Q);
   //int y;4
   //GetHead(Q, y);
   //printf("hnbb    %d\n",QueueLength(Q));
   init(Q);
}


  1. Algorithm test results
    Demonstrate each operation of the menu as follows:

  2. Analysis and summary
    (1) Analysis of algorithm complexity and its advantages and disadvantages
    The time complexity of the algorithm varies according to each operation. Except that the traversal operation of the queue is O (N), the others are o (1). The time complexity is very low, which is very suitable for operation. The advantage of the algorithm is that each has a fixed module, the structure of the algorithm is very good, and it is very friendly to the overall programming. The disadvantage is that due to its linear structure, the algorithm has its own limitations, high requirements for entering and leaving the queue, and it is not random.
    (2) Experimental summary
    When writing the algorithm to judge the empty and full teams, it was not analyzed clearly, and it was unable to find out how to judge these two operations more accurately, so that segment errors occurred in the later stage, which made it impossible to carry out the program. Also, pay attention to the use of circular queues to make better use of the whole space and make better use of all space.

Topics: Java data structure linked list