- #include <stdio.h>
- #include <stdlib.h>
- typedef struct _queue
- {
- int data;
- struct _queue *next;
- }QUEUE;
- QUEUE *front = NULL, *rear = NULL;
- int InQueue(int value)
- {
- QUEUE *temp = NULL;
- temp = (QUEUE *)malloc(sizeof(QUEUE));
- temp->data = value;
- temp->next = NULL;
- if(front == NULL)
- front = temp;
- else
- rear->next = temp;
- rear = temp;
- return 1;
- } //在队列 尾部 进行输入
- int InQueue2(int value)
- {
- QUEUE *temp = NULL;
- temp = (QUEUE *)malloc(sizeof(QUEUE));
- temp->data = value;
- temp->next = NULL;
- if(front == NULL && rear == NULL)
- {
- front = rear = temp;
- rear->next = front->next = NULL;
- }
- else
- {
- temp->next = front;
- front = temp;
- }
- } //在对列 头部进行输入
- int OutQueueByFront(int *value)
- {
- QUEUE *temp = NULL;
- temp = front;
- if(front == NULL)
- return 0;
- *value = front->data;
- front = front->next;
- free(temp);
- return 1;
- }
- int OutQueueByRear(int *value)
- {
- QUEUE *temp;
- if(rear == NULL)
- return 0;
- if(front == rear)
- {
- *value = rear->data;
- free(rear);
- front = NULL;
- rear = NULL;
- }else
- {
- temp = front;
- while(temp->next != rear)
- temp = temp->next;
- *value = rear->data;
- free(rear);
- rear = temp;
- rear->next = NULL;
- }
- return 1;
- }
- void main()
- {
- int arr[6] = {1,2,3,4,5,6},res,i;
- for(i=0; i<6; i++)
- InQueue2(arr[i]);
- if(OutQueueByFront(&res))
- printf("what we get is %d \n",res);
- else printf("we not get it\n");
- if(OutQueueByRear(&res))
- printf("what we get is %d \n",res);
- else printf("we not get it\n");
- }