queue.cpp

来自「购物商场中模拟排队的程序」· C++ 代码 · 共 40 行

CPP
40
字号
#include <stdlib.h>
#include <stdio.h>
#include "item.h"
#include "queue.h"


typedef struct QUEUEnode* link;
struct QUEUEnode { Item item; link next; };
struct queue { link head; link tail; };


link NEW(Item item, link next)      
  { link x = (link)malloc(sizeof *x);
    x->item = item; x->next = next;     
    return x;                         
  }                                   
Q QUEUEinit(int maxN) 
  { Q q = (Q)malloc(sizeof *q); 
    q->head = NULL; q->tail = NULL; 
    return q;
  }
int QUEUEempty(Q q)
  { return q->head == NULL; }
void QUEUEput(Q q, Item item)
  { 
    if (q->head == NULL)
      { q->tail = NEW(item, q->head);
        q->head = q->tail;printf("No.%d element",item); return; }
    q->tail->next = NEW(item, q->tail->next); 
    q->tail = q->tail->next;
	printf("No.%d element",item);
  }
Item QUEUEget(Q q)
  { Item item = q->head->item;
    link t = q->head->next;
    free(q->head); q->head = t;
    return item;
  }

                 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?