0% found this document useful (0 votes)
25 views2 pages

Coada

This document defines functions for implementing a queue data structure using a circular buffer. It defines a queue struct with fields for storing elements, head and tail pointers, and current size. Functions are defined for enqueueing (adding) and dequeueing (removing) elements from the queue, as well as printing the current elements. The main function demonstrates using these by adding and removing elements from a sample queue.

Uploaded by

Balan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Coada

This document defines functions for implementing a queue data structure using a circular buffer. It defines a queue struct with fields for storing elements, head and tail pointers, and current size. Functions are defined for enqueueing (adding) and dequeueing (removing) elements from the queue, as well as printing the current elements. The main function demonstrates using these by adding and removing elements from a sample queue.

Uploaded by

Balan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>
#define capacity 10

typedef struct
{
int vec[capacity];
int size;
int head, tail;
}queue;

void enqueue(queue *v, int key)


{
if(v->size == capacity - 1) return;
v->vec[v->tail] = key;
v->tail = v->tail + 1;
if(v->tail == capacity) v->tail = 0;
v->size = v->size + 1;

int dequeue(queue* v)
{
if(v->size == 0) return 0;
int ret;
ret = v->vec[v->head];
v->head = v->head + 1;
if(v->head == capacity) v->head = 0;
v->size = v->size - 1;
return ret;
}

void print(queue* vect)


{
if(vect->size == 0) printf("lista e goala");
else
{
if(vect->head < vect->tail)
{
for(int i = vect->head; i < vect->tail; i++)
{
printf("%d ", vect->vec[i]);
}
}
else
{
for(int i = vect->head; i < capacity; i++)
{
printf("%d ", vect->vec[i]);
}

for(int i = 0; i < vect->tail; i++)


{
printf("%d ", vect->vec[i]);
}
}
}
printf("\n");
}

void main()
{
queue v;
v.head = 0;
v.tail = 0;
v.size = 0;

enqueue(&v, 1);
enqueue(&v, 2);
enqueue(&v, 3);
enqueue(&v, 4);
enqueue(&v, 5);
enqueue(&v, 6);
enqueue(&v, 7);
enqueue(&v, 8);
enqueue(&v, 9);
enqueue(&v, 10);
enqueue(&v, 11);

printf("Coada este:\n");
print(&v);

printf("Se sterg elementele:\n");


printf("%d ", dequeue(&v));
printf("%d ", dequeue(&v));
printf("%d ", dequeue(&v));
printf("%d ", dequeue(&v));

printf("\nNoua coada va fi\n");

print(&v);
}

You might also like