用数组和链表两种方式实现队列
手写数组实现队列
int queue[20]; int front,rear; void clear() { front = rear = -1; } int size() { return (rear-front); } bool empty() { if(front==rear) return true; else return false; } void push(int x) { queue[++rear] = x; } void pop() { front++; } int get_front() { return queue[front+1]; } int get_rear() { return queue[rear]; }
手写链表实现队列
struct node { int data; node* next; }; node* Create(int box[],int t) // 创建一个有头节点的链表 { node *head,*pre,*temp; head = new node; head->next = NULL; pre = head; for(int i=0;i<t;++i) { temp = new node; temp->data = box[i]; temp->next = NULL; pre->next = temp; pre = temp; } return head; }