Using arrays to implement a double-ended queue

Source: Internet
Author: User
Tags arrays

#include <stdio.h>

#include <stdlib.h>

struct Queuenode

{

int front;

int rear;

int capacity;

int count;

Char *arrayitem;

};

typedef struct QUEUENODE* Queue;


void queueinital (int amaxeelement);

void Queuepushfront (char aelement);

Char Queuepopfront ();

void Quequeuepushrear (char aelement);

Char queuepoprear ();

void Queuefree ();


#include "DoubleQueue.h"

Static queue queue;

static char* Isempty = "\nqueue is empty";

static char* Isfull = "\nqueue is full";

void queueinital (int amaxeelement)

{

Queue = malloc (sizeof (*queue));

if (!queue)

{

printf ("Create QUEUE failed!!!!");

Return

}

queue->capacity = amaxeelement;

Queue->arrayitem = malloc (sizeof (char) *amaxeelement);

if (! ( Queue->arrayitem))

{

printf ("Create array failed!!!!");

Return

}

Queue->count = 0;

Queue->front = 0;

Queue->rear = aMaxEelement-1;

}

static int IsEmpty ()

{

return queue->count = = 0;

}

static int Isfull ()

{

return Queue->count = = queue->capacity;

}


static int Add (int aindex)

{

if (Aindex >= queue->capacity)

{

Aindex = 0;

}

return aindex;

}

static int minus (int aindex)

{

if (Aindex < 0)

{

Aindex = queue->capacity-1;

}

return aindex;

}

void Queuepushfront (char aelement)

{

if (Isfull ())

{

printf ("%s", isfull);

Return

}

queue->arrayitem[queue->front++] = aelement;

Queue->front = Add (Queue->front);

queue->count++;

}

Char Queuepopfront ()

{

if (IsEmpty ())

{

printf ("%s", Isempty);

return 0;

}

Queue->front = minus (--queue->front);

--queue->count;

Return queue->arrayitem[queue->front];

}

void Quequeuepushrear (char aelement)

{

if (Isfull ())

{

printf ("%s", isfull);

Return

}

queue->arrayitem[queue->rear--] = aelement;

Queue->rear = minus (queue->rear);

queue->count++;

}

Char Queuepoprear ()

{

if (IsEmpty ())

{

printf ("%s", Isempty);

return 0;

}

Queue->rear = Add (++queue->rear);

--queue->count;

Return queue->arrayitem[queue->rear];

}

void Queuefree ()

{

Free (Queue->arrayitem);

Free (queue);

}

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.