Sunday, October 6, 2024

SYBSc (CS) Sem III : Data Structure Slip 6 Que - 2

   SAVITIBAI PHULE UNIVERSITY OF PUNE

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

Practical Examination

      SUBJECT: CS-233 Practical course based on CS231 

/* Q. 2 Implement a queue library (dyqueue.h) of integers using a dynamic (linked list) implementation of the queue and implement init, enqueue, dequeue, isempty, peek operations.*/

/// File name : "dyqueue.c"

#include"dyqueue.h"

int init()

{

    front = rear = NULL;

    return 0;

}

int isEmpty()

{

    if((front == NULL && rear == NULL) || front == rear)

        return 1;

    else

        return 0;

}

void enqueue(int x)

{

    struct Queue *NewNode = NULL;    

    NewNode = (struct Queue*) malloc(sizeof(struct Queue));

    if (NewNode != NULL)

    {

       NewNode->data = x;

       NewNode->Next = NULL;       

       if (front == NULL)

          rear = front = NewNode;

       else{

            rear->Next = NewNode;

            rear = NewNode;

       }            

    }

}

int dequeue()

{

    int x;    

    struct Queue *temp = front;    

    if(isEmpty() == 1)

    {

         printf("Queue is empty...");

         return -1;

    }

    else

    {    

        x = front->data;        

        if(front == rear)

             front = rear = NULL;

        else

             front = front->Next;        

        free(temp);        

        return (x);

    }

}

int peek()

{

    int x;    

    struct Queue *temp = front;    

    x = front->data;    

    if(front == rear)

         front = rear = NULL;

    else

         front = front->Next;    

    return (x);

}

void Display()

{

     struct Queue *temp = front;         

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

     while(temp)

     {

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

           temp = temp->Next;

     }

}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "dyqueue.h"

#include<stdio.h>
#include<stdlib.h>

struct Queue
{
       int data;
       struct Queue *Next;
}*front, *rear;

int init();
int isEmpty();
void enqueue(int x);
int dequeue();
int peek();
void Display();


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

#include"dyqueue.h"

int main(int argc, char *argv[])
{
  int ch, num;
    
    init();
    
    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);
                 
                 enqueue(num);                 
                 break;
                 
            case 2:
                 dequeue();
                 break;
            
            case 3:
                 Display();
                 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...