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


No comments:

Post a Comment

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