Saturday, October 19, 2024

SYBSc (CS) Sem III : Data Structure Slip 20 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. There are lists where new elements are always appended at the end of the list. The list can be implemented as a circular list with the external pointer pointing to the last element of the list. Implement singly linked circular list of integers with append and display operations. The operation append(L, n), appends to the end of the list, n integers either accepted from user or randomly generated.*/

#include<stdio.h>

#include<stdlib.h>

struct node

{

int data;

struct node *next;

};

struct node * create(struct node *head)

{

struct node *newnode = NULL, *temp = NULL;

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

if(newnode!=NULL)

{

    printf("Enter node data");

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

    newnode->next = NULL;

   

    if(head == NULL)

        {

            head = newnode;

            newnode->next = head;

        }

        else

        {   

        temp = head;

       

        while(temp->next != head)

        {

                temp = temp->next;

            }

            temp->next = newnode;

            newnode->next = head;

         }

     }  

     return head;

}


struct node * append(struct node *head, int n)

{

struct node *newnode = NULL, *temp = NULL;

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

newnode->data = n;

newnode->next = NULL;

temp = head;

while(temp->next != head)

{

        temp = temp->next;

    }

    temp->next = newnode;

    newnode->next = head;    

    return head;

}


void display(struct node *head)

{

     struct node *temp = head; 

     do

    {

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

          temp = temp->next;

    }while(temp != head);

}

int main()

{

int num, i;

struct node *head = NULL;

        printf("Enter the number of nodes in list: ");

        scanf("%d", &num);

   

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

      head = create(head);

 

        display(head);


printf("\n enter data to be append:");

scanf("%d",&num);

head = append(head, num);

display(head);

system("pause");

return 0;

}


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>
#define MAX 30

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

int init(struct Queue *Q);
int isEmpty(struct Queue *Q);
int isFull(struct Queue *q);
int 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. addFront \n2. getRear \n3. deleteRear \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 20 Que - 2

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