Data structure learning cycle queue (sequential storage) __ Queue

Source: Internet
Author: User
Tags assert

Summary queue feature: Advanced First Out (FIFO)--The elements of the advanced queue are first out of the queue. Come from the queues in our Lives (queue first and do it first).

This has a flaw, space utilization is not high, so we directly learn the loop queue (based on contiguous memory).


(1) Design the queue data structure

typedef struct _QUEUE_NODE
{
    int* pData;
    int length;//Queue Length
    int head;/team header pointer
    int tail;//team tail pointer
    int count;//queue element Current number
}queue_node;

(2) Application queue memory

queue_node* alloca_queue (int number)
{
    queue_node* pqueuenode;
    if (0 = number) return
        NULL;

    Pqueuenode = (queue_node*) malloc (sizeof (Queue_node));
    ASSERT (NULL!= pqueuenode);
    memset (pqueuenode, 0, sizeof (queue_node));

    Pqueuenode->pdata = (int*) malloc (sizeof (int) * number);
    if (NULL = = pqueuenode->pdata) {free
        (pqueuenode);
        Pqueuenode = NULL;
        return NULL;
    }

    Pqueuenode->length = number;//Queue Length return
    pqueuenode;
}

(3) Free queue memory

STATUS delete_queue (const queue_node* pqueuenode)
{
    if (NULL = = Pqueuenode) return 
        FALSE;

    ASSERT (NULL!= pqueuenode->pdata);

    Free (pqueuenode->pdata);
    Free ((void*) pqueuenode);
    return TRUE;
}

(4) Press the data into the queue

STATUS Insert_queue (queue_node* pqueuenode, int value)
{
    if (NULL = = Pqueuenode) return
        FALSE;

    if (pqueuenode->length = = pqueuenode->count)//judgment is not overflow return
        FALSE;

    Pqueuenode->pdata[pqueuenode->tail] = value;
    Pqueuenode->tail = (pqueuenode->tail + 1)% pqueuenode->length;  Team tail pointer position
    Pqueuenode->count + +;
    return TRUE;
}

(5) The data pop-up queue

STATUS Get_queue_data (queue_node* pqueuenode, int* value)
{
    if (NULL = = Pqueuenode | | NULL = value) return
        FALSE;

    if (0 = pqueuenode->count) return
        FALSE;

    *value = pqueuenode->pdata[pqueuenode->head];
    pqueuenode-> Pdata[pqueuenode->head] = 0; The position being ejected is assigned a value of 0
    pqueuenode-> count--;
    Pqueuenode->head = (pqueuenode->head + 1)% pqueuenode->length;//reposition the team head pointer return
    TRUE;
}

(6) Statistics how much data is in the current queue

int  get_total_number (const queue_node* pqueuenode)
{
    if (NULL = = pqueuenode) return
        0;

    Return pqueuenode->count;
}

(7) See how long the total length is when initializing in the queue

