Sunday, October 6, 2024

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

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