DATA STRUCTURE:ASSIGNMENT NO.7:QUEUE

 PRACTICE PROGRAM:

1) Write a C program to Implement Static implementation of circular queue of integers which 

includes operation as: a) Initialize() b) insert() c) delete() d) isempty() e) isfull() f) display() 

g) peek() 

ANS:

#include <stdio.h>

#include <conio.h>

#define max 10

typedef struct queue

{

    int item[max];

    int front, rear;

} QUEUE;


void initQueue(QUEUE *q)

{

    q->front = -1;

    q->rear = -1;

}


int isEmpty(QUEUE *q)

{

    return (q->front == -1 && q->rear == -1) ? 1 : 0;

}


int isFull(QUEUE *q)

{

    return ((q->rear + 1) % max == q->front) ? 1 : 0;

}


void insert(QUEUE *q, int n)

{

    if (isFull(q))

    {

        printf("\n The Queue is full!");

    }

    else

    {

        if (isEmpty(q))

        {

            q->front++;

            q->rear++;

            q->item[q->rear] = n;

        }

        else

        {

            q->rear = (q->rear + 1) % max;

            q->item[q->rear] = n;

        }

    }

}


void delete(QUEUE *q)

{

    int n;

    if (isEmpty(q))

    {

        printf("\n The Queue is Empty!");

    }

    else

    {

        if (q->front == q->rear)

        {

            n = q->item[q->front];

            q->front = q->rear = -1;

            printf("\n The deleted element is:%d", n);

        }

        else

        {

            n = q->item[q->front];

            q->front = (q->front + 1) % max;

            printf("\n The deleted element is:%d", n);

        }

    }

}


void display(QUEUE *q)

{

    int i;

    for (i = q->front; i != q->rear; i = (i + 1) % max)

    {

        printf("\t%d", q->item[i]);

    }

    printf("\t%d", q->item[i]);

}


int peek(QUEUE *q)

{

    int p;

    p = q->item[q->front];

    return p;

}

int main()

{

    int n, choice, p;

    QUEUE q;

    initQueue(&q);

    do

    {

        printf("\n----------------");

        printf("\n1.insert\n2.delete\n3.display\n4.Exit");

        printf("\n----------------");

        printf("\n Enter your choice:");

        scanf("%d", &choice);

        switch (choice)

        {

        case 1:

            printf("\n Enter for insert:");

            scanf("%d", &n);

            insert(&q, n);

            break;

        case 2:

            delete (&q);

            break;

        case 3:

            if (!isEmpty(&q))

            {

                printf("\n The Queue is:\n");

                display(&q);

            }

            else

            {

                printf("\n Queue is Empty!");

            }

            break;

        case 4:

            p = peek(&q);

            printf("\n The peek element is:%d", p);

            break;

        }

    } while (choice != 5);

    getch();

    return 0;

}

----------------------------------------------------------------------------

2) Write a C program to Implement Dynamic implementation of circular queue of integers 

includes operation as : a)Initialize() b) insert() c)delete() d) isempty() e)display() f) peek()

ANS:

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

typedef struct queue

{

    int data;

    struct queue *next;

} QUEUE;

QUEUE *rear = NULL;

int isEmpty(QUEUE *q)

{

    if (q == NULL)

        return 1;

    else

        return 0;

}


QUEUE *insert(QUEUE *q, int n)

{

    QUEUE *new_node;

    new_node = (QUEUE *)malloc(sizeof(QUEUE));

    new_node->next = NULL;

    new_node->data = n;

    if (isEmpty(q))

    {

        rear = q = new_node;

        return q;

    }

    else

    {

        rear->next = new_node;

        new_node->next = q;

        rear = new_node;

        return q;

    }

}


QUEUE *delete(QUEUE *q)

{

    int n;

    QUEUE *temp = q;

    if (isEmpty(q))

    {

        printf("\n The Queue is empty!");

        return q;

    }

    else

    {

        n = temp->data;

        q = temp->next;

        free(temp);

        rear->next = q;

        printf("\n the deleted element is:%d", n);

        return q;

    }

}


void display(QUEUE *q)

{

    QUEUE *temp = q;

    do

    {

        printf("\t%d", temp->data);

        temp = temp->next;

    } while (temp != q);

}


int main()