int  get_total_number (const queue_node* pqueuenode)
{
    if (NULL = = pqueuenode) return
        0;

    Return pqueuenode->length;
}
Loop queue. CPP: Defines the entry point for a console application. Loop queue//written by ZP1015//2015.10.21 #include "stdafx.h" #include <stdlib.h> #include <stdio.h> #includ
    E <string.h> struct Queue_node {int* pData;

int queuelenmax;//queue maximum length int head;/team header pointer int tail;//team tail pointer int queuecurlen;//queue element current number};

    struct queue_node* alloc_queue (int queue_max_size) {if (queue_max_size <= 0) return NULL;

    struct queue_node* pqueuenode = NULL;
    Pqueuenode = (queue_node*) malloc (sizeof (Queue_node));
    if (null = = Pqueuenode) {return null;

    memset (pqueuenode, 0, sizeof (struct queue_node));
    Pqueuenode->pdata = (int*) malloc (sizeof (int) * queue_max_size);
    if (NULL = = pqueuenode->pdata) {goto malloc_failed;
    } Pqueuenode->queuelenmax = queue_max_size;//Queue Length pqueuenode->head = 0;
    Pqueuenode->tail = 0;

    Pqueuenode->queuecurlen = 0;

return pqueuenode;
    Malloc_failed:free (Pqueuenode); Return NULL;

    int free_queue (struct queue_node* pqueuenode) {if (NULL = = Pqueuenode) return-1;
        if (NULL = = Pqueuenode->pdata) {free (pqueuenode); 
    return-1;
    Free (pqueuenode->pdata);

    Free (Pqueuenode);
return 0;

    int Queue_push (struct queue_node* pqueuenode, int value) {if (NULL = Pqueuenode) return-1;

    if (Pqueuenode->queuelenmax = = Pqueuenode->queuecurlen)//Judge is not overflow return-1;
    Pqueuenode->pdata[pqueuenode->tail] = value;  Pqueuenode->tail = (pqueuenode->tail + 1)% pqueuenode->queuelenmax;

    Team Tail pointer position Pqueuenode->queuecurlen + +;
return 0; int Queue_pop (struct queue_node* pqueuenode, int* value) {if (NULL = = Pqueuenode | |

    NULL = = value) return-1;

    if (0 = = Pqueuenode->queuecurlen) return-1;
    *value = pqueuenode->pdata[pqueuenode->head]; Pqueuenode->pdata[pqueuenode->head] = 0; The position being ejected is assigned a value of 0 Pqueuenode->queuecurleN--;
Pqueuenode->head = (pqueuenode->head + 1)% pqueuenode->queuelenmax;//Reposition team head pointer position return 0;

    int Get_queue_curlen (struct queue_node* pqueuenode) {if (NULL = = Pqueuenode) return-1;
Return pqueuenode->queuecurlen;

    int Get_queue_maxlen (struct queue_node* pqueuenode) {if (NULL = = Pqueuenode) return 0;
Return pqueuenode->queuelenmax; } void Print_queue_node (struct queue_node *pqueuenode) {/*1. The argument entered is incorrect */if (NULL = = Pqueuenode) {printf (" [%d] Pqueuenode is illegal!
        \ n ", __line__);
    Return
        /*2. The input chain stack is empty */if (0 = pqueuenode->queuecurlen) {printf ("[%d] Pqueuenode is empty!\n", __line__);
    return;
    } struct Queue_node *pqueuenodetemp = Pqueuenode;

    int count = 0;
        while (Count < Pqueuenode->queuecurlen) {printf ("%d", Pqueuenode->pdata[pqueuenode->head+count]);
    count++;;
printf ("\ n"); int main () {struct QUEUE_NODE *pqueuenode;
    Pqueuenode = Alloc_queue (20);
    int i = 0;
    for (i = 0;i<10;i++) {Queue_push (pqueuenode,i);
    } print_queue_node (Pqueuenode);
    int a = 0;
    Queue_pop (Pqueuenode,&a);

    Print_queue_node (Pqueuenode);

    Free_queue (Pqueuenode);
    GetChar ();

    GetChar ();
return 0;
 }
basic operations for OJ series loop queues
/****************************************************************************** Copyright (C), 2001-2011, SCUT. File Name:Version:Aut HOR:CREATED:2016/01/21 last Modified:Description:Function list:history:1.d ATE:2016/01/21 Author:Modification:Created File ***********************************************
    /#include <stdlib.h> #define MAXSIZE struct Strqueue {int queue[maxsize]; int head; * * Team head */int tail;  /* Team TAIL */int num;

/* Team Elements number */};
    /* Initialize queue, return 0 to indicate failure, return 1 for Success/bool Initqueue (struct Strqueue *s) {if (!s) returns 0;
    for (int i = 0;i<maxsize;i++) {S->queue[i] = 0;
    } s->head =-1;
    S->tail =-1;

    S->num = 0;
return 1; BOOL Enqueue (struct strqueue *s, int x)/* Enter queue, return 0 to indicate failure, return 1 to indicate success */{IF (!s) return 0;

    if (S->num = = MAXSIZE) return 0; if (s->head==-1| |
        S->tail==-1) {s->head = 0;
        S->tail = 0; S->queue[s->tail] = x;
        /* Assign Value * * S->num = 1;
    return 1; S->tail = (s->tail + 1)% maxsize;/* from team tail into queue * * * S->queue[s->tail] = x;

    /* Assigned value */S->num + +;
return 1; BOOL Dequeue (struct strqueue *s, int *x)/* out queue, return 0 for failure, return 1 for Success */{if (!s| |!
    x) return 0;

    if (S->num = = 0) return 0; *x = s->queue[s->head];

    /* Assigned value */s->queue[s->head] = 0;

    S->head = (s->head + 1)% maxsize;/* from head out queue * * * s->num-;
        if (s->num==0) {s->head =-1;
    S->tail =-1;

return 1;

    The int gethead (struct strqueue *s)/* Gets the queue header value/{if (!s) return-1;

    if (s->num==0) return-1;
    int head = 0;

    Head = s->queue[s->head];
return head; int GetTail (StrucT strqueue *s)/* Get the tail value of the queue/{if (!s) return 0;

    int tail = 0;

    if (s->num==0) return-1;

    tail=s->queue[s->tail];
return tail;

    int getqueuelenth (struct strqueue *s)/* Get Queue Length/{if (!s) return 0;
    int lenth = 0;

    Lenth = s->num;
return lenth;

    BOOL Search (struct strqueue *s, int x)/* Find x in the queue if there is a return of 1, otherwise return 0 */{if (!s) returns 0;
    int headtemp = s->head;
    int tailtemp = s->tail;
                /*1. The status of the team is full * */if (S->num = = MAXSIZE) {for (int i=0;i<maxsize;i++) {if (x==s->queue[i))
        return 1; } else {/*2. Queue dissatisfaction, from head to tail is the presence of elements of the */while (headtemp!=tailtemp) {if (x==s->queue[headtemp
            ] Return 1;
        headtemp= (headtemp + 1)% MAXSIZE;
} return 0;






 }

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.