Skip to main content

Interactive C program to Simulate Queue operations on Circular Queue using arrays

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 */
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();
 }

}
/* end of the program*/

Comments

Popular posts from this blog

C Program to Simulate Linux Cat command

/*  * C program to Simulate Implementation of  Linux *cat* command  * This program will not handle redirection  * you can try to improve this program to handle redirection of file contents  */ #include<stdio.h> #include<string.h> #define MAX_FILE_NAME_CHARS 255 int main(int argc, char *argv[]) {  FILE *fp;  char file_name[MAX_FILE_NAME_CHARS], ch;  int i;  /*   * after creating a.out, rename it as mycat for our own cat command   * and it usage is same as standard cat command   */  if(argc<1){     printf("Usage mycat <filename> \n");     return 0;  }  /*   * cat handles more than one file   * so we need this loop to handle all the files provided   * on command line   */  for(i=1; i<=argc;i++){     /* no need of copy but for understanding purpose, i have created      * str...

simple program to create Orphan process

shiv@ubuntu:~/ds/unix$ cat  orp.c /*  * Program to create orphan process @ Linux  * getpid() gives process PID and   * getppid() gives process's parent ID   * here main() process ID is parent id is current shells PID  * once process becomes orphan it is adopted by init process(it's PID is 1)  */   #include<stdio.h> #include<unistd.h> int main() {  pid_t p; /* create child process */  p=fork();  if(p==0) {     /* fork() returns Zero to child */     sleep(10);  }  printf("The child process pid is %d parent pid %d\n", getpid(), getppid()); /*parent/child waits for 20 secs and exits*/  sleep(20);  printf("\nProcess %d is done its Parent pid %d...\n", getpid(), getppid());  return 0; } O/p shiv@ubuntu:~/ds/unix$ ./a.out The child process pid is 2575 parent pid 1922 The child process pid is 2576 parent pid 2575 Process 2575 is done it...

Simple program to illustrate variadic macros In C

/*  * C99 has  support of variable arguments in macros statement  * (called variadic macro) just like variable argument function, Macros also   * can be of variable number of augments  * This program illustrate variadic macros  */ #include <stdio.h>   /*   * Simple Macro definition   * which allows to print whatever the way you want   *  in Macro definition "..." indicates variadic macro, OP() definition is    * mixture of named args and variable number of aguments, it is possible   * to have macro of complete variable number of args   * like DISPLAY in below example   */ #define OP(format, exp, ...)  printf(format, exp, ##__VA_ARGS__) #define DISPLAY(...)    printf(__VA_ARGS__) int main() {  int a=3, b=4, c=5;  OP("%s %d\n", "Result after add", a+b);  OP("%s %d %s %d\n", "Result after add", a+b+c, "Result after sub", a-b);  OP("%s\n", "This is tes...