{

    int n, choice;

    QUEUE *q = NULL;

    do

    {

        printf("\n------------");

        printf("\n1.insert\n2.delete\n3.display\n4.Exit");

        printf("\n------------");

        printf("\n Enter your choice:");

        scanf("%d", &choice);

        switch (choice)

        {

        case 1:

            printf("\n Enter element for inserting:");

            scanf("%d", &n);

            q = insert(q, n);

            break;

        case 2:

            q = delete (q);

            break;

        case 3:

            if (!isEmpty(q))

            {

                printf("\nThe Queue is:\n");

                display(q);

            }

            else

            {

                printf("\n The Queue is Empty!");

            }

            break;

        }

    } while (choice != 4);

    getch();

    return 0;

}

--------------------------------------------------------------------------------------

3) Write a C Program to implement Deque using doubly linked list.

ANS:

-------------------------------------------------------------------------------------

SET A:

1) Write a C program to Implement Static implementation of Queue of integers with 

following operation:

-Initialize(), insert(), delete(), isempty(), isfull(), display(), peek()

ANS:

#include <stdio.h>

#include <conio.h>

#define max 100

typedef struct queue

{

    int front;

    int rear;

    int item[max];

} QUEUE;


void initQueue(QUEUE *q)

{

    q->front = -1;

    q->rear = -1;

}


int isEmpty(QUEUE *q)

{

    if (q->front == -1 && q->rear == -1)

        return 1;

    else

        return 0;

}


int isFull(QUEUE *q)

{

    if (q->rear == max - 1)

        return 1;

    else

        return 0;

}


void insert(QUEUE *q, int n)

{

    if (isEmpty(q))

    {

        q->front++;

        q->rear++;

        q->item[q->rear] = n;

    }

    else

    {

        if (isFull(q))

        {

            printf("\n The Queue is full...!");

        }

        else

        {

            q->rear++;

            q->item[q->rear] = n;

        }

    }

}


int delete(QUEUE *q)

{

    int a;

    if (isEmpty(q))

    {

        printf("\n The Queue is Empty...!");

    }

    else

    {

        if (q->front == q->rear)

        {

            a = q->item[q->front];

            q->front = q->rear = -1;

        }

        else

        {

            a = q->item[q->front];

            q->front++;

        }

    }

    return a;

}


void display(QUEUE *q)

{

    int i;

    for (i = q->front; i <= q->rear; i++)

    {

        printf("\t%d", q->item[i]);

    }

}

void peek(QUEUE *q)

{

    int i;

    i = q->item[q->front];

    printf("\n The Peek element is:%d", i);

}

int main()

{

    QUEUE q;

    int choice, n;

    initQueue(&q);

    do

    {

        printf("\n-----------------");

        printf("\n1.insert");

        printf("\n2.delete");

        printf("\n3.display");

        printf("\n4.peek");

        printf("\n5.exit");

        printf("\n-----------------");

        printf("\n Enter your choice:");

        scanf("%d", &choice);

        switch (choice)

        {

        case 1:

            printf("\n Enter value for inserting:");

            scanf("%d", &n);

            insert(&q, n);

            break;

        case 2:

            n = delete (&q);

            printf("\n The deleted element is:%d", n);

            break;

        case 3:

            printf("\n The Queue is:\n");

            display(&q);

            break;

        case 4:

            peek(&q);

            break;

        }

    } while (choice != 5);

}

---------------------------------------------------------------------------

2) Write a program to reverse the elements of a queue (Use Static implementation of Queue).

ANS:

#include <stdio.h>

#include <conio.h>

#define max 100

typedef struct queue

{

    int front;

    int rear;

    int item[max];

} QUEUE;


void initQueue(QUEUE *q)

{

    q->front = -1;

    q->rear = -1;

}


int isEmpty(QUEUE *q)

{

    if (q->front == -1 && q->rear == -1)

        return 1;

    else

        return 0;

}


int isFull(QUEUE *q)

{

    if (q->rear == max - 1)

        return 1;

    else

        return 0;

}


void insert(QUEUE *q, int n)

{

    if (isEmpty(q))

    {

        q->front++;

        q->rear++;

        q->item[q->rear] = n;

    }

    else

    {

        if (isFull(q))

        {

            printf("\n The Queue is full...!");

        }

        else

        {

            q->rear++;

            q->item[q->rear] = n;

        }

    }

}


