Tuesday, October 15, 2024

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

No comments:

Post a Comment

To Connect Java program with the MYSQL Database

To connect java application with the MYSQL database, first of all to install MYSQL database in your machine. To install MYSQL in your mach...