Tuesday, October 15, 2024

SYBSc (CS) Sem III : Data Structure Slip 14 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 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 following operations. 

a. isFull(Q)

b. addFront(Q)

c. getRear(Q)

d. deleteRear(Q)

*/

// File name : "dstqueue.c"


#include"dstqueue.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 addFront(struct Queue *q, int x)

{

    if(isFull(q) == 1)

    {

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

       return 1;

    }

    else{

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

    }    

    return 0;

}

int getRear(struct Queue *q)

{

    if(isEmpty(q) == 1)

    {

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

       return 1;

    }

    else{

         return q->data[q->rear];

    }

}

int deleteRear(struct Queue *q)

{

    if(isEmpty(q) == 1)

    {

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

       return 1;

    }

    else{

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

    }

}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "dstqueue.h"

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

struct Queue
{
       int data;
       struct Queue *Next;
};

int init(struct Queue *Q);
int isEmpty(struct Queue *Q);
int isFull(struct Queue *q);
void addFront(struct Queue *Q, int x);
int getRear(struct Queue *Q);
int deleteRear(struct Queue *Q);



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

#include"dstqueue.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);
                 
                 addFront(&q, num);                 
                 break;
                 
            case 2:
                 printf("The current element : %d", getRear(&q));
                 break;
            
            case 3:
                 printf("The deleted element : %d", deleteRear(&q));
                 break;
            default:
                 printf("Thank you visit again ....\n\n");
                 break;          
       }
    
    }while(ch != 4);
    
    system("pause");
    return 0;
}

SYBSc (CS) Sem III : Data Structure Slip 11 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 priority queue library (Priority.h) of integers using a static implementation of the queue and implementing the below two operations. Write a driver program that includes queue library and calls different queue operations.

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

2) Delete an element from queue according to its priority. */

// File name : "PriorityQ.c"

#include"PriorityQ.h"

int initPQ(struct PriorityQueue *pq)

{

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

    return 0;

}

int isFullPQ(struct PriorityQueue *pq)

{

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

        return 1;

    else

        return 0;

}

int isEmptyPQ(struct PriorityQueue *pq)

{

    if(pq->front == -1)

        return 1;

    else

        return 0;

}

void InsertPQ(struct PriorityQueue *pq, struct data dt)

{

    struct data temp;

    int i, j;    

    pq->rear++;

    pq->d[pq->rear] = dt;

    

    if(pq->front == -1)

            pq->front = 0;    

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

    {

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

          {

               if( pq->d[i].priority > pq->d[j].priority )

               {

                   temp = pq->d[i];

                   pq->d[i] = pq->d[j];

                   pq->d[j] = temp;

               }

               else

               {

                   if( pq->d[i].priority == pq->d[j].priority )

                   {

                       if( pq->d[i].order > pq->d[j].order )

                       {

                            temp = pq->d[i];

                            pq->d[i] = pq->d[j];

                            pq->d[j] = temp;

                       }

                   }

               }

          }

    }    

}

struct data DeletePQ(struct PriorityQueue *pq)

{

    struct data t = pq->d[pq->front];   

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

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

    else

        pq->front++;        

