The Programming Project: algorithm
Showing posts with label algorithm. Show all posts
Showing posts with label algorithm. Show all posts

Tuesday, September 24, 2013

Combination in Lexicographical Order




#include<stdio.h>
#include<malloc.h>
#include<conio.h>
int main()
{
char alpha[]={'?','A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int n,i,pos,r,tmp,j,flag=1;
long int tot=0;
char *combination;
printf("\n Enter the number of objects (max value is 26):");
scanf("%d",&n);
printf("\n Enter the value of r ( 1<= r <= n):");
scanf("%d",&r);
combination =(char *)malloc(n*sizeof(char));
for(i=1;i<=n;i++)
combination[i]=alpha[i];
printf("\n The combinations are (is):\n");
printf("\t\t      ");
for(i=1;i<=r;i++)
printf("%c",combination[i]);
tot=1;
while(flag)
{
for(i=r;i>=1;i--)
{
if(combination[i]-64==n-r+i)
{
}
else
{
pos=i;
break;
}
}
if(i==0)
{
flag=0;
break;
}
for(j=1;j<=26;j++)
if(combination[pos]==alpha[j])
break;
combination[pos]=alpha[j+1];
for(i=pos+1;i<=r;i++)
combination[i]=alpha[(combination[i-1]-64)+1];
printf("\n");
printf("\t\t      ");
for(i=1;i<=r;i++)
printf("%c",combination[i]);
tot++;
}
printf("\n Total number of combination is: %ld",tot);
return 0;
}


Friday, September 20, 2013

Permutation in Lexicographical Order

This C program prints the permutation of n distinct objects (0 < n <= 26 )  in Lexicographical Order.



#include<stdio.h>
#include<malloc.h>
void sort(char *permutation,int n,int pos);
void replace_min (char *permutation,int n,int pos);
int main()
{
char alpha[]={'A','B','C','D','E','F','G','H','I','J','K','L','M',
          'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int n,i,flag=1,pos;
long int tot=0;
char *permutation;
printf("\n Enter the numbr of objects (max values is 26):");
scanf("%d",&n);
permutation =(char *)malloc(n*sizeof(char));
for(i=0;i<n;i++)
    permutation[i]=alpha[i];
printf("\n The permutations are:\n");
printf("\t\t      ");
for(i=0;i<n;i++)
    printf("%c",permutation[i]);
    tot=1;
while(flag)
    {
    pos=-1;
    for(i=n-1;i>=1;i--)
        {
        if(permutation[i-1]<permutation[i])
             {
             pos=i-1;
             flag=1;
             break;
             }
        }
        if(pos==-1)
            {
            flag=0;
            break;
            }
    replace_min(permutation,n,pos);
    sort(permutation,n,pos);
   printf("\n");
    printf("\t\t      ");
    for(i=0;i<n;i++)
        printf("%c",permutation[i]);
    //tot++;
    //getche();
    }
//printf("\n Total number of permutation is: %ld",tot);
return 0;
}
void sort(char *permutation,int n,int pos)
{
    int i,j;
   char temp='A';
    for(i=pos+1;i<n-1;i++)
        {
        for(j=pos+1;j<n-1;j++)
            {
            if(permutation[j]>permutation[j+1])
                {
                temp=permutation[j+1];
                permutation[j+1]=permutation[j];
                permutation[j]=temp;
                }
            }
        }
  return;
}
void replace_min(char *permutation,int n,int pos)
{
  char min='Z',temp;
  int k,min_pos=0;
  for(k=pos+1;k<n;k++)
    {
        if(permutation[k]>permutation[pos])
            {
            if(permutation[k]<min)
                {
                min=permutation[k];
                min_pos=k;
                }
            }
    }
    temp=permutation[min_pos];
    permutation[min_pos]=permutation[pos];
    permutation[pos]=temp;
    return;
}

Monday, September 16, 2013

Union of Sets


This C program finds the Union of two Sets (atleast one of is non-empty)
#Task- Apply recursion to the program below to find Union of finite number of sets

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void union_set(int *s1,int*s2,int n,int m);
int main()
{
int n,m,i;
int *s1,*s2;
printf("\n Enter the number of elements in the sets:");
scanf("%d %d",&n,&m);
s1=(int *)malloc(n*sizeof(int));
s2=(int *)malloc(m*sizeof(int));
printf("\n Enter the elements of the set 1:");
for(i=0;i<n;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s1[i]);
}
printf("\n Enter the elements of the set 2:");
for(i=0;i<m;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s2[i]);
}
union_set(s1,s2,n,m);
return 0;
}
void union_set(int *s1,int*s2,int n,int m)
{
int *s,i,j,k;
int flag=0;
s=(int *)malloc((m+n)*sizeof(int));
for(i=0;i<n;i++)
s[i]=s1[i];
for(j=0;j<m;j++)
{
for(k=0;k<n;k++)
{
if(s2[j]==s[k])
{
flag=1;
break;
}
}

if(flag==1)
{
flag=0;
continue;
         }
else
      if(flag==0)
{
s[i]=s2[j];
i++;
flag=0;
}
}
  printf("\n Union of the entered sets is:");
  printf("\n { ");
  for(j=0;j<i-1;j++)
printf(" %d,",s[j]);
  printf(" %d }\n ",s[i-1]);



}

