Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: C Program #18

Thursday, March 28, 2013

C Program #18


/* 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 front;
                        int rear;
                        int items[MAXSIZE];
                        };
struct stack{
                        int top;
                        int items[MAXSIZE];
                        };
struct stack stk;
struct queue p,q,temp;
int pop(struct stack *);
int remove(struct queue *);
int Isempty(struct queue *);
void push(struct stack *,int);
int isempty_st(struct stack *);
void insert(struct queue *,int);
void reverse_queue(struct queue *);
void display_front_rear(struct queue *);
void display_rear_front(struct queue *);
void append(struct queue *,struct queue *);
void replace(struct queue *,int,int);
void display_front_rear_unchanged(struct queue *);
void display_rear_front_unchanged(struct queue *);
int main()
{
            int choice,x,ins,e;
             stk.top=-1;
            p.front=p.rear=MAXSIZE-1;
            q.front=q.rear=MAXSIZE-1;
            temp.front=temp.rear=MAXSIZE-1;
            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 Type 10 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(&p,ins);
                                                                                    break;
                                                                                    case 2:
                                                                        printf("\n Enter the element to be inserted:");
                                                                                    scanf("%d",&ins);
                                                                                    insert(&q,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:
                                                            printf("\n Element removed is: %d\n",remove(&p));
                                                                                    break;
                                                                                    case 2:
                                                            printf("\n Element removed is: %d\n",remove(&p));
                                                                                    break;
                                                                                    }
                                    break;
                                    case 3:
                                    append(&p,&q);
                                    break;
                                    case 4:
                                    display_front_rear(&p);
                                    break;
                                    case 5:
                                    display_rear_front(&p);
                                    break;
                                    case 6:
                                    display_front_rear_unchanged(&p);
                                    break;
                                    case 7:
                                    display_rear_front_unchanged(&p);
                                    break;
                                    case 8:
                                    reverse_queue(&p);
                                    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(&p,e,x);
                                    break;
                                    }
                        }while(choice>=1 && choice<=9);
return 0;
}
void replace(struct queue *p,int e,int x)
            {
            int y;
             while(!Isempty(p))
                        {
                        y=remove(p);
                        if(y==e)
                                    insert(&temp,x);
                        else
                                    insert(&temp,y);
                        }
            printf("\n Elements of the queue after replacing\n");
            while(!Isempty(&temp))
                        {
                        x=remove(&temp);
                        printf("%d ",x);
                        insert(p,x);
                        }
            }
void reverse_queue(struct queue *p)
            {
   int x;
            while(!Isempty(p))
                        {
                        x=remove(p);
                        push(&stk,x);
                        }
            printf("\n Elements of the queue (after reversing)\n");
            while(!isempty_st(&stk))
                        {
                        x=pop(&stk);
                        printf("%d ",x);
                        insert(p,x);
                        }
            }
void display_rear_front_unchanged(struct queue *p)
            {
            int x;
            while(!Isempty(p))
                        {
                        x=remove(p);
                        push(&stk,x);
                        insert(&temp,x);
                        }
            printf("\n Elements of the queue (rear to front)\n");
            while(!isempty_st(&stk))
                        {
                        printf("%d ",pop(&stk));
                        x=remove(&temp);
                        insert(p,x);
                        }
            }
void display_front_rear_unchanged(struct queue *p)
            {
            int x;
            while(!Isempty(p))
                        insert(&temp,remove(p));
            printf("\n Elements of the queue (front to rear)\n");
            while(!Isempty(&temp))
                        {
                        x=remove(&temp);
                        printf("%d ",x);
                        insert(p,x);
                        }
            }
void display_rear_front(struct queue *p)
            {
            while(!Isempty(p))
                        push(&stk,remove(p));
            printf("\n Elements of the queue (rear to front)\n");
            while(!isempty_st(&stk))
                        printf("%d ",pop(&stk));
            }
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;
            }
void display_front_rear(struct queue *q)
            {
            printf("\n Elements of the queue (front to end)\n");
            while(!Isempty(q))
                        printf("%d ",remove(q));
            }
void append(struct queue *p, struct queue *q)
            {
            while(!Isempty(q))
                        insert(p,remove(q));
            printf("\n Appended........\n");
            }
int remove(struct queue *q)
            {
                        if(Isempty(q))
                                    {
                                    printf("\n Queue underflow:");
                                    exit(1);
                                    }
                        if(q->front==MAXSIZE-1)
                                    q->front=0;
                        else
                                    (q->front)++;
                        return(q->items[q->front]);
            }
int Isempty(struct queue *q)
            {
            return((q->front==q->rear)?TRUE:FALSE);
            }
void insert(struct queue *q,int x)
            {
            if(q->rear==MAXSIZE-1)
                        q->rear=0;
            else
                        (q->rear)++;
            if(q->front==q->rear)
                        printf("\n Queue Overflow...:");
                        q->items[q->rear]=x;
   return;
            }

No comments:

Post a Comment