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 13 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 

/* Q2 Write a program that sorts the elements of linked list using bubble sort technique */

#include<stdio.h>

#include<stdlib.h>


struct Node

{

        int data;

        struct Node *Next;

}*Head, *Last;


void CreateList()

{

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

    

    if (NewNode != NULL)

    {

             printf("Enter the data value: ");

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

             NewNode->Next = NULL;             

            

            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;

    }

}


void SortLinkedList()

{

     struct Node *current = Head, *index = NULL;

     int temp;

     

     if (Head != NULL)

     {

           while (current != NULL)

           {

                 index = current->Next;

                 

                 while (index != NULL)

                 {

                       if (current->data > index->data)

                       {

                             temp = current->data;

                             

                             current->data = index->data;

                             

                             index->data = temp;

                       }

                       

                       index = index->Next;

                 }

                 

                 current = current->Next;

           }

     }                                 

}


int main()

{

    int pos = 0, choice = 0;

    

    do

    {

            printf("\n\nEnter your choice: \n1. Create \n2. Display \n3. Sort the List \n4. Exit \n\n");

            scanf("%d", &choice);

            

            switch(choice)

            {

                 case 1:

                      CreateList();

                      break;

                 case 2:

                      Display();

                      break;

                 

                 case 3:

                      SortLinkedList();

                      break;                        

                 default:

                      printf("Thank you visit again ....\n\n");

                      break;

            }

    }while(choice != 4);

    

    system("pause");

    return 0;

}

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;
}

Sunday, October 6, 2024

SYBSc (CS) Sem III : Data Structure Slip 8 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 C program to check whether the contents of two stacks are identical. Use stack library to perform basic stack operations. Neither stack should be changed.*/

/// 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 s1, s2;
    int n, i, num, flag = 0;
    
    init(&s1);
    
    printf("How many number we have to enter in stack 1 : ");
    scanf("%d", &n);
    printf("Enter the stack elements: ");
    
    for (i = 0; i < n; i++)
    {
        scanf("%d", &num);
        Push(&s1, num); 
    }
    
    init(&s2);
    
    printf("How many number we have to enter in stack 2 : ");
    scanf("%d", &n);
    printf("Enter the stack elements: ");
    
    for (i = 0; i < n; i++)
    {
        scanf("%d", &num);
        Push(&s2, num); 
    }
       
    while(!isEmpty(&s1) && !isEmpty(&s2))
    {
        if(Pop(&s1) != Pop(&s2))
        {
             flag = 0;
             break;
        }
        
        flag = 1;       
    }
    
    if( flag == 1)
        printf("The contents of two stacks are identical.\n\n");
    else
        printf("The contents of two stacks are not identical.\n\n");
    
    system("PAUSE");
    return 0;
}


SYBSc (CS) Sem III : Data Structure Slip 8 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 list library (singlylist.h) for a singly linked list of integer with the operations create, delete specific element and display. Write a menu driven program to call these operations.*/

/// File name : "singlylist.h"

#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 DeleteAtPosition(int Position)

{

    struct node *temp = Head, *temp1 = NULL;

    int  i = 0;

    

    if (Position == 1)

    {

       Head = temp->Next;

       free(temp);

    }

    else

    {

        for (i = 2; i < Position; i++)

        {

            if (temp->Next->Next != NULL)

            {

                  temp = temp->Next;

            }

        }

        temp1 = temp->Next;

        temp->Next = temp1->Next;

        

        free(temp1);

    }

}

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;

    }

}


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

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

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

void CreateList();
void DeleteAtPosition(int Position);
void Display();


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

#include"singlylist.h"

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


SYBSc (CS) Sem III : Data Structure Slip 7 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 Sort a random array of n integers (accept the value of n from user) in ascending order by using quick sort algorithm */


#include<stdio.h>

#include<stdlib.h>

#define MAX 100


void CreateArray(int A[], int n)

{     

     int i = 0;

        

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

          A[i] = rand(); 

}


void DisplayArray(int A[], int n)

{

     int i = 0;

     

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

          printf("%d \t", A[i]); 

}


void QuickSort(int A[], int low, int high)

{     

     int pivot, t, i, j;

     

     if ( low <= high)

     {

          pivot = A[low];

          i = low + 1;

          j = high;

          

          while ( i <= j)

          {

                while ( pivot >= A[i] && i <= high)

                      i++;

           

                while ( pivot < A[j] && j >=  low)

                      j--;

                

                if ( i < j)

                {

                     t = A[i];

                     A[i] = A[j];

                     A[j] = t;

                }

          }

          

          A[low] = A[j];

          A[j] = pivot;

          

          QuickSort(A, low, j-1);

          QuickSort(A, j+1, high);

     }

}


int main(){

    int A[MAX], num = 0;

    

    printf("Enter How many element we want to in array: ");

    scanf("%d", &num);

    

    CreateArray(A, num);

    

    printf("\n\nThe given array elements are:  \n\n");

    DisplayArray(A, num);

    

    QuickSort(A, 0, num-1);

    printf("\n\n");

    DisplayArray(A, num);    

    printf("\n\n");

    system("pause");

    return 0;

}


SYBSc (CS) Sem III : Data Structure Slip 7 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 checks whether a string of characters is palindrome or not. The function should use a stack library (cststack.h) of stack of characters using a static implementation of the stack. */

/// File name : "cststack.c"

#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)--];

    }

}

char Peek(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);
char Peek(struct Stack *s);


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

#include"cststack.h"

int main(int argc, char *argv[])
{
    struct Stack s;
    char str[MAX], temp[MAX];
    int i = 0;
    
    init(&s);
    
    printf("Enter the given String :: ");
    scanf("%s", str);
    
    while(str[i] != '\0')
    {
       Push(&s, str[i]);
       i++;
    }
    
    i = 0;
    
    while(!isEmpty(&s))
    {
        temp[i] = Pop(&s);
        i++;
    }
    
    if(strcmp(str, temp) == 0)
    {
        printf("Given string is palindrome.\n\n");
    }
    else
    {
        printf("Given string is not a palindrome.\n\n");
    }
    
    system("PAUSE");
    return 0;
}

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;
}

SYBSc (CS) Sem III : Data Structure Slip 5 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 implement the below two 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 4 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

/* Write a program to reverse the elements of a queue using queue library. Implement basic queue operations init, enqueue, dequeue. */

// File name : "Queue.c"

#include"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 enqueue(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 dequeue(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]);

}

void Reverse(struct Queue *q)

{

     int x;     

     if (isEmpty(q) == 1)

        return;         

     x = dequeue(q);     

     Reverse(q);     

     enqueue(q, x);              

}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// File name : "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 enqueue(struct Queue *q, int x);

int dequeue(struct Queue *q);

void Display(struct Queue *q);

void Reverse(struct Queue *q);


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// File name : main.c


#include"Queue.h"

int main(int argc, char *argv[])
{
  struct Queue q;
    
    int ch, num;
    
    init(&q);
    
    do{
       printf("\n1. Insert \n2. Reverse \n3. Display \n4. Exit \n");
       scanf("%d", &ch);
       
       switch(ch)
       {
            case 1: 
                 printf("Enter the element to insert into queue: ");
                 scanf("%d", &num);
                 
                 enqueue(&q, num);                 
                 break;
                 
            case 2:
                 Reverse(&q);
                 break;
            
            case 3:
                 Display(&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 14 Que - 2

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