码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构之队列

时间:2016-03-31 23:20:07      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

基本概念

技术分享

循环队列

技术分享

入队出队操作

技术分享

队列应用

技术分享

代码实现

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!