Dec 21, 2011

C Program for Simple DSC order Priority QUEUE Implementation

Data structures using C,Priority QUEUE is a abstract data type in which the objects are inserted with respect to certain priority. In this program, we created the simple descending order priority queue, here items are inserted in descending order. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/
#define SIZE 5            /* Size of Queue */
int PQ[SIZE],f=0,r=-1;       /* Global declarations */

PQinsert(int elem)
{
 int i;       /* Function for Insert operation */
 if( Qfull()) printf("\n\n Overflow!!!!\n\n");
 else
 {
  i=r;
  ++r;
  while(PQ[i] <= elem && i >= 0) /* Find location for new elem */
  {
   PQ[i+1]=PQ[i];
   i--;
  }
  PQ[i+1]=elem;
 }
}

int PQdelete()
{                      /* Function for Delete operation */
 int elem;
 if(Qempty()){ printf("\n\nUnderflow!!!!\n\n");
 return(-1); }
 else
 {
  elem=PQ[f];
  f=f+1;
  return(elem);
 }
}

int Qfull()
{                     /* Function to Check Queue Full */
 if(r==SIZE-1) return 1;
 return 0;
}

int Qempty()
{                    /* Function to Check Queue Empty */
 if(f > r) return 1;
 return 0;
}

display()
{                  /* Function to display status of Queue */
 int i;
 if(Qempty()) printf(" \n Empty Queue\n");
 else
 {
  printf("Front->");
  for(i=f;i<=r;i++)
   printf("%d ",PQ[i]);
  printf("<-Rear");
 }
}

main()
{                         /* Main Program */
 int opn,elem;
 do
 {
  clrscr();
  printf("\n ### Priority Queue Operations(DSC order) ### \n\n");
  printf("\n Press 1-Insert, 2-Delete,3-Display,4-Exit\n");
  printf("\n Your option ? ");
  scanf("%d",&opn);
  switch(opn)
  {
  case 1: printf("\n\nRead the element to be Inserted ?");
  scanf("%d",&elem);
  PQinsert(elem); break;
  case 2: elem=PQdelete();
  if( elem != -1)
   printf("\n\nDeleted Element is %d \n",elem);
  break;
  case 3: printf("\n\nStatus of Queue\n\n");
  display(); break;
  case 4: printf("\n\n Terminating \n\n"); break;
  default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
  break;
  }
  printf("\n\n\n\n  Press a Key to Continue . . . ");
  getch();
 }while(opn != 4);
}


Read more Similar C Programs
Data Structures

Learn C Programming
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!
To browse more C Programs visit this link

K & R C Programs Exercise 2-3.

K and R C, Solution to Exercise 2-3:
C Function htoi() which converts a string of hexa decimal digits into its equivalent integer value. K and R C Program. Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#define YES 1
#define NO  0
//HTOI: CONVERT HEXADECIMAL STRING S TO INTEGER
int htoi(char s[])
{
 int hexdigit, i, inhex, n,i=0;
 if(s[i]=='0'){
  ++i;
  if(s[i]=='x' || s[i]=='x')
   ++i;
 }
 n=0;
 inhex=YES;
 for(;inhex==YES;++i){
  if(s[i]>='0' && s[i]<='9')
   hexdigit=s[i]-'0';
  else if(s[i]>='a' && s[i]<='f')
   hexdigit=s[i]-'a'+10;
  else if (s[i]>='A' && s[i]<='F')
   hexdigit=s[i]-'A'+10;
  else
   inhex=NO;
  if(inhex==YES)
   n=16*n+hexdigit;
 }
 return n;
}


Read more Similar C Programs
C Basic

K and R C Programs Exercise
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!
To browse more C Programs visit this link

K & R C Programs Exercise 2-2.

K and R C, Solution to Exercise 2-2:
C Program to demonstrate for and while loops. K and R C Program. Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. Read more about C Programming Language .
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

/*Write the equivalent to the below for loop without using && or ||.*/

