题解 | #队列(模板)#

C语言版本

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
  int arr[100000];
  int front;
  int back;
} queue;

void push(queue *q, int num) {
  q->arr[(q->back)++] = num;
}

int front(queue *q) {
  if (q->front == q->back) {
    printf("error\n");
    return -1;
  } else {
    return q->arr[(q->front)];
  }
}

void pop(queue *q) {
  if (q->front == q->back) {
    printf("error\n");
    return ;
  } else {
    printf("%d\n", q->arr[(q->front)++]);
  }
}

int main(int argc, char *argv[]) {
  queue *q = (queue *)malloc(sizeof(queue));
  q->front = 0;
  q->back = 0;
  
  int count = 0;
  scanf("%d", &count);
  
  while (--count >= 0) {
    char str[5];
    scanf("%s", str);
    
    if (!strcmp(str, "push")) {
      int tmp;
      scanf("%d", &tmp);
      
      push(q, tmp);
    } else if (!strcmp(str, "pop")) {
      pop(q);
    } else if (!strcmp(str, "front")) {
      int tmp = front(q);
      
      if (-1 != tmp) {
        printf("%d\n", tmp);
      }
    }
  }
  
  return 0;
}
全部评论

相关推荐

谦虚的布莱克选钝角:华为呢,那个很热情的
点赞 评论 收藏
分享
每晚夜里独自颤抖:把华北改为华南再试一试,应该就没啥问题了。改完可能都不用投,别人主动联系了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务