Queue of data structures

Source: Internet
Author: User

Basic concepts

Loop queue

Queue up operation

Queue Application

Code implementation

C Language (Array)

#ifndef _queue_h_#define _queue_h_#include <malloc.h> #define TRUE 1#define FALSE 0#define action_in 0    #define Action_out 1typedef 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 (Isqueu       Eempty (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 Edition

#version1class 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 retur        n 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 isempt Y (self,): Return len (self.queue) = = 0 def isfull (self): return len (self.queue) = = self.maxroom#verision2c        Lass Queue (object): ' Last_action = 1 indicates that the last insert action is a push-in queue, and 0 indicates the queue ' 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] = VA Lue Self.tail = (self.tail + 1)% Self.maxroom self.last_action = 0 return True return False def pop (SE            LF): 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 D        EF Readtop (self): if isn't 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 an D Self.head = = Self.tail

  

Queue of data structures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.