PTA 带头结点的链队列的基本操作 (10 分)

  • 实现链队列的入队列及出队列操作。

函数接口定义:

Status QueueInsert(LinkQueue *Q,ElemType e);

Status QueueDelete(LinkQueue *Q,ElemType *e);
其中 Q 和 e 都是用户传入的参数。 LinkQueue 的类型定义如下:

typedef int ElemType; 
typedef struct LNode
{
    ElemType data;
    struct LNode * next;
}LNode,*LinkList;
typedef struct
 {
   LinkList front,rear; /* 队头、队尾指针 */
 }LinkQueue;

裁判测试程序样例:

#include <stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
typedef int Status;
typedef int ElemType; 
typedef struct LNode
{
    ElemType data;
    struct LNode * next;
}LNode,*LinkList;

typedef struct
 {
   LinkList front,rear; /* 队头、队尾指针 */
 }LinkQueue;
 /* 带头结点的链队列的基本操作 */
 Status InitQueue(LinkQueue *Q)
 { /* 构造一个空队列Q */
     LinkList p;
   p=(LNode*)malloc(sizeof(LNode)); 
   p->next=NULL;
   (*Q).rear=(*Q).front=p;
   return OK;
 }
Status List(LinkList L)
{
    LinkList p;
    if(!L) return ERROR;
    p=L->next;

    while(p)
    {
        printf(" %d",p->data);
        p=p->next;
    }
    printf("\n");
    return OK;
}

int QueueLenth(LinkQueue Q)
{
    int n=0;
    LinkList p;
    if(Q.rear==Q.front)
        return 0;
    p=Q.front->next;
    while(p)
    {
        n++;
        p=p->next;
    }
    return n;
}

int main()
{
    int x;
    LinkQueue Q;
    InitQueue(&Q);
    QueueInsert(&Q,1);QueueInsert(&Q,2);QueueInsert(&Q,3);
    List(Q.front );
    QueueDelete( &Q,&x);
    printf(" %d %d\n",x,QueueLenth(Q));
    QueueDelete(&Q,&x);QueueDelete(&Q,&x);
    printf(" %d %d\n",x,QueueLenth(Q));
    return 0;
}

/* 请在这里填写答案 */

输出样例:

在这里给出相应的输出。例如:

 1 2 3
 1 2
 3 0
结尾无空行

AC

Status QueueInsert(LinkQueue *Q, ElemType e)
{
    LinkList p;
    p = (LinkList)malloc(sizeof(LinkList));
    if(p == NULL)
        return ERROR;
    p->data = e;
    p->next = NULL;
    Q->rear->next = p;
    Q->rear = p;
    return OK;
}

Status QueueDelete(LinkQueue *Q, ElemType *e)
{
    LNode *p;
    p = Q->front->next;
    if(Q->front == Q->rear){
        *e = -1;
        return ERROR;
    }
    Q->front->next = p->next;
    *e = p->data;
    free(p);
    return OK;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
队列是一种数据结构,它是一种先进先出(FIFO)的线性数据结构队列只允许在队列的一端进行插操作,称为队尾,而在另一端进行删除操作,称为队头。下面是队列实现基本操作。 1. 队列的结构定义 ```c #define MAXSIZE 100 // 队列最大容量 typedef struct { int data[MAXSIZE]; // 队列元素数组 int front; // 队头指针 int rear; // 队尾指针 } Queue; ``` 2. 初始化队列 ```c void initQueue(Queue *q) { q->front = q->rear = 0; // 队头和队尾指针初始化为0 } ``` 3. 判断队列是否为空 ```c int isEmpty(Queue *q) { return q->front == q->rear; } ``` 4. 判断队列是否已满 ```c int isFull(Queue *q) { return (q->rear + 1) % MAXSIZE == q->front; } ``` 5. 操作 ```c int enqueue(Queue *q, int x) { if (isFull(q)) // 队列已满,无法插 return 0; q->data[q->rear] = x; // 插元素到队尾 q->rear = (q->rear + 1) % MAXSIZE; // 队尾指针加1 return 1; } ``` 6. 出队操作 ```c int dequeue(Queue *q, int *x) { if (isEmpty(q)) // 队列为空,无法删除 return 0; *x = q->data[q->front]; // 取出队头元素 q->front = (q->front + 1) % MAXSIZE; // 队头指针加1 return 1; } ``` 7. 获取队头元素 ```c int getFront(Queue *q, int *x) { if (isEmpty(q)) // 队列为空 return 0; *x = q->data[q->front]; // 取出队头元素 return 1; } ``` 8. 获取队列长度 ```c int getLength(Queue *q) { return (q->rear - q->front + MAXSIZE) % MAXSIZE; } ``` 9. 输出队列元素 ```c void printQueue(Queue *q) { if (isEmpty(q)) // 队列为空 return; int i = q->front; while (i != q->rear) { printf("%d ", q->data[i]); i = (i + 1) % MAXSIZE; } printf("\n"); } ``` 以上就是队列实现基本操作。在使用队列时,需要注意队列的容量大小,避免队列溢出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值