Thursday, August 1, 2013

Uniform Symbol Table

A program to create a Terminal Table on the basis of the subsets of tokens
{ "if","for","while","int","float","char","+","-","*","/","=","<",">","{",",","(",")",";","?"} . Given a C statement the system program should create and display a uniform table that consists of full list of valid tokens. There must be two entries for each token in the Symbol Table, namely class and index (that is a pointer to a specific location of an appropriate table where the description of the token can be found. The index can be found to any one of the following tables: Identifier table, Literal Table and Terminal Table depending on the type of token. Each  of these three table would have its own relevant fields.

#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
int tokencount(char *str);
void  classification(char iden[],int *key,int *sym,int *ope,int *lit,int *ide,int *pkey,int *psym,int *pope);
void print_table(int,int,int);
#define TRUE 1
#define FALSE 0
typedef struct
    {
    int indx;
    char tok[20];
    int pos;
    }Terminal_table;
typedef struct
    {
    int indx;
    char tok[20];
    char t_nme[20];
    int pos;
    }Uniform_sym_table;
typedef struct
    {
    int indx;
    char lit[20];
    int pos;
    }Literal;
typedef struct
    {
    int indx;
    char iden[20];
    int pos;
    }Identifier;
Terminal_table *TT;
Uniform_sym_table *UST;
Literal *LT;
Identifier *IDE;
int main(void)
{
char *keyword1[]={ "if","for","while","int","float","char","+","-","*","/","=","<",">",",","(",")",";","?"};
char *str,ar[80],dig[2],sp[20];
char *token;
int FLAG=FALSE,count=0,n,len,i=0,tc=0,p=0,q=0,r=0,s=0,t=0,j=0;
int key=FALSE,sym=FALSE,ope=FALSE,lit=FALSE,ide=FALSE,lit_count=0,FLAG1=FALSE,iden_count=0;
int pkey,psym,pope;
printf("\n Enter the maximum length of the statement:");
scanf(" %d", &n);
str=(char *)malloc( (n+1)*sizeof(char));
fflush(stdin);
printf("\n Enter the C statement:");
scanf("%[^\n]",str);
len=strlen(str);
if(len>n)
{
printf("\n Length of the statement is greater than the input length:");
exit(1);
}
token=str;
tc=tokencount(str);
UST=(Uniform_sym_table *)malloc(tc*sizeof(Uniform_sym_table));
TT=(Terminal_table *)malloc(20*sizeof(Terminal_table));
LT=(Literal *)malloc(tc*sizeof(Literal));
IDE=(Identifier *)malloc(tc*sizeof(Identifier));
for(i=0;i<18;i++)
    {
    strcpy(TT[i].tok,keyword1[i]);
    TT[i].indx=i+1;
    TT[i].pos=i+1;
    }
tc=0;
while(1)
         {
         while(*token!= ' ')
         {
         if(*token== '\0')
            {
            FLAG=TRUE;
            break;
            }
    ar[count++]=*token;
    token++;
    }
    ar[count]='\0';
    classification(ar,&key,&sym,&ope,&lit,&ide,&pkey,&psym,&pope);
   if(key)
        {
        UST[tc].indx=tc+1;
        strcpy(UST[tc].tok,ar);
        strcpy(UST[tc].t_nme,"TRM");
        for(i=0;i<19;i++)
            {
            if((strcmp(TT[i].tok,ar))==0)
                break;
            }
        UST[tc].pos=TT[i].pos;
        tc++;
        }
    if(sym)
        {
        UST[tc].indx=tc+1;
        strcpy(UST[tc].tok,ar);
        strcpy(UST[tc].t_nme,"TRM");
        for(i=0;i<19;i++)
            {
            if((strcmp(TT[i].tok,ar))==0)
                break;
            }
        UST[tc].pos=TT[i].pos;
        tc++;
      }
    if(ope)
        {
      UST[tc].indx=tc+1;
        strcpy(UST[tc].tok,ar);
        strcpy(UST[tc].t_nme,"TRM");
        for(i=0;i<19;i++)
            {
            if((strcmp(TT[i].tok,ar))==0)
                break;
            }
        UST[tc].pos=TT[i].pos;
        tc++;
        }
    if(lit)
        {
        UST[tc].indx=tc+1;
        for(i=0;i<p;i++)
            {
            if((strcmp(LT[i].lit,ar))==0)
                {
                FLAG1=TRUE;
                s=i;
                break;
                }
            }
        if(FLAG1==0)
            {
            strcpy(UST[tc].tok,ar);
            strcpy(UST[tc].t_nme,"LTR");
            LT[p].indx=p+1;
            strcpy(LT[p].lit,ar);
            LT[p].pos=p+1;
            UST[tc].pos=LT[p].pos;
            p++;
            lit_count++;
            }
        else
        {
        strcpy(UST[tc].tok,ar);
        strcpy(UST[tc].t_nme,"LTR");
        UST[tc].pos=LT[s].pos;
        }
        tc++;
        }
    if(ide)
        {
        
      UST[tc].indx=tc+1;
        for(i=0;i<q;i++)
            {
            if((strcmp(IDE[i].iden,ar))==0)
                {
                FLAG1=TRUE;
                r=i;
                break;
                }
            }
        if(FLAG1==0)
            {
            strcpy(UST[tc].tok,ar);
            strcpy(UST[tc].t_nme,"IDE");
            IDE[q].indx=q+1;
            strcpy(IDE[q].iden,ar);
            IDE[q].pos=q+1;
            UST[tc].pos=IDE[q].pos;
            q++;
            iden_count++;
            }
        else
        {
        strcpy(UST[tc].tok,ar);
        strcpy(UST[tc].t_nme,"IDE");
        UST[tc].pos=IDE[r].pos;
        }
        tc++;

        }
/*    if(!key && !sym && !ope && !lit && !ide)
        {
        printf("\n Invalid tokens: %s ", ar);
      }*/
    if(FLAG==TRUE)
         break;
    while(*token==' ')
    {
         token++;
         if(*token=='\0')
           {
            FLAG=TRUE;
            break;
         }
      }
    if(FLAG==TRUE)
        break;
    count=0;
   FLAG1=FALSE;
    key=FALSE;
    sym=FALSE;
    ope=FALSE;
    lit=FALSE;
    ide=FALSE;
      }
print_table(tc,lit_count,iden_count);
printf("\n Enter a key");
getche();
return 0;
}
void print_table(int tc, int lit_count, int iden_count)
{
int i;
printf("\n");
printf("\n\t Uniform Symbol Table");
printf("\n---------------------------------------------------------");
printf("\n #Index\t\tTokens\t\tClass\t\tPosition");
printf("\n---------------------------------------------------------");
for(i=0;i<tc;i++)
    {
    printf("\n%d\t\t%s\t\t%s\t\t%d",UST[i].indx,UST[i].tok,UST[i].t_nme,UST[i].pos);
    }
printf("\n\tTerminal Table\n");
printf("---------------------------------");
printf("\nIndex\tKeywords\tPosition\n");
printf("---------------------------------");
for(i=0;i<18;i++)
{
printf("\n#%d\t%s\t %d",TT[i].indx,TT[i].tok,TT[i].pos);
}
printf("\n\n\n");
printf("\n\t Literal Table");
printf("\n---------------------------------------------------------");
printf("\n #Index\t\tTokens\t\tPosition");
printf("\n---------------------------------------------------------");
for(i=0;i<lit_count;i++)
    {
    printf("\n%d\t\t%s\t\t%d",LT[i].indx,LT[i].lit,LT[i].pos);
    }
printf("\n\n\n");
printf("\n\t Identifier Table");
printf("\n---------------------------------------------------------");
printf("\n #Index\t\tTokens\t\tPosition");
printf("\n---------------------------------------------------------");
for(i=0;i<iden_count;i++)
    {
    printf("\n%d\t\t%s\t\t%d",IDE[i].indx,IDE[i].iden,IDE[i].pos);
    }
return;
}

int tokencount(char *str)
{
char *token,ar[30];
int tc=0,count=0,FLAG=FALSE;
token=str;
while(1)
       {
       while(*token!= ' ')
       {
       if(*token== '\0')
         {
         FLAG=TRUE;
         break;
         }
    ar[count++]=*token;
    token++;
    }
    ar[count]='\0';
    tc++;
    if(FLAG==TRUE)
       break;
         while(*token== ' ')
       {
       token++;
       if(*token== '\0')
           {
            FLAG=TRUE;
              break;
        }
            
    }
    if(FLAG==TRUE)
    break;
    count=0;
    }
    return (tc);
}
void classification( char ar[],int *key,int *sym,int *ope,int *lit,int *ide,int *pkey,int *psym,int *pope)
{
int  FLAG=FALSE,len,count=0,flag=TRUE,numb=0,d,t=0,m=0;
char *keyword[]={ "if","for","while","int","float","char"};
char *operatr[]={ "+","-","*","/","=","<",">"};
char *symbols[]={ ",","(",")",";","?"};
int i=0;
len=strlen(ar);
for(i=0;i<len;i++)
    if(ar[i]==' ')
    t++;
if(t==len)
    return;
if(len>8)
FLAG=FALSE;
for(i=0;i<=5;i++)
{
    if(strcmp(keyword[i],ar)==0)
    {
  //      printf("\n%s\tis a Keyword:",ar);
        FLAG=TRUE;
        flag=FALSE;
        *key=TRUE;
      *pkey=i;
    }

}
i=0;
for(i=0;i<=6;i++)
    {
    if(strcmp(operatr[i],ar)==0)
        {
     //    printf("\n%s\tis a Operator:",ar);
        FLAG=TRUE;
        flag=FALSE;
        *ope=TRUE;
      *pope=i;
        }
    }
i=0;
for(i=0;i<5;i++)
    {
    if(strcmp(symbols[i],ar)==0)
        {
     //    printf("\n%s\tis a Symbol:",ar);
        FLAG=TRUE;
        flag=FALSE;
        *sym=TRUE;
      *psym=i;
        }
    }
if ( ((65 <= ar[0] && ar[0] <= 93 )|| ( 97 <= ar[0] && ar[0] <= 122)) && len <= 8 && FLAG==FALSE)
    {
  //    printf("\n%s\tis an Identifier:",ar);
   for(i=1;i<strlen(ar);i++)
        {
        if( (65 <= ar[i] && ar[i] <= 93 ) ||  ( 97 <= ar[i] && ar[i] <= 122)  || ( 48 <= ar[i] && ar[i] <= 57) || ar[i]==95 )
            {
             m++;
            }

        }
    if(m==strlen(ar)-1)
    {
  //    printf("\n%s\tis an Identifier:\n",ar);
    FLAG=TRUE;
    flag=FALSE;
    *ide=TRUE;
    }
  //    FLAG=TRUE;
  //    flag=FALSE;
  //    *ide=TRUE;
    }
for(i=0;i<len;i++)
    {
    if(48 <= ar[i] && ar[i] <=57 )
    count++;
    else
        continue;
    }
if(count==len && flag==TRUE && FLAG==FALSE)

    {
    numb=atoi(ar);
    if(numb==0 || numb >999)
     {printf("\n Invalid token: %s ", ar);}//printf("%s\tis not a valid Constant:",ar);
    else
    {*lit=TRUE;}//printf("\n%s\tis a Constant:",ar);
    FLAG=TRUE;

}
i=0;
if( FLAG==FALSE)
    {
    if(ar[0]=='-' || (48 <= ar[0] && ar[0] <=57))
    {printf("\n Invalid token: %s ", ar);}//printf("%s\tis not a valid Constant ",ar);
    else
    {printf("\n Invalid token: %s ", ar);}//printf("\n%s\tis an INVALID TOKEN:",ar);
    }
//printf("\n");
return;
}

Saturday, July 20, 2013

Minimal Spanning Tree~Prims Alogorithm

Minimal Spanning Tree by Prims Alogorithm


#include<stdio.h>
int main()
{
float graph[11][11],lcost[11],sum=0.0,min;
int close[11],i,j,k,n;
printf("\n Enter the number of vertices:\n");
do
   {
   scanf("%d",&n);
   if(n<=10)
       break;
   printf("\n Number of vertices cannot exceed 10:");
    }while(n>10);
for(i=1;i<=n;i++)
    graph[i][i]=0.0;
printf("\n Enter 999 if there is no edge between selected vertices:\n");
printf("\n Enter the weighted matrix:\n");
for(i=1;i<n;i++)
    for(j=i+1;j<=n;j++)
        {
        printf("\n Enter the cost of edge %d<--->%d : ",i,j);
        scanf("%f",&graph[i][j]);
        graph[j][i]=graph[i][j];
        }
printf("\nThe weighted matrix is:\n");
for(i=1;i<=n;i++)
    {
    for(j=1;j<=n;j++)
        {
        printf("%f\t  ",graph[i][j]);
        }
    printf("\n");
    }
for(i=2;i<=n;i++)
    {
    lcost[i]=graph[1][i];
    close[i]=1;
    }
printf("\n Minimum cost spanning tree by Prim's Algorithm are:\n");
    for(i=2;i<=n;i++)
    {
    min=lcost[2];
    k=2;
    for(j=3;j<=n;j++)
        if(lcost[j]<min)
        {
            min=lcost[j];
            k=j;
        }
    printf("Min=%f\n",min);
    sum +=min;
    printf(" %d<--->%d\n",k,close[k]);
    lcost[k]=1000.0;
    for(j=2;j<=n;j++)
        if((graph[k][j]<lcost[j]) && (lcost[j]<1000.0))
            {
            lcost[j]=graph[k][j];
            close[j]=k;
            }
    }
printf("\n Minimum cost is: %f\n",sum);
return 0;
}
   



Friday, March 29, 2013

C Program #19


/* Dynamic Implementation of queue and few basic operations!*/

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define TRUE 1
#define FALSE 0
struct queue{
                                                int items;
                                                struct queue *next;
                                                };
typedef struct queue node;
struct stack{
                        int top;
                        int items[MAXSIZE];
                        };
struct stack stk;
struct queue p,q,temp;
int pop(struct stack *);
int remove(node **);
int Isempty(node *);
void push(struct stack *,int);
int isempty_st(struct stack *);
void insert(node *,int);
void reverse_queue(node **);
void display_front_rear(node **);
void display_rear_front(node **);
void append(node *,node *);
void replace(node**,int,int,node **);
void display_front_rear_unchanged(node **,node **);
void display_rear_front_unchanged(node **,node **);
int Isequal(node **,node**,node**,node**);
int main()
{
            int choice,x,ins,e;
            stk.top=-1;
            node *rootp,*rootq,*tqueue,*rqueue;
            rootp=(node *)malloc(sizeof(node));
            rootp->next=NULL;
            rootq=(node *)malloc(sizeof(node));
            rootq->next=NULL;
            tqueue=(node *)malloc(sizeof(node));
            tqueue->next=NULL;
            rqueue=(node *)malloc(sizeof(node));
            rqueue->next=NULL;
            do
                        {
                        printf("\n Press 1 to add elements in Queue:");
                        printf("\n Press 2 to remove elements from Queue:");
                        printf("\n Press 3 to append queue Q at the end of queue P:");
                        printf("\n Press 4 to print elements of queue P from front to end with P becoming empty:");
                        printf("\n Press 5 to print elements of queue P from rear to front with P becoming empty:");
                        printf("\n Press 6 to print elements of queue P from front to end with P remaining same:");
                        printf("\n Press 7 to print elements of queue P from rear to front with P remaining same:");
                        printf("\n Press 8 to reverse the queue P:");
                        printf("\n Press 9 to replace a specific element of a queue:");
                        printf("\n Press 10 to check whether the queues P and Q are equal or not:");
                        printf("\n Type 11 to exit:");
                        scanf("%d",&choice);
                        switch(choice)
                                    {
                                    case 1:
                                                            printf("\n Enter 1 to insert in queue P, 2 to insert in queue Q:");
                                                            scanf("%d",&x);
                                                                        switch(x)
                                                                                    {
                                                                                    case 1:
                                                                                    printf("\n Enter the element to be inserted:");
                                                                                    scanf("%d",&ins);
                                                                                    insert(rootp,ins);
                                                                                    break;
                                                                                    case 2:
                                                                                    printf("\n Enter the element to be inserted:");
                                                                                    scanf("%d",&ins);
                                                                                    insert(rootq,ins);
                                                                                    break;
                                                                                    }
                          break;
                          case 2:
                                                            printf("\n Enter 1 to remove in queue P, 2 to remove in queue Q:");
                                                            scanf("%d",&x);
                                                                        switch(x)
                                                                                    {
                                                                                    case 1:
                                                                                    e=remove(&rootp);
                                                                                    //if(e=-9999)
                                                                                    //                      {}
                                                                           //       else
                                                                                    printf("\n Element removed is:%d\n",e);
                                                                                    break;
                                                                                    case 2:
                                                                                    e=remove(&rootq);
                                                                                    //if(e=-9999)
                                                                                    //          {}
                                                                                    //else
                                                                                    printf("\n Element removed is: %d\n",e);
                                                                                    break;
                                                                                    }
                                    break;
                                    case 3:
                                    append(rootp,rootq);
                                    break;
                                    case 4:
                                    display_front_rear(&rootp);
                                    break;
                                    case 5:
                                    display_rear_front(&rootp);
                                    break;
                                    case 6:
                                    display_front_rear_unchanged(&rootp,&tqueue);
                                    break;
                                    case 7:
         case 8:
                                    reverse_queue(&rootp);
                                    break;
                                    display_rear_front_unchanged(&rootp,&tqueue);
                                    break;
                                    case 9:
                                    printf("\n Enter the element to be replaced:");
                                    scanf("%d",&e);
                                    printf("\n Enter the element to be placed:");
                                    scanf("%d",&x);
                                    replace(&rootp,e,x,&tqueue);
                                    break;
                                    case 10:
                                    if(Isequal(&rootp,&rootq,&tqueue,&rqueue))
                                                printf("\n Queues are equal:\n");
                                    else
                                                printf("\n Queues are not equal:\n");
                                    break;
                                    }
                        }while(choice>=1 && choice<=10);
return 0;
}
int Isequal(node **qp,node **qq,node **t1,node **t2)
            {
            int FLAG=TRUE,s1=0,s2=0,x,y;
            if((Isempty(*qp) && !Isempty(*qq)) ||(!Isempty(*qp) && Isempty(*qq)))
                        return (FALSE);
            else if(Isempty(*qp) && Isempty(*qq))
                        return (TRUE);
            else
                                    {
                                    while(!Isempty(*qp))
                                                {insert(*t1,remove(qp)); s1++;}
                                    while(!Isempty(*qq))
                                                {insert(*t2,remove(qq)); s2++;}
                                    if(s1!=s2)
                                                return (FALSE);
                                    else
                                                {
                                                while(!Isempty(*t1))
                                                                        {
                                                                        x=remove(t1); y=remove(t2);
                                                                        if(x!=y)
                                                                        FLAG=FALSE;
                                                                        insert(*qp,x); insert(*qq,y);
                                                                        }
                                                }
                                     if(FLAG==TRUE)
                                                return (TRUE);
                                     else
                                                return (FALSE);
                                    }

   }
void replace(node**qp,int e,int x,node **qq)
            {
            int y;
            node *tmp;
            tmp=*qp;
            printf("\n Elements of the queue:");
            while(!Isempty(*qp))
                        insert(*qq,remove(qp));
            while(!Isempty(*qq))
                        {
                        y=remove(qq);
                        if(y==e)
                                    {
                                    printf("%d ",x);
                                    insert(tmp,x);
               }
                        else
                                    {
                                    insert(tmp,y);
                                    printf("%d ",y);
                                    }
                        }
            printf("\n");
            }
void display_rear_front_unchanged(node **qp,node **qq)
            {
            int x;
            printf("\n Elements of the queue:");
            while(!Isempty(*qp))
                         {
                         x=remove(qp);
                         push(&stk,x);
                         insert(*qq,x);
                         }
            while(!isempty_st(&stk))
                        {
                        printf("%d ",pop(&stk));
                        insert(*qq,remove(qq));
                        }
            printf("\n");
            }
void display_front_rear_unchanged(node **qp,node **qq)
            {
            int x;
            node *tmp;
            tmp=*qp;
            printf("\n Elements of the queue:");
            while(!Isempty(*qp))
                        insert(*qq,remove(qp));
            while(!Isempty(*qq))
                        {
                        x=remove(qq);
                        printf("%d ",x);
                        insert(tmp,x);
                        }
            printf("\n");
            }

void reverse_queue(node **use)
{
            node *curr,*prev;
   prev=(node *)malloc(sizeof(node));
            prev->next=NULL;
            curr=*use;
            while((*use)->next!=NULL)
                        {
                                                curr=*use;
                                                *use=(*use)->next;
                                                curr->next=prev;
                                                prev=curr;
                        }
                        *use=curr;
}
void display_rear_front(node **use)
            {
            printf("\n Elements of the queue:");
            while(!Isempty(*use))
                        push(&stk,remove(use));
            while(!isempty_st(&stk))
                        printf("%d ",pop(&stk));
            printf("\n");
            }
void display_front_rear(node **use)
            {
            printf("\n Elements of the queue:");
            while(!Isempty(*use))
                        printf("%d ",remove(use));
            printf("\n");
            }
void append(node *p,node *q)
            {
            while(p->next->next!=NULL)
                        p=p->next;
            p->next=q;
            }
int Isempty(node *use)
            {
            return ((use->next==NULL)? TRUE : FALSE);
            }
int remove(node **use)
            {
            if(Isempty(*use))
                        {printf("\n Queue is empty:\n");
                        return (-9999);}
            else{
            node *temp,*tp;
            temp=*use;
            tp=temp;
            temp=temp->next;
            *use=temp;
            return(tp->items);
            free(tp);}
            }
void insert(node *use,int x)
            {
            node *p;
            while(use->next!=NULL)
            use=use->next;
            use->items=x;
            p=(node *)malloc(sizeof(node));
            use->next=p;
            p->next=NULL;
            }
int isempty_st(struct stack *stk)
            {
             return((stk->top==-1)?TRUE:FALSE);
            }
int pop(struct stack *stk)
            {
            return(stk->items[(stk->top)--]);
            }
void push(struct stack *stk,int x)
            {
            stk->items[++stk->top]=x;
            }