队列数据结构代码
使用数组创建队列
1 #include <stdio.h> 2 #define MAXQUEUE 10 3 4 int queue[MAXQUEUE]; 5 int front = -1; 6 int rear = -1; 7 8 int enqueue(int value) 9 { 10 if(rear >= MAXQUEUE) 11 return -1; 12 rear++; 13 queue[rear] = value; 14 } 15 16 int dequeue() 17 { 18 if(front == rear) 19 return -1; 20 front++; 21 return queue[front]; 22 } 23 24 int main() 25 { 26 int input[100]; 27 int output[100]; 28 int select; 29 int i_count = 0; 30 int o_count = 0; 31 int loop = 1; 32 int i,temp; 33 34 while(loop) 35 { 36 printf("[1]输入 [2]取出 [3]列出全部内容 ==> "); 37 scanf("%d",&select); 38 switch(select) 39 { 40 case 1: printf("请输入存入队列的值(%d) ==> ",i_count + 1); 41 scanf("%d",&temp); 42 if(enqueue(temp) == -1) 43 printf("队列全满. \n"); 44 else 45 input[i_count++] = temp; 46 break; 47 case 2: if((temp = dequeue()) == -1) 48 printf("队列是空的. \n"); 49 else 50 { 51 printf("取出队列元素: %d\n",temp); 52 output[o_count++] = temp; 53 } 54 break; 55 case 3: loop = 0; 56 break; 57 58 } 59 } 60 printf("输入队列的元素:"); 61 for(i = 0; i < i_count;i++) 62 printf("[%d]",input[i]); 63 printf("\n取出队列的元素:"); 64 for(i = 0;i < o_count;i++) 65 printf("[%d]",output[i]); 66 printf("\n剩下队列的元素:"); 67 while((temp = dequeue()) != -1) 68 printf("[%d]",temp); 69 printf("\n"); 70 71 return 0; 72 }
数组循环队列
1 #include <stdio.h> 2 #define MAXQUEUE 10 3 4 int queue[MAXQUEUE]; 5 int front = -1; 6 int rear = -1; 7 8 int enqueue(int value) 9 { 10 if(rear + 1 == front || (rear == (MAXQUEUE - 1) && front <= 0)) 11 return -1; 12 rear++; 13 if(rear == MAXQUEUE) 14 rear = 0; 15 queue[rear] = value; 16 } 17 18 int dequeue() 19 { 20 if(front == rear) 21 return -1; 22 front++; 23 if(front == MAXQUEUE) 24 front = 0; 25 return queue[front]; 26 } 27 28 int main() 29 { 30 int input[100]; 31 int output[100]; 32 int select; 33 int i_count = 0; 34 int o_count = 0; 35 int loop = 1; 36 int i,temp; 37 while(loop) 38 { 39 printf("[1]输入 [2]取出 [3]列出全部内容 ==> "); 40 scanf("%d",&select); 41 switch(select) 42 { 43 case 1: printf("请输入存入队列的值(%d) ==> ",i_count + 1); 44 scanf("%d",&temp); 45 if(enqueue(temp) == -1) 46 printf("队列全满.\n"); 47 else 48 input[i_count++] = temp; 49 break; 50 51 case 2: if((temp = dequeue()) == -1) 52 printf("队列是空的.\n"); 53 else 54 { 55 printf("取出队列元素: %d\n",temp); 56 output[o_count++] = temp; 57 } 58 break; 59 60 case 3: loop = 0; 61 break; 62 } 63 } 64 printf("输入队列的元素:"); 65 for(i = 0;i < i_count;i++) 66 printf("[%d]",input[i]); 67 printf("\n取出队列的元素:"); 68 for(i = 0;i < o_count;i++) 69 printf("[%d]",output[i]); 70 printf("\n剩下队列的元素:"); 71 while((temp = dequeue()) != -1) 72 printf("[%d]",temp); 73 printf("\n"); 74 }
使用链表创建队列
1 #include <stdlib.h> 2 #include <stdio.h> 3 4 struct queue_node 5 { 6 int data; 7 struct queue_node * next; 8 }; 9 10 typedef struct queue_node queue_list; 11 typedef queue_list * link; 12 13 link front = NULL; 14 link rear = NULL; 15 16 int enqueue(int value) 17 { 18 link new_node; 19 new_node = (link)malloc(sizeof(queue_list)); 20 if(!new_node) 21 { 22 printf("内存分配失败!\n"); 23 return -1; 24 } 25 new_node->data = value; 26 new_node->next = NULL; 27 if(rear == NULL) 28 front = new_node; 29 else 30 rear->next = new_node; 31 rear = new_node; 32 } 33 34 int dequeue() 35 { 36 link top; 37 int temp; 38 39 if(front != NULL) 40 { 41 top = front; 42 front = front->next; 43 temp = top->data; 44 free(top); 45 return temp; 46 } 47 else 48 return -1; 49 } 50 51 int main() 52 { 53 int input[100]; 54 int output[100]; 55 int select; 56 int i_count = 0; 57 int o_count = 0; 58 int loop = 1; 59 int i,temp; 60 while(loop) 61 { 62 printf("[1]输入 [2]取出 [3]列出全部内容 ==> "); 63 scanf("%d",&select); 64 switch(select) 65 { 66 case 1: printf("请输入存入队列的值(%d) ==> ",i_count + 1); 67 scanf("%d",&temp); 68 if(enqueue(temp) == -1) 69 printf("队列全满.\n"); 70 else 71 input[i_count++] = temp; 72 break; 73 case 2: if((temp = dequeue()) == -1 ) 74 printf("队列是空的.\n"); 75 else 76 { 77 printf("取出队列元素: %d\n",temp); 78 output[o_count++] = temp; 79 } 80 break; 81 case 3: loop = 0; 82 break; 83 } 84 } 85 86 printf("输入队列的元素: "); 87 for(i = 0;i < i_count;i++) 88 printf("[%d]",input[i]); 89 printf("\n取出队列的元素: "); 90 for(i = 0;i < o_count;i++) 91 printf("[%d]",output[i]); 92 printf("\n剩下队列的元素: "); 93 while((temp = dequeue()) != -1) 94 printf("[%d]",temp); 95 printf("\n"); 96 }
使用数组来创建输入性限制双队列
1 #include <stdio.h> 2 3 #define MAXQUEUE 10 4 5 int queue [MAXQUEUE]; 6 int front = -1; 7 int rear = -1; 8 9 int enqueue(int value) 10 { 11 if(rear + 1 == front || (rear == (MAXQUEUE - 1) && front <= 0)) 12 return 1; 13 rear++; 14 if(rear == MAXQUEUE) 15 rear = 0; 16 queue[rear] = value; 17 } 18 19 int dequeue_rear() 20 { 21 int temp; 22 23 if(front == rear) 24 return -1; 25 temp = queue[rear]; 26 rear--; 27 if(rear < 0 && front != -1) 28 rear = MAXQUEUE - 1; 29 return temp; 30 } 31 32 int dequeue_front() 33 { 34 if(front == rear) 35 return -1; 36 front++; 37 if(front == MAXQUEUE) 38 front = 0; 39 return queue[front]; 40 } 41 42 43 int main() 44 { 45 int input_list[6] = {1,2,3,4,5,6}; 46 int output_list[6]; 47 int select; 48 int i,temp,pos = 0; 49 50 for(i = 0;i < 6;i++) 51 enqueue(input_list[i]); 52 while(front != rear) 53 { 54 printf("[1]从后取出 [2]从前取出 ==> "); 55 scanf("%d",&select); 56 switch(select) 57 { 58 case 1: temp = dequeue_rear(); 59 output_list[pos++] = temp; 60 break; 61 case 2: temp = dequeue_front(); 62 output_list[pos++] = temp; 63 break; 64 } 65 } 66 printf("原来数组的顺序: "); 67 for(i = 0;i < 6;i++) 68 printf("[%d]",input_list[i]); 69 printf("\n队列取出的顺序: "); 70 for(i = 0;i < 6;i++) 71 printf("[%d]",output_list[i]); 72 printf("\n"); 73 74 return 0; 75 }
使用链表创建输出限制性双队列
1 #include <stdlib.h> 2 #include <stdio.h> 3 4 struct queue_node 5 { 6 int data; 7 struct queue_note * next; 8 }; 9 typedef struct queue_node queue_list; 10 typedef queue_list * link; 11 12 link front = NULL; 13 link rear = NULL; 14 15 int enqueue_rear(int value) 16 { 17 link new_node; 18 19 new_node = (link)malloc(sizeof(queue_list)); 20 if(!new_node) 21 { 22 printf("内存分配失败! \n"); 23 return -1; 24 } 25 new_node->data = value; 26 new_node->next = NULL; 27 if(rear == NULL) 28 front = new_node; 29 else 30 rear->next = new_node; 31 rear = new_node; 32 } 33 34 int enqueue_front(int value) 35 { 36 link new_node; 37 38 new_node = (link)malloc(sizeof(queue_list)); 39 if(!new_node) 40 { 41 printf("内存分配失败! \n"); 42 return -1; 43 } 44 new_node->data = value; 45 if(front == NULL) 46 { 47 new_node->next = NULL; 48 rear = new_node; 49 } 50 else 51 new_node->next = front; 52 front = new_node; 53 } 54 55 int dequeue() 56 { 57 link top; 58 int temp; 59 60 if(front != NULL) 61 { 62 top = front; 63 front = front->next; 64 temp = top->data; 65 free(top); 66 return temp; 67 } 68 else 69 return -1; 70 } 71 72 int main() 73 { 74 int input_list[6] = {1,2,3,4,5,6}; 75 int select; 76 int i,temp,pos = 0; 77 78 for(i = 0;i < 6;i++) 79 { 80 printf("[1]从后存入 [2]从前存入 ==> "); 81 scanf("%d",&select); 82 switch(select) 83 { 84 case 1: enqueue_rear(input_list[i]); 85 break; 86 case 2: enqueue_front(input_list[i]); 87 break; 88 default: 89 enqueue_rear(input_list[i]); 90 break; 91 } 92 } 93 printf("原来数组的顺序: "); 94 for(i = 0;i < 6;i++) 95 printf("[%d]",input_list[i]); 96 printf("\n队列取出的顺序: "); 97 while((temp = dequeue()) != -1) 98 printf("[%d]",temp); 99 printf("\n"); 100 return 0; 101 }