请实现一个MyQueue类,实现出队,入队,求队列长度.
实现入队函数 void push(int x); 实现出队函数 int pop(); 实现求队列长度函数 int size();
#include#include "malloc.h" typedef struct QNode{ int data; struct QNode *next; }QNode, *QueuePtr; typedef struct { QueuePtr front, rear; }linkQueue; void push(int e, linkQueue *q){ QueuePtr s = (QueuePtr)malloc(sizeof(QNode)); // if (!s) // exit(0); s->data = e; s->next = NULL; q->rear->next = s; q->rear = s; } int pop(linkQueue *q, int e){ QueuePtr p = {0}; if (q->front == q->rear) { printf("Invalidn"); return 0; } p = q->front->next; e = p->data; q->front->next = p->next; if (q->rear == p) q->rear = q->front; free(p); printf("%dn", e); } int size(linkQueue q){ int count = 0; if (q.front == q.rear) return count; QueuePtr p = q.rear->next; while(1){ if (p == q.rear) break; p = q.front->next; count++; } return count; } int main() { int n, a1; int ope; int x; linkQueue q = {0}, *q1; q.front = q.rear = (QueuePtr)malloc(sizeof(QNode)); q.rear->data=0; q.front->next = NULL; q1 = &q; scanf("%d", &n); for (int i = 0; i < n; i++){ scanf("%d", &ope); switch(ope){ case(1): scanf("%d", &x); push(x, q1); break; case 2: pop(q1, x); break; case 3: a1 = size(q); printf("%dn", a1); break; } } return 0; }



