Dec 14, 2011

C Program for Simple ASC 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 ascending order priority queue, here items are inserted in ascending 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(ASC 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

C Program to implementing two Stacks on Single Array.

Data structures using C, Stack is a data structure in which the objects are arranged in a non linear order. In stack, elements are aded or deleted from only one end, i.e. top of the stack. In this program, we implement the two stacks using the single array. 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 10            /* Size of Stack */
int s[SIZE],top[3]={0,-1,SIZE};
       /* Global declarations */
push(int elem,int stno)
 {
  int pos;      /* Function for PUSH operation */
  if( Sfull()) printf("\n\n Overflow!!!!\n\n");
  else
   {
    if(stno==1) pos= ++top[stno];
     else pos=--top[stno];
    s[pos]=elem;
    }
  }


int pop(int stno)
 {                      /* Function for POP operation */
  int elem,pos;
  if(Sempty(stno)){ printf("\n\nUnderflow!!!!\n\n");
  return(-1); }
  else
   {
    pos=top[stno];
    elem=s[pos];
    if(stno == 1)top[stno]--;
    else
    top[stno]++;
    return(elem);
   }
 }

 int Sfull()
  {                     /* Function to Check Stack Full */
    if(top[1] == top[2]-1) return 1;
    return 0;
  }

 int Sempty(stno)
  {
      /* Function to Check Stack Empty */
    switch(stno)
    {
    case 1: if(top[1] == -1) return 1; else return 0;
    case 2: if(top[2] == SIZE) return 1;else return 0;
    }
  }

  display(int stno)
   {                  /* Function to display status of Stack */
    int i;
    if(Sempty(stno)) printf(" \n Empty Stack\n");
    else
     {
     if(stno == 1)
     {
      for(i=0;i<=top[stno];i++)
 printf("%d\n",s[i]);
      printf("^Top");
     }
     else
     {
     for(i=SIZE-1;i>=top[stno];i--)
 printf("%d\n",s[i]);
      printf("^Top");
     }
   }
   }

  main()
  {                         /* Main Program */
   int opn,elem,stno;
   do
    {
     clrscr();
     printf("\n ### Stack Operations ### \n\n");
     printf("\n Stack Number (1,2): ");
     scanf("%d",&stno);
     printf("\n Press 1-Push, 2-Pop,3-Display,4-Exit\n");
     printf("\n Your option ? ");
     scanf("%d",&opn);

     switch(opn)
      {
       case 1: printf("\n\nRead the element to be pushed ?");
        scanf("%d",&elem);
        push(elem,stno); break;
       case 2: elem=pop(stno);
        if( elem != -1)
    printf("\n\nPopped Element is %d \n",elem);
        break;
       case 3: printf("\n\nStatus of Stack %d \n\n",stno);
        display(stno); 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