Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to initialize a queue in c

#include <stdio.h>

#define MAX_QUEUE_SIZE 100;

typedef struct queue {
    int queue[MAX_QUEUE_SIZE];
    int currSize;
    int head;
    int tail;
} queue;

void initQueue(queue *queue) {
    queue->currSize = 0;
    queue->pos = 0;
    queue->head = 0;
    queue->tail = 0;
}

void enqueue(queue *queue, int e) {
    if (isFull()) printf("Queue is full!");

    queue->queue[queue->tail] = e;
    queue->tail = (queue->tail + 1) % MAX_QUEUE_SIZE;
    q->currSize = q->currSize + 1;
}

int dequeue(queue *queue) {
    if (isEmpty()) printf("Queue is empty!");

    int toDequeue = queue->head;
    queue->head = (queue->head + 1) % MAX_QUEUE_SIZE;
    q->currSize = q->currSize - 1;

    return toDequeue;
}

int isEmpty(queue *queue) {
    return (queue->head == queue->tail);
}

int isFull(queue *queue) {
    return (currSize == MAX_QUEUE_SIZE);
}

int main() {
    /* how do I initialize the queue and enq/deq to test? */
    printf("Hello World!
");
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: system cpp 
Cpp :: search by value in map in stl/cpp 
Cpp :: bfs to detect cycle in undirected graph 
Cpp :: c++ math 
Cpp :: constructor syntax in c++ 
Cpp :: c++ for each loop 
Cpp :: stl in c++ 
Cpp :: comparator priority queue c++ 
Cpp :: install qpid broker in ubuntu/linux 
Cpp :: how to take full sentence in c++ 
Cpp :: how to change the value of a key in hashmp in c++ 
Cpp :: C++ wchar_t 
Cpp :: c++ remove all elements equal to 
Cpp :: cpp ignore warning in line 
Cpp :: how to write int variable c++ 
Cpp :: glfw error: the glfw library is not initialized 
Cpp :: c++ define constant in class header 
Cpp :: how to print a word in c++ 
Cpp :: c++ delete printed characters 
Cpp :: backtrack 
Cpp :: ifstream 
Cpp :: c++ class methods 
Cpp :: how to make a pointer point to a the last value in an array 
Cpp :: tower of hanoi 
Cpp :: << in C++ 
Cpp :: calling by reference c++ 
Cpp :: c++ map vector as keys 
Cpp :: how atan work c++ 
Cpp :: the amount of input is unknown 
Cpp :: input numbers to int c++ 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =