This is a function for doubling the circular queue capacity in C
(https://i.stack.imgur.com/xxhah.jpg)
Code source : Fundamentals of data structures in C
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int key;
} element;
element *queue;
void addq(element);
element deleteq(void);
void queueFull(void);
void displayq(void);
int capacity = 1;
int f, r;
void main() {
element e, temp;
int choice, done = 0;
f = 0;
r = 0;
while (!done) {
printf("1.Insert\t2.Delete\t3.Display\t4.Exit\n");
printf("Enter choice");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter element");
scanf("%d", &e.key);
addq(e);
break;
case 2:
temp = deleteq();
if (temp.key == -1)
printf("Queue Underflow\n");
else
printf("Queue element deleted : %d\n", temp.key);
break;
case 3:
displayq();
break;
case 4:
done = 1;
break;
default:
printf("Illegal Entry\n");
break;
}
}
}
void addq(element e) {
if (f == (r + 1) % capacity)
queueFull();
r = (r + 1) % capacity;
queue[r] = e;
}
void queueFull() {
int start;
element *newQueue;
newQueue = (element*) malloc(2 * capacity * sizeof(queue));
/*Copy from queue to newQueue*/
start = (f + 1) % capacity;
if (start < 2)
/*No wrap around*/
copy(queue + start, queue + start + capacity - 1, newQueue);
else {
copy(queue + start, queue + capacity, newQueue);
copy(queue + start, queue + r + 1, newQueue + capacity - start);
}
/*Switch to newqueue*/
f = 2 * capacity - 1;
r = capacity - 1;
capacity *= 2;
free(queue);
queue = newQueue;
}
element deleteq() {
element temp;
temp.key = -1;
if (f == r)
return temp;
else {
f = (f + 1) % capacity;
temp = queue[f];
return temp;
}
}
void displayq() {
int i;
i = f;
if (f == r)
printf("Queue empty\n");
else {
while (i != r) {
i = (i + 1) % capacity;
printf("%d", queue[i].key);
}
printf("\n");
}
}
When I compile the code, I get the error undefined reference to copy
.
Does anyone know in which library this function exists? Or is there any other way around?