标签:
c语言(数组)
#ifndef _QUEUE_H_ #define _QUEUE_H_ #include <malloc.h> #define TRUE 1 #define FALSE 0 #define ACTION_IN 0 #define ACTION_OUT 1 typedef unsigned char boolean; typedef struct QUEUE{ USER_TYPE *data; int maxRoom; int head; int tail; boolean lastAction; } QUEUE; boolean initQueue(QUEUE **queue, int maxRoom); void destoryQueue(QUEUE **queue); boolean isQueueEmpty(QUEUE queue); boolean isQueueFull(QUEUE queue); boolean QueueIn(QUEUE *queue, USER_TYPE value); boolean QueueOut(QUEUE *queue, USER_TYPE *value); boolean readHead(QUEUE queue, USER_TYPE *value); boolean readHead(QUEUE queue, USER_TYPE *value){ if(isQueueEmpty(queue)) return FALSE; *value = queue.data[queue.head]; return TRUE; } boolean QueueOut(QUEUE *queue, USER_TYPE *value){ if(!queue) return FALSE; if(isQueueEmpty(*queue)) return FALSE; *value = queue->data[queue->head]; queue->head = (queue->head+1) % queue->maxRoom; queue->lastAction = ACTION_OUT; return TRUE; } boolean QueueIn(QUEUE *queue, USER_TYPE value){ if(!queue) return FALSE; if(isQueueFull(*queue)) return FALSE; queue->data[queue->tail] = value; queue->tail = (queue->tail+1) % queue->maxRoom; queue->lastAction = ACTION_IN; return TRUE; } boolean isQueueFull(QUEUE queue){ return queue.lastAction == ACTION_IN && queue.head == queue.tail; } boolean isQueueEmpty(QUEUE queue){ return queue.lastAction == ACTION_OUT && queue.head == queue.tail; } void destoryQueue(QUEUE **queue){ if(*queue == NULL) return; if((*queue)->data) free((*queue)->data); free(*queue); *queue = NULL; } boolean initQueue(QUEUE **queue, int maxRoom){ if(*queue) return FALSE; if(maxRoom <= 0) return FALSE; if( (*queue = (QUEUE *)malloc(sizeof(QUEUE))) == NULL ) return FALSE; if ( ((*queue)->data = (USER_TYPE *)malloc(sizeof(USER_TYPE)*maxRoom)) == NULL ){ free(*queue); *queue = NULL; return FALSE; } (*queue)->maxRoom = maxRoom; (*queue)->head = (*queue)->tail = 0; (*queue)->lastAction = ACTION_OUT; return TRUE; } #endif
Python 版
#version1 class Queue(object): def __init__(self, maxRoom): self.maxRoom = maxRoom self.queue = [] def push(self,value): if not self.isfull(): self.queue.append(value) return True return False def pop(self): if not self.isempty(): value = self.queue.pop() return value return def readtop(self,): if not self.isempty(): return self.queue[-1] return def isempty(self,): return len(self.queue) == 0 def isfull(self): return len(self.queue) == self.maxRoom #verision2 class Queue(object): ‘‘‘ last_action = 1 表示上次插入动作为push入队列,0表示出队列 ‘‘‘ def __init__(self, maxRoom = 1024): self.maxRoom = maxRoom self.queue = [None for _ in range(maxRoom)] self.head = 0 self.tail = 0 self.last_action = 1 def push(self,value): if not self.isfull(): self.queue[self.tail] = value self.tail = (self.tail + 1) % self.maxRoom self.last_action = 0 return True return False def pop(self): if not self.isempty(): value = self.queue[self.head] self.queue[self.head] = None self.head = (self.head + 1) % self.maxRoom self.last_action = 1 return value return def readtop(self,): if not self.isempty(): return self.queue[head] return def isempty(self,): return self.last_action == 1 and self.head == self.tail def isfull(self): return self.last_action == 0 and self.head == self.tail
标签:
原文地址:http://www.cnblogs.com/wxl-dede/p/5343148.html