// Original
 
 for(i=0; i<lim-1 && (c=getchar()) != '\n' && c != EOF; ++i)
  {
   s[i] = c;
  }
// Equivalent:
 

 while (i < (lim - 1))
 {
  c = getchar();

  if (c == EOF)
   break;
  else if (c == '\n')
   break;

  s[i++] = c;
 }

Read more Similar C Programs
C Basic

K and R C Programs Exercise
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!
To browse more C Programs visit this link

K & R C Programs Exercise 2-1.

K and R C, Solution to Exercise 2-1:
C program to determine the ranges of char, short, int and long variables, both signed and unsigned, by printing appropriate values from standard headers and by direct computation.the ANSIstandard for C specifies that ranges be defined in <limits.h>. K and R C Program. Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. Read more about C Programming Language .
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <stdio.h>
#include <limits.h>

int 
main ()
{
 printf("Range of Char %d\n", CHAR_BIT);
 printf("Range of Char Max %d\n", CHAR_MAX);
 printf("Range of Char Min %d\n", CHAR_MIN);
 printf("Range of int min %d\n", INT_MIN);
 printf("Range of int max %d\n", INT_MAX);
 printf("Range of long min %ld\n", LONG_MIN);       
 printf("Range of long max %ld\n", LONG_MAX);       
 printf("Range of short min %d\n", SHRT_MIN);
 printf("Range of short max %d\n", SHRT_MAX);
 printf("Range of unsigned char %u\n", UCHAR_MAX);  
 printf("Range of unsigned long %lu\n", ULONG_MAX); 
 printf("Range of unsigned int %u\n", UINT_MAX);    
 printf("Range of unsigned short %u\n", USHRT_MAX);

 return 0;
}

Read more Similar C Programs
C Basic

K and R C Programs Exercise
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!
To browse more C Programs visit this link

K & R C Programs Exercise 1-24.

K and R C, Solution to Exercise 1-24:
C program for rudimentary syntax errors like unbalanced parentheses, brackets,quotes,and braces. This program is hard if we do it in full generality. K and R C Program Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. Read more about C Programming Language .
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <stdio.h>

#define MAXLINE 1000 /* max input line size */
char line[MAXLINE]; /*current input line*/

int getline(void); /* taken from the KnR book. */

int main() {
 int len = 0;
 int t = 0;
 int brace = 0, parenthesis = 0, brack = 0;
 int s_quote = 1, d_quote = 1;

 while ((len = getline()) > 0) {
  t = 0;
  while (t < len) {
   if (line[t] == '{') {
    brace++;
   }
   if (line[t] == '}') {
    brace--;
   }
   if (line[t] == '[') {
    brack++;
   }
   if (line[t] == ']') {
    brack--;
   }
   if (line[t] == '(') {
    parenthesis++;
   }
   if (line[t] == ')') {
    parenthesis--;
   }
   if (line[t] == '\'') {
    s_quote *= -1;
   }
   if (line[t] == '"') {
    d_quote *= -1;
   }
   t++;
  }
 }
 if (d_quote != 1)
  printf("Mismatching double quote mark\n");
 if (s_quote != 1)
  printf("Mismatching single quote mark\n");
 if (parenthesis != 0)
  printf("Mismatching parenthesis\n");
 if (brace != 0)
  printf("Mismatching braces\n");
 if (brack != 0)
  printf("Mismatching bracket mark\n");
 if (brack == 0 && brace == 0 && parenthesis == 0 && s_quote == 1 && d_quote
   == 1)
  printf("Syntax appears to be correct.\n");
 return 0;
}

/* getline function */
int getline(void) {
 int c, i;
 extern char line[];

 for (i = 0; i < MAXLINE - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
  line[i] = c;
 if (c == '\n') {
  line[i] = c;
  ++i;
 }
 line[i] = '\0';
 return i;

}

Read more Similar C Programs
C Basic

K and R C Programs Exercise
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!
To browse more C Programs visit this link