Monday, October 7, 2024

SYBSc (CS) Sem III : Data Structure Slip 10 Que - 1

   SAVITIBAI PHULE UNIVERSITY OF PUNE

S. Y. B.Sc. (Computer Science) Semester III

Practical Examination

      SUBJECT: CS-233 Practical course based on CS231 

/* Q. 1. Implement a linear queue library (st_queue.h) of integers using a static implementation of the queue and implementing the init(Q), add(Q) and peek(Q) operations. Write a program that includes queue library and calls different queue operations.*/

/// File name : "st_queue.c"

#include"st_queue.h"

int init(struct Queue *q)

{

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

}

int isEmpty(struct Queue *q)

{

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

    {

       return 1;

    }

    else{

       return 0;

    }

}

int isFull(struct Queue *q)

{

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

    {

       return 1;

    }

    else{

       return 0;

    }

}

int add(struct Queue *q, int x)

{

    if(isFull(q) == 1)

    {

       printf("The queue is overflow...");

       return 1;

    }

    else{

         q->data[++(q->rear)] = x;

    }    

    return 0;

}

int peek(struct Queue *q)

{

    if(isEmpty(q) == 1)

    {

       printf("The stack is underflow...");

       return 1;

    }

    else{

         return q->data[++(q->front)];

    }

}

void Display(struct Queue *q)

{

     int i = 0;     

     printf("\n The Queue elements are : \t");     

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

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

}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "st_queue.h"

#include<stdio.h>
#include<stdlib.h>
#define MAX 20

struct Queue
{
       int data[MAX];
       int front, rear;
};

int init(struct Queue *q);
int isEmpty(struct Queue *q);
int isFull(struct Queue *q);
int add(struct Queue *q, int x);
int peek(struct Queue *q);
void Display(struct Queue *q);



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "main.c"

#include"st_queue.h"

int main()
{
    struct Queue q;
    
    int ch, num;
    
    init(&q);
    
    do{
       printf("\n1. Insert \n2. Delete \n3. Display \n4. Exit \n");
       scanf("%d", &ch);
       
       switch(ch)
       {
            case 1: 
                 printf("Enter the element to insert into queue: ");
                 scanf("%d", &num);
                 
                 add(&q, num);                 
                 break;
                 
            case 2:
                 peek(&q);
                 break;
            
            case 3:
                 Display(&q);
                 break;
            default:
                 printf("Thank you visit again ....\n\n");
                 break;          
       }
    
    }while(ch != 4);
    
    system("pause");
    return 0;
}

No comments:

Post a Comment

To Connect Java program with the MYSQL Database

To connect java application with the MYSQL database, first of all to install MYSQL database in your machine. To install MYSQL in your mach...