p6 by ma'am (practical 4)


// 6.Write a Program to Implement a Circular Queue using Following Operation 1. Insert 2. Delete 3. Display 4. Exit

#include <stdio.h>
#include <conio.h>
#include <process.h>

struct queue
{
    int rear, front;
    int q[5];
};

void insert(struct queue *);
void delet(struct queue *, int);
void display(struct queue *);

void main()
{
    int i, a;
    struct queue x;
    clrscr();

    x.rear = x.front = -1;

    while (i != 6)
    {
        printf("\n1.INSERT\n2.DELETE\n3.DISPLAY\n4.EXIT");
        printf("\nEnter Your Choice:-");
        scanf("%d", &i);

        switch (i)
        {
        case 1:
            clrscr();
            insert(&x);
            display(&x);
            break;

        case 2:
            clrscr();
            delet(&x, a);
            display(&x);
            break;

        case 3:
            clrscr();
            display(&x);
            getch();
            clrscr();
            break;

        case 4:
            clrscr();
            printf("\n\n\n\t\t\t\tBYE");
            exit(0);
        }
    }
    getch();
}
void insert(struct queue *p)
{
    int a;
    printf("\nEnter the element to be inserted\n");
    scanf("%d", &a);
    if ((*p).rear == 4)
    {
        (*p).rear = -1;
    }

    if ((*p).rear >= 4)
    {
        printf("STACK IS OVERFLOW");
    }
    else
    {
        (*p).rear += 1;
        (*p).q[(*p).rear] = a;
    }
    if ((*p).front == (*p).rear)
    {
        printf("\nThe queue is full can not insert a value\n");
        goto d;
    }
d:
}
void delet(struct queue *p, int a)
{
    if ((*p).front == (*p).rear)
    {
        printf("\nThe queue is empty can not delete a value\n");
        goto d;
    }
    if ((*p).front == 4)
        (*p).front = -1;
    else
    {
        (*p).front += 1;
        a = (*p).q[(*p).front];
        printf("\nThe Value Deleted is %d\n", a);
    }
d:
}
void display(struct queue *p)
{
    int i, j;
    if ((*p).rear > (*p).front)
    {
        for (i = (*p).front + 1; i <= (*p).rear; i++)
            printf("\n\t\ts[%d] = %d ", i, (*p).q[i]);
    }
    else if ((*p).rear < (*p).front)
    {
        for (i = (*p).front + 1; i <= 4; i++)
            printf("\n\t\ts[%d]=%d", i, (*p).q[i]);
        for (j = 0; j <= (*p).rear; j++)
            printf("\n\t\tq[%d] = %d", j, (*p).q[j]);
    }
}

/*
OUTPUT

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice:-1
Enter the element to be inserted
34
                s[0] = 34
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice:-1
Enter the element to be inserted
59

                s[0] = 34
                s[1] = 59
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice:-1
Enter the element to be inserted
21

                s[0] = 34
                s[1] = 59
                s[2] = 21
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice:-2
The Value Deleted is 78
                s[1] = 59
                s[2] = 21
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice:-3
                s[1] = 59
                s[2] = 21
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice:-4
*/

Post a Comment

0 Comments