用数组和链表两种方式实现队列

手写数组实现队列

 1 int queue[20];
 2 int front,rear;
 3 
 4 void clear()
 5 {
 6     front = rear = -1;
 7 }
 8 
 9 int size()
10 {
11     return (rear-front);
12 }
13 
14 bool empty()
15 {
16     if(front==rear)
17         return true;
18     else
19         return  false;
20 }
21 
22 void push(int x)
23 {
24     queue[++rear] = x;
25 }
26 
27 void pop()
28 {
29     front++;    
30 }
31 
32 int get_front()
33 {
34     return queue[front+1];
35 }
36 
37 int get_rear()
38 {
39     return queue[rear];
40 }

手写链表实现队列

 1 struct node
 2 {
 3     int data;
 4     node* next;
 5 };
 6  
 7 node* Create(int box[],int t)  // 创建一个有头节点的链表 
 8 {
 9     node *head,*pre,*temp;
10     head = new node;
11     head->next = NULL;
12     pre = head;
13     for(int i=0;i<t;++i) { temp = new node; temp->data = box[i];
14         temp->next = NULL;
15         pre->next = temp;
16         pre = temp;
17     }
18     return head;
19 } 

猜你喜欢

转载自www.cnblogs.com/kachunyippp/p/10256782.html