    return t;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "PriorityQ.h"

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

struct data
{
       int item;
       int priority;
       int order;
};

struct PriorityQueue
{
       struct data d[MAX];
       int front;
       int rear;
};

int initPQ(struct PriorityQueue *pq);
int isFullPQ(struct PriorityQueue *pq);
int isEmptyPQ(struct PriorityQueue *pq);
void InsertPQ(struct PriorityQueue *pq, struct data dt);
struct data DeletePQ(struct PriorityQueue *pq);


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

#include"PriorityQ.h"

int main(int argc, char *argv[])
{
    struct PriorityQueue q;
    struct data temp;
    
    int i = 0, ch;
        
    initPQ(&q);
    
    do{
       printf("\n1. Insert \n2. Delete \n3. Exit \n");
       scanf("%d", &ch);
       
       switch(ch)
       {
            case 1:
                 if(isFullPQ(&q))
                     printf("\n Queue is full.");
                 else
                 {
                     printf("Enter the data and Priority : ");
                     printf("\nData \t Priority\n");
                     scanf("%d%d", &(temp.item), &(temp.priority));
                     temp.order = i++;             
                     InsertPQ(&q, temp);
                 } 
                 break;
                 
            case 2:
                 if(isEmptyPQ(&q))
                    printf("\n Queue is Empty");
                 else
                 {
                     temp = DeletePQ(&q);
                     printf("Data = %d \t Priority  = %d \n", temp.item, temp.priority);
                 }
                 break;
            default:
                 printf("Thank you visit again ....\n\n");
                 break;          
       }
    
    }while(ch>0 && ch<3);
    
    system("PAUSE");
    return 0;
}

SYBSc (CS) Sem III : Data Structure Slip 21 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. Read the data from the file "employee.txt" and sort on names in alphabetical order (use strcmp) using insertion sort and selection sort */

// File Name: "employee.txt" (This file create separate with txt extension and save in current folder.)

John

Aniket

Julie

Dolly

Rosemary


// File Name: "Slip21_2.c"

#include<stdio.h>

#include<stdlib.h>

#define MAX 20


typedef struct employee{

        char name[MAX];

}record;

record emp[MAX];


int read_file(record *a){

    int i = 0;

    FILE *fp;   

    if ((fp = fopen("employee.txt", "r")) != NULL)

    {

            while (!feof(fp)){

                  fscanf(fp, "%s", a[i].name);

                  i++;

            }

    }    

    return (i-1);

}

void InsertionSort(record *a, int n)

{

    int i, j;    

    record elem;

    for (i = 1; i <= n; i++) 

   {        

        elem = a[i];        

        for (j = i-1; strcmp(a[j].name, elem.name) > 0 && j > 0; j--) 

       {

            a[j + 1] = a[j];  

        }        

        a[i] = elem; 

    }

}


void SelectionSort(int A[], int n)

{

     int i = 0, j = 0, loc = 0, min = 0, temp = 0;     

     for ( i = 0; i < n-1; i++)

     {

         min = A[i];

         loc = i;         

         for ( j = i+1; j < n; j++)

         {

             if ( min > A[j])

             {

                  min = A[j];

                  loc = j;

             }

         }         

         temp = A[i];         

         A[i] = A[loc];         

         A[loc] = temp;

     }

}

void display(record *a, int n)

{

    int i;

    printf("\n\n");

    for (i = 0; i < n; i++) {

        printf("%s ", a[i].name);

    }

    printf("\n\n");

}

int main()

{

   int n = read_file(emp);   

   printf("%d", n);   

   InsertionSort(emp, n);   

   display(emp, n);

   int n = read_file(emp);   

   printf("%d", n);   

   SelectionSort(emp, n);   

   display(emp, n);   

   system("pause");

   return 0;

}


SYBSc (CS) Sem III : Data Structure Slip 21 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. Write a program that reverses a string of characters. The function should use a stack library (cststack.h). Use a static implementation of the stack. */

/// File name : "cststack.h"

#include"cststack.h"

int init(struct Stack *s)

{

    s->top = -1;

}

int isEmpty(struct Stack *s)

{

    if(s->top == -1)

    {

       return 1;

    }

    else{

       return 0;

    }

}

int isFull(struct Stack *s)

{

    if(s->top == MAX-1)

    {

       return 1;

    }

    else{

       return 0;

    }

}

int Push(struct Stack *s, char x)

{

    if(isFull(s) == 1)

    {

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

       return 1;

    }

    else{

         s->top++;

         s->data[s->top] = x;

    }

    

    return 0;

}

char Pop(struct Stack *s)

{

    if(isEmpty(s) == 1)

    {

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

       return 1;

    }

    else{

         return s->data[(s->top)--];

    }

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "cststack.h"

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

struct Stack
{
       char data[MAX];
       int top;
};

int init(struct Stack *s);
int isEmpty(struct Stack *s);
int isFull(struct Stack *s);
int Push(struct Stack *s, char x);
char Pop(struct Stack *s);

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

#include"cststack.h"

int main(int argc, char *argv[])
{
    struct Stack s;
    char str[MAX];
    int i = 0;
    
    init(&s);
    
    printf("Enter the given String :: ");
    scanf("%s", str);
    
    while(str[i] != '\0')
    {
       Push(&s, str[i]);
       i++;
    }
    
    while(!isEmpty(&s))
    {
        printf("%c", Pop(&s));
    }
  
    system("PAUSE");
    return 0;
}

SYBSc (CS) Sem III : Data Structure Slip 17 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. Write a program that copies the contents of one stack into another. Use stack library to perform basic stack operations. The order of two stacks must be identical. (Hint: Use a temporary stack to preserve the order). */

/// File name : "stack.c"

#include"stack.h"

int init(struct Stack *s)

{

    s->top = -1;

}

int isEmpty(struct Stack *s)

{

    if(s->top == -1)

    {

       return 1;

    }

    else{

       return 0;

    }

}

int isFull(struct Stack *s)

{

    if(s->top == MAX-1)

    {

       return 1;

    }

    else{

       return 0;

    }

}

int Push(struct Stack *s, int n)

{

    if(isFull(s) == 1)

    {

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

       return 1;

    }

    else{

         s->top++;

         s->data[s->top] = n;

    }    

    return 0;

}

int Pop(struct Stack *s)

{

    if(isEmpty(s) == 1)

    {

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

       return 1;

    }

    else{

         return s->data[(s->top)--];

    }

}

int Peek(struct Stack *s)

{

    if(isEmpty(s) == 1)

    {

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

       return 1;

    }

    else{

         return s->data[s->top];

    }

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "stack.h"

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

struct Stack
{
       int data[MAX];
       int top;
};

int init(struct Stack *s);
int isEmpty(struct Stack *s);
int isFull(struct Stack *s);
int Push(struct Stack *s, int n);
int Pop(struct Stack *s);
int Peek(struct Stack *s);


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

#include"stack.h"

int main(int argc, char *argv[])
{
    struct Stack s, s1, s2;
    int A[MAX], n = 0, i = 0, num = 0;
    
    init(&s);
    
    printf("How many number we have to enter : ");
    scanf("%d", &n);
    printf("Enter the stack elements: ");
    
    for (i = 0; i < n; i++)
    {
        scanf("%d", &num);
        Push(&s, num); 
    }
       
    for (i = 0; i < n; i++)
    {
        A[i] = Pop(&s);       
    }
    
    init(&s1);
        
    for (i = 0; i < n; i++)
    {
        Push(&s1, A[i]); 
    }
       
    for (i = 0; i < n; i++)
    {
        A[i] = Pop(&s1);       
    }
    
    init(&s2);
        
    for (i = 0; i < n; i++)
    {
        Push(&s2, A[i]); 
    }
       
    for (i = 0; i < n; i++)
    {
        printf("%d\t", Pop(&s2));       
    }
    
    system("PAUSE");
    return 0;
}

SYBSc (CS) Sem III : Data Structure Slip 17 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 

/* Implement a list library (singlylist.h) for a singly linked list. Create a linked list, reverse it and display reversed linked list.*/

/// File name : "singlylist.c"

#include"singlylist.h"

void CreateList()

{

     struct node *NewNode = NULL;     

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

     if (NewNode != NULL)

     {

        NewNode->Next = NULL;         

        printf("Enter the data value: ");

        scanf("%d", &(NewNode->data));

         

        if (Head == NULL)

        {

           Head = Last = NewNode;

        }

        else

        {

             Last->Next = NewNode;

             Last = NewNode;

        }

     }

}

void Display()

{

    struct node *temp = Head;

    printf("\n\nThe given Linked List is : ");

    while (temp != NULL)

    {     

          if (temp->Next != NULL)

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

          else

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

          temp = temp->Next;

    }

}

struct Node* ReverseLinkedList()

{

     struct Node *current = NULL, *reverse = NULL, *temp = NULL;     

     current = Head;     

     while(current != NULL)

     {

         temp = reverse;         

         reverse = current;         

         current = current->Next;         

         reverse->Next = temp;

     }     

    Head = reverse;                           

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "singlylist.h"


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

struct node{
        int data;
        struct node *Next;
}*Head, *Last;

void CreateList();
void Display();
struct Node* ReverseLinkedList();


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

#include"singlylist.h"

int main(int argc, char *argv[])
{
    int pos = 0, choice = 0;
    struct Node *rev = NULL;
    
    do
    {
            printf("\n\nEnter your choice: \n1. Create \n2. Display \n3. Reverse the List \n4. Exit \n\n");
            scanf("%d", &choice);
            
            switch(choice)
            {
                 case 1:
                      CreateList();
                      break;
                 case 2:
                      Display();
                      break;
                 
                 case 3:
                      ReverseLinkedList();
                      break;                        
                 default:
                      printf("Thank you visit again ....\n\n");
                      break;
            }
    }while(choice != 4);
  
    system("PAUSE");
    return 0;
}

SYBSc (CS) Sem III : Data Structure Slip 13 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 stack library (ststack.h) of integers using a static implementation of the stack and implementing the operations like init(S), S = push(S) and S = pop(S). Write a driver program that includes stack library and calls different stack operations. */

/// File name : "ststack.c"


#include"ststack.h"

int init(struct Stack *s)

{

    s->top = -1;

}

int isEmpty(struct Stack *s)

{

    if(s->top == -1)

    {

       return 1;

    }

    else{

       return 0;

    }

}

int isFull(struct Stack *s)

{

    if(s->top == MAX-1)

    {

       return 1;

    }

    else{

       return 0;

    }

}

int Push(struct Stack *s)

{

    int n;    

    if(isFull(s) == 1)

    {

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

       return -1;

    }

    else{

         printf("Enter the stack elements: ");

         scanf("%d", &n);

         s->top++;

         s->data[s->top] = n;

    }    

    return 0;

}

int Pop(struct Stack *s)

{

    if(isEmpty(s) == 1)

    {

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

       return -1;

    }

    else{

         return s->data[(s->top)--];

    }

}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "ststack.h"

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

struct Stack
{
       int data[MAX];
       int top;
};

int init(struct Stack *s);
int isEmpty(struct Stack *s);
int isFull(struct Stack *s);
int Push(struct Stack *s);
int Pop(struct Stack *s);


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

#include"ststack.h"

int main(int argc, char *argv[])
{
    struct Stack s;
    int choice = 0, n;
    
    do
    {
            printf("\n\nEnter your choice: \n1. Init \n2. Push \n3. Pop \n4. Exit \n\n");
            scanf("%d", &choice);
            
            switch(choice)
            {
                 case 1:
                      init(&s);
                      break;
                 case 2:
                      Push(&s);
                      break;
                 
                 case 3:
                      if ((n = Pop(&s)) != -1)
                         printf("Stack element : %d", n);
                      break;                        
                 default:
                      printf("Thank you visit again ....\n\n");
                      break;
            }
    }while(choice != 4);
    
    system("PAUSE");
    return 0;
}

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

  SAVITIBAI PHULE UNIVERSITY OF PUNE S. Y. B.Sc. (Computer Science) Semester III Practical Examination       SUBJECT: CS-233 Practical cours...