int delete(QUEUE *q)

{

    int a;

    if (isEmpty(q))

    {

        printf("\n The Queue is Empty...!");

    }

    else

    {

        if (q->front == q->rear)

        {

            a = q->item[q->front];

            q->front = q->rear = -1;

        }

        else

        {

            a = q->item[q->front];

            q->front++;

        }

    }

    return a;

}


void display(QUEUE *q)

{

    int i;

    for (i = q->front; i <= q->rear; i++)

    {

        printf("\t%d", q->item[i]);

    }

}


void reverse(QUEUE *q)

{

    int s, i;

    s = q->front;

    q->front = q->rear;

    q->rear = s;

    printf("\n The reverse Queue is:\n");

    for (i = q->front; i >= q->rear; i--)

    {

        printf("\t%d", q->item[i]);

    }

}

int main()

{

    QUEUE q;

    int choice, n;

    initQueue(&q);

    do

    {

        printf("\n-----------------");

        printf("\n1.insert\n2.delete\n3.display\n4.reverse\n5.Exit");

        printf("\n-----------------");

        printf("\n Enter your choice:");

        scanf("%d", &choice);

        switch (choice)

        {

        case 1:

            printf("\n Enter insert element:");

            scanf("%d", &n);

            insert(&q, n);

            break;

        case 2:

            n = delete (&q);

            printf("\n The deleted element is:%d", n);

            break;

        case 3:

            printf("\n The Queue is:\n");

            display(&q);

            break;

        case 4:

            reverse(&q);

            break;

        }

    } while (choice != 5);

}

-----------------------------------------------------------------------------------------

SET B:

1) Write a C program to Implement Dynamic implementation of Queue of integers with 

following operation:

-Initialize(), insert(), delete(), isempty(), display(), peek()

ANS:

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

typedef struct queue

{

    int data;

    struct queue *next;

} QUEUE;


QUEUE *rear = NULL;

int isEmpty(QUEUE *q)

{

    return (q == NULL) ? 1 : 0;

}


QUEUE *insert(QUEUE *q, int n)

{

    QUEUE *new_node;

    new_node = (QUEUE *)malloc(sizeof(QUEUE));

    new_node->next = NULL;

    new_node->data = n;

    if (isEmpty(q))

    {

        q = rear = new_node;

        return q;

    }

    else

    {

        rear->next = new_node;

        rear = new_node;

        return q;

    }

}


QUEUE *delete(QUEUE *q)

{

    int n;

    QUEUE *temp = q;

    if (isEmpty(q))

    {

        printf("\n The Queue is empty!");

        return q;

    }

    else

    {

        n = temp->data;

        q = temp->next;

        free(temp);

        printf("\n the deleted element is:%d", n);

        return q;

    }

}


void display(QUEUE *q)

{

    QUEUE *temp;

    for (temp = q; temp != NULL; temp = temp->next)

    {

        printf("\t%d", temp->data);

    }

}


int main()

{

    int n, choice;

    QUEUE *q = NULL;

    do

    {

        printf("\n------------");

        printf("\n1.insert\n2.delete\n3.display\n4.Exit");

        printf("\n------------");

        printf("\n Enter your choice:");

        scanf("%d", &choice);

        switch (choice)

        {

        case 1:

            printf("\n Enter element for inserting:");

            scanf("%d", &n);

            q = insert(q, n);

            break;

        case 2:

            q = delete (q);

            break;

        case 3:

            if (!isEmpty(q))

            {

                printf("\nThe Queue is:\n");

                display(q);

            }

            else

            {

                printf("\n Queue is Empty!");

            }

            break;

        }

    } while (choice != 4);

    getch();

    return 0;

}

----------------------------------------------------------------------------

2) Write a program to reverse the elements of a queue (Use Dynamic implementation of 

Queue).

ANS:

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

typedef struct queue

{

    int data;

    struct queue *next;

} QUEUE;


int isEmpty(QUEUE *q)

{

    if (q == NULL)

        return 1;

    else

        return 0;

}


QUEUE *insert(QUEUE *q, int n)

{

    QUEUE *new_node, *temp;

    new_node = (QUEUE *)malloc(sizeof(QUEUE));

    new_node->next = NULL;

    new_node->data = n;

    if (isEmpty(q))

    {

        temp = q = new_node;

        return q;

    }

    else

    {

        for (temp = q; temp->next != NULL; temp = temp->next)

            ;

        temp->next = new_node;

        temp = new_node;

        return q;

    }

}


QUEUE *delete(QUEUE *q)

{

    int n;

    QUEUE *temp = q;

    if (isEmpty(q))

    {

        printf("\n The Queue is empty!");

        return q;

    }

    else

    {

        n = temp->data;

        q = temp->next;

        free(temp);

        printf("\n%d", temp->data);

        printf("\n the deleted element is:%d", n);

        return q;

    }

}


void display(QUEUE *q)

{

    QUEUE *temp;

    for (temp = q; temp != NULL; temp = temp->next)

    {

        printf("\t%d", temp->data);

    }

}


QUEUE *reverse(QUEUE *q)

{

    QUEUE *t1, *t2, *t3;

    t1 = q;

    t2 = t1->next;

    t3 = t2->next;

    t1->next = NULL;

    while (t3 != NULL)

    {

        t2->next = t1;

        t1 = t2;

        t2 = t3;

        t3 = t3->next;

    }

    t2->next = t1;

    return t2;

}

int main()

{

    int n, choice;

    QUEUE *q = NULL;

    do

    {

        printf("\n------------");

        printf("\n1.insert\n2.delete\n3.display\n4.reverse\n5.Exit");

        printf("\n------------");

        printf("\n Enter your choice:");

        scanf("%d", &choice);

        switch (choice)

        {

        case 1:

            printf("\n Enter element for inserting:");

            scanf("%d", &n);

            q = insert(q, n);

            break;

        case 2:

            q = delete (q);

            break;

        case 3:

            printf("\nThe Queue is:\n");

            display(q);

            break;

        case 4:

            q = reverse(q);

            break;

        }

    } while (choice != 5);

    getch();

    return 0;

}

--------------------------------------------------------------------------------------------

3) Write a C program to Implement Static implementation of circular queue of integers with 

following operation:

-Initialize(), insert(), delete(), isempty(), isfull(), display(), peek().

ANS:

#include <stdio.h>

#include <conio.h>

#define max 100

typedef struct queue

{

    int item[max];

    int front;

    int rear;

} QUEUE;


void initQueue(QUEUE *pq)

{

    pq->front = pq->rear = -1;

}


int isEmpty(QUEUE *pq)

{

    if (pq->front == -1 && pq->rear == -1)

        return 1;

    else

        return 0;

}


int isFull(QUEUE *pq)

{

    if ((pq->rear + 1) % max == pq->front)

        return 1;

    else

        return 0;

}


void insertQueue(QUEUE *pq, int n)

{

    if (isFull(pq))

    {

        printf("\n The Queue is full--!");

    }

    else

    {

        if (isEmpty(pq))

        {

            pq->front++;

            pq->rear++;

            pq->item[pq->rear] = n;

        }

        else

        {

            pq->rear = (pq->rear + 1) % max;

            pq->item[pq->rear] = n;

        }

    }

}


int deleteQueue(QUEUE *pq)

{

    int n;

    if (isEmpty(pq))

    {

        printf("\n The Queue is Empty!");

    }

    else

    {

        if (pq->front == pq->rear)

        {

            n = pq->item[pq->front];

            pq->front = pq->rear = -1;

        }

        else

        {

            n = pq->item[pq->front];

            pq->front = (pq->front + 1) % max;

        }

    }

    return n;

}


void display(QUEUE *pq)

{

    int i;

    i = pq->front;

    while (i != pq->rear)

    {

        printf("\t%d", pq->item[i]);

        i = (i + 1) % max;

    }

    printf("\t%d", pq->item[i]);

}


int main()

{

    int choice, n, p;

    QUEUE q1;

    initQueue(&q1);

    do

    {

        printf("\n--------------");

        printf("\n1.insert\n2.delete\n3.display\n4.Exit");

        printf("\n--------------");

        printf("\nEnter your choice:");

        scanf("%d", &choice);

        switch (choice)

        {

        case 1:

            printf("\n Enter Element for inserting:");

            scanf("%d", &n);

            insertQueue(&q1, n);

            break;

        case 2:

            n = deleteQueue(&q1);

            printf("\n The deleted element is:%d", n);

            break;

        case 3:

            printf("\n The Queue is:\n");

            display(&q1);

            break;

        }

    } while (choice != 4);

    getch();

    return 0;

}

------------------------------------------------------------------------------------

SET C:

1) Write a c program to simulate waiting list operations of railway reservation system.

ANS:

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

#define max 100

#define COUNT 50

typedef struct queue

{

    int id;

    char name[max];

    char destination[max];

    char source[max];

    int rate;

    int noises;

    int total;

    struct queue *next;

} QUEUE;


QUEUE *rear = NULL;

QUEUE *front = NULL;

int isEmpty(QUEUE *q)

{

    return (q == NULL) ? 1 : 0;

}


QUEUE *insert(QUEUE *q, int id, char name[], char dest[], char source[], int rate, int n, int total)

{

    QUEUE *temp, *new_node;

    new_node = (QUEUE *)malloc(sizeof(QUEUE));

    new_node->next = NULL;

    new_node->id = id;

    strcpy(new_node->name, name);

    strcpy(new_node->destination, dest);

    strcpy(new_node->source, source);

    new_node->rate = rate;

    new_node->noises = n;

    new_node->total = total;

    if (isEmpty(q))

    {

        front = rear = q = new_node;

        printf("\n Thanks For Visit!");

        printf("\n------------------------------------------");

    }

    else

    {

        rear->next = new_node;

        rear = new_node;

        printf("\n Thanks For Visit!");

        printf("\n------------------------------------------");

    }

    return q;

}


QUEUE *delete(QUEUE *q)

{

    QUEUE *temp;

    if (isEmpty(q))

    {

        printf("\n Sorry!passenger not available!");

        printf("\n------------------------------------------");

        return q;

    }

    else

    {

        temp = front;

        printf("\n Passenger id=%d", temp->id);

        printf("\n Passenger Name=%s", temp->name);

        printf("\n Passenger destination=%s", temp->destination);

        printf("\n Passenger Source =%s", temp->source);

        printf("\n No.of Passengers=%d", temp->noises);

        printf("\n Per Passenger cost=%d", temp->rate);

        printf("\n Total cost=%d", temp->total);

        front = temp->next;

        free(temp);

        printf("\n Record Deleted Successfully!");

        printf("\n------------------------------------------");

        return q;

    }

}


void display(QUEUE *q)

{

    printf("\tid\tname\tdestination\tsource\tno.of passengers\tcost\ttotal");

    printf("\n---------------------------------------------------------------------------\n");

    front = q;

    do

    {

        printf("\t%d", front->id);

        printf("\t%s", front->name);

        printf("\t%s", front->destination);

        printf("\t%s", front->source);

        printf("\t%d", front->noises);

        printf("\t%d", front->rate);

        printf("\t%d", front->total);

        printf("\n");

        front = front->next;

    } while (front != NULL);

}


void search(QUEUE *q, char name[])

{

    int flag = 0;

    front = q;

    do

    {

        if (strcmp(front->name, name) == 0)

        {

            flag = 1;

            printf("\tid\tname\tdestination\tsource\tno.of passengers\tcost\ttotal");

            printf("\n---------------------------------------------------------------------------\n");

            printf("\t%d", front->id);

            printf("\t%s", front->name);

            printf("\t%s", front->destination);

            printf("\t%s", front->source);

            printf("\t\t%d", front->noises);

            printf("\t\t%d", front->rate);

            printf("\t\t%d", front->total);

            printf("\n---------------------------------------------------------------------------");

            break;

        }

        front = front->next;

    } while (front != rear);


    if (flag == 0)

    {

        printf("\nSorry!Passenger Not Found!");

    }

}

int main()

{

    QUEUE *q = NULL;

    int choice, id, cost, total, n;

    char name[max], source[max], dest[max];

    do

    {

        printf("\n-------------Railway Reservation System--------------------");

        printf("\n1.Ticket Booking");

        printf("\n2.Passenger Detail");

        printf("\n3.Remove Passenger");

        printf("\n4.Display all Passenger Details");

        printf("\n5.Exit");

        printf("\n-----------------------------------------------------------");

        printf("\n Enter your choice:");

        scanf("%d", &choice);

        switch (choice)

        {

        case 1:

            printf("\n Enter Passenger id:\t");

            scanf("%d", &id);

            printf("\n Enter Passenger name:\t");

            scanf("%s", &name);

            printf("\n Enter Passenger source:\t");

            scanf("%s", &source);

            printf("\n Enter Passenger destination:\t");

            scanf("%s", &dest);

            printf("\n Enter No.of peoples Travel:\t");

            scanf("%d", &n);

            if (strcmp(source, "mumbai") == 0 || strcmp(source, "pune") == 0 || strcmp(source, "nashik") == 0)

            {

                cost = 200;

            }

            else

            {

                cost = 100;

            }

            printf("\n Cost per passenger is:\t%d\n", cost);

            total = cost * n;

            printf("\n Total Cost:\t%d\n", total);

            q = insert(q, id, name, dest, source, cost, n, total);

            break;

        case 2:

            printf("\n Enter Name of Passenger:");

            scanf("%s", &name);

            search(q, name);

            break;

        case 3:

            q = delete (q);

            break;

        case 4:

            display(q);

            break;

        }

    } while (choice != 5);

    getch();

    return 0;

}

----------------------------------------------------------------------------------------------------------------

2) Implement a priority of integers using a static implementation of the queue and 

implementing the below two operations. Write a menu driven program 

a) Add an element with its priority into the queue. 

b) Delete an element from queue according to its priority. 

ANS:

//dynamic implementation...

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

typedef struct node

{

    int data;

    int priority;

    struct node *next;

} QUEUE;

QUEUE *rear = NULL;


int isEmpty(QUEUE *q)

{

    return (q == NULL) ? 1 : 0;

}

QUEUE *insert(QUEUE *q, int n, int p)

{

    QUEUE *new_node;

    new_node = (QUEUE *)malloc(sizeof(QUEUE));

    new_node->data = n;

    new_node->priority = p;

    new_node->next = NULL;

    if (isEmpty(q))

    {

        q = rear = new_node;

    }

    else

    {

        rear->next = new_node;

        rear = new_node;

    }

    return q;

}


QUEUE *delete(QUEUE *q, int p)

{

    QUEUE *temp, *temp1;

    int n;

    if (isEmpty(q))

    {

        printf("\n The Queue is Empty!");

        return q;

    }

    else

    {

        if (q->priority == p)

        {

            temp = q;

            n = temp->data;

            q = temp->next;

            free(temp);

            printf("\n The deleted node data is:%d", n);

            return q;

        }

        else

        {

            for (temp = q; temp->next->priority != p; temp = temp->next)

                ;

            temp1 = temp->next;

            n = temp1->data;

            temp->next = temp1->next;

            free(temp1);

            printf("\n The deleted node data is:%d", n);

            return q;

        }

    }

}


int getHeighPriority(QUEUE *q)

{

    int p;

    QUEUE *temp = q;

    p = temp->priority;

    for (temp = q; temp != NULL; temp = temp->next)

    {

        if (temp->priority < p)

        {

            p = temp->priority;

        }

    }

    return p;

}


void display(QUEUE *q)

{

    QUEUE *temp;

    printf("\nQueue:\t");

    for (temp = q; temp != NULL; temp = temp->next)

    {

        printf("\t%d", temp->data);

    }

    printf("\nPriority:");

    for (temp = q; temp != NULL; temp = temp->next)

    {

        printf("\t%d", temp->priority);

    }

}

int main()

{

    QUEUE *q = NULL;

    int p, n, choice;

    do

    {

        printf("\n-------------");

        printf("\n1.Insert\n2.Delete\n3.Get heigh priority\n4.Display\n5.Exit");

        printf("\n-------------");

        printf("\n Enter your choice:");

        scanf("%d", &choice);

        switch (choice)

        {

        case 1:

            printf("\n Enter data:");

            scanf("%d", &n);

            printf("\n Enter priority:");

            scanf("%d", &p);

            q = insert(q, n, p);

            break;

        case 2:

            if (!isEmpty(q))

            {

                p = getHeighPriority(q);

                q = delete (q, p);

            }

            else

            {

                printf("\n The Queue is Empty!");

            }

            break;

        case 3:

            if (!isEmpty(q))

            {

                p = getHeighPriority(q);

                printf("\n The heigh priority is:%d", p);

            }

            else

            {

                printf("\n The Queue is Empty!");

            }

            break;

        case 4:

            if (!isEmpty(q))

            {

                printf("\n The Queue is:\n");

                display(q);

            }

            else

            {

                printf("\n The Queue is Empty!");

            }

            break;

        }

    } while (choice != 5);

    getch();

    return 0;

}


//static implementation...

#include <stdio.h>

#include <conio.h>

#define max 100

typedef struct queue

{

    int data;

    int priority;

    int rear;

} P_QUEUE;


void initQueue(P_QUEUE *q)

{

    q->rear = -1;

}


int isEmpty(P_QUEUE *q)

{

    if (q->rear == -1)

        return 1;

    else

        return 0;

}


int isFull(P_QUEUE *q)

{

    if (q->rear == max - 1)

        return 1;

    else

        return 0;

}


void insert(P_QUEUE *q, int n, int p)

{

    if (isFull(q))

    {

        printf("\n The Queue is Full!");

    }

    else

    {

        q->rear++;

        q[q->rear].data = n;

        q[q->rear].priority = p;

    }

}


int delete(P_QUEUE *q, int p)

{

    int i, n, j;

    if (isEmpty(q))

        printf("\n The Queue is Empty!");

    else

    {

        for (i = 0; i < q->rear; i++)

        {

            if (q[i].priority == p)

            {

                n = q[i].data;

                break;

            }

        }

        for (j = i; j < q->rear; j++)

        {

            q[j].data = q[j + 1].data;

            q[j].priority = q[j + 1].priority;

        }

        q->rear--;

    }

    return n;

}


int getHeighPriority(P_QUEUE *q)

{

    int p, i;

    if (isEmpty(q))

    {

        printf("\n The Queue is Empty!");

    }

    else

    {

        p=q[0].priority;

        for(i=0;i<q->rear;i++)

        {

            if(q[i].priority>p)

            {

                p=q[i].priority;

            }

        }

    }

    return p;

}

void display(P_QUEUE *q)

{

    int i;

    for (i = 0; i <= q->rear; i++)

    {

        printf("\t%d", q[i].data);

    }

}

int main()

{

    P_QUEUE q[max];

    initQueue(&q);

    int choice, n, p;

    do

    {

        printf("\n---------");

        printf("\n1.insert\n2.delete\n3.Get Heigh Priority\n4.display\n5.Exit");

        printf("\n---------");

        printf("\n Enter choice:");

        scanf("%d", &choice);

        switch (choice)

        {

        case 1:

            if (isFull(&q))

                printf("\n The Queue is Full!");

            else

            {

                printf("\n Enter data:");

                scanf("%d", &n);

                printf("\n Enter priority:");

                scanf("%d", &p);

                insert(&q, n, p);

            }

            break;

        case 2:

            if (isEmpty(&q))

            {

                printf("\n The Queue is Empty!");

            }

            else

            {

                p = getHeighPriority(&q);

                n = delete (&q, p);

                printf("\n The deleted element is:%d", n);

            }

            break;

        case 3:

            p = getHeighPriority(&q);

            printf("\n The heigh priority is:%d", p);

            break;

        case 4:

            printf("\n The Queue is:\n");

            display(&q);

            break;

        }

    } while (choice != 5);

    getch();

    return 0;

}

-----------------------------------------------------------------------------------------------------

3) A doubly ended queue allows additions and deletions from both the ends that is front and 

rear. Initially additions from the front will not be possible. To avoid this situation, the array 

can be treated as if it were circular. Implement a queue library (dstqueue.h) of integers 

using a static implementation of the circular queue and implementing the nine operations : 

1)init(Q), 2) isempty(Q) 3) isFull(Q) 4)getFront(Q), 5)getRear(Q), 6)addFront(Q,x), 

7)deleteFront(Q) 8) addRear(Q,x) 9)deleteRear(Q)

ANS:


-----------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

PHP ALL ASSIGNMENT PDF

DATA STRUCTURE ALL PDF(LAB ASSIGNMENTS)

DATA STRUCTURE :ASSIGNMENT NO.8:TREE