shiv@ubuntu:~/ds/queue$ cat circularq.c
/*
* Interactive program to simulate Circular Queue using arrays
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_SIZE 5
#define FALSE 0
#define TRUE 1
/* Insert element into Queue */
int insert(int cq[], int e, int head, int *tail)
{
if((*tail + 1)%MAX_SIZE == head){
printf("Queue is full\n");
return FALSE;
}
cq[*tail]=e;
*tail = (*tail +1)% MAX_SIZE;
return TRUE;
}
/* Delete element from queue */
int del(int cq[], int *head, int tail)
{
if(tail == *head) {
printf("Queue is empty");
return FALSE;
}
printf("deleted %d\n", cq[*head]);
*head = (*head + 1)%MAX_SIZE;
return TRUE;
}
/* Display elements in the queue */
void display(int cq[], int h, int t)
{
int i=h;
printf("Queue elements are ..\n");
for (i=h; i != t; i=(i+1)%MAX_SIZE)
{
printf("%d\n", cq[i]);
}
}
/* main program to help testing */
/* end of the program*//*
* Interactive program to simulate Circular Queue using arrays
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_SIZE 5
#define FALSE 0
#define TRUE 1
/* Insert element into Queue */
int insert(int cq[], int e, int head, int *tail)
{
if((*tail + 1)%MAX_SIZE == head){
printf("Queue is full\n");
return FALSE;
}
cq[*tail]=e;
*tail = (*tail +1)% MAX_SIZE;
return TRUE;
}
/* Delete element from queue */
int del(int cq[], int *head, int tail)
{
if(tail == *head) {
printf("Queue is empty");
return FALSE;
}
printf("deleted %d\n", cq[*head]);
*head = (*head + 1)%MAX_SIZE;
return TRUE;
}
/* Display elements in the queue */
void display(int cq[], int h, int t)
{
int i=h;
printf("Queue elements are ..\n");
for (i=h; i != t; i=(i+1)%MAX_SIZE)
{
printf("%d\n", cq[i]);
}
}
/* main program to help testing */
int main()
{
int ch, e;
int cq[MAX_SIZE];
int head = 0, tail=0;
while(1){
system("clear");
printf("1 insert\n2 delete\n3 display\n4 exit\n");
printf("Enter choice\n");
scanf("%d", &ch);
switch(ch) {
case 1:
printf("Enter element to insert\n");
scanf("%d", &e);
if(insert(cq, e, head, &tail)){
printf("Successfuly inserted \n");
} else {
printf("failure to insert\n");
}
break;
case 2:
if(del(cq, &head, tail)){
printf("Deleted successfully ..\n");
} else {
printf("Can't deleted more \n");
}
break;
case 3: display(cq, head, tail); break;
case 4: exit(0);
default: printf("Invalid choice ..\n");
}
getchar();
getchar();
}
}
Comments
Post a Comment