Data Structures Practical 16

Question 1
Write a C program to perform following operations on Empty stack: PUSH (10), PUSH (20), POP, PUSH (10), PUSH (20), POP, POP, POP, PUSH (20),POP.
stack.c
C
				#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define max 7
int stack[max]; 
int top = -1;   

void push(int element);
int pop();
void display();

void main()
{
    int poppedVal;
    clrscr();

    printf("--- Executing Sequential Stack Operations ---\n\n");

    printf("Operation: PUSH(10)\n");
    push(10);
    display();

    printf("Operation: PUSH(20)\n");
    push(20);
    display();

    printf("Operation: POP\n");
    poppedVal = pop();
    if (poppedVal != -1) printf("Popped: %d\n", poppedVal);
    display();

    printf("Operation: PUSH(10)\n");
    push(10);
    display();

    printf("Operation: PUSH(20)\n");
    push(20);
    display();

    printf("Operation: POP\n");
    poppedVal = pop();
    if (poppedVal != -1) printf("Popped: %d\n", poppedVal);
    display();

    printf("Operation: POP\n");
    poppedVal = pop();
    if (poppedVal != -1) printf("Popped: %d\n", poppedVal);
    display();

    printf("Operation: POP\n");
    poppedVal = pop();
    if (poppedVal != -1) printf("Popped: %d\n", poppedVal);
    display();

    printf("Operation: PUSH(20)\n");
    push(20);
    display();

    printf("Operation: POP\n");
    poppedVal = pop();
    if (poppedVal != -1) printf("Popped: %d\n", poppedVal);
    display();

    printf("\nExecution completed. Press any key to exit...");
    getch();
}

void push(int element)
{
    if (top == max - 1) 
    {
        printf("Stack is Full.\n");
        return;
    }
    top++;                
    stack[top] = element; 
}

int pop()
{
    int num;
    if (top == -1) 
    {
        printf("Stack Underflow! ");
        return -1;
    }
    num = stack[top];
    top--;
    return (num); 
}

void display()
{
    int i;
    if (top == -1) 
    {
        printf("Stack is empty\n");
        return;
    }
    printf("Current Stack Status: ");
    for (i = top; i >= 0; i--)
        printf("%d ", stack[i]); 
    printf("\n------------------------------------\n");
}

			
Output
Question 2
Write a C program to reverse a String using Stack.
strreverse.c
C
				#include <stdio.h>
#include <conio.h>
#include <string.h>

#define MAX 50

char stack[MAX]; /* Declaring a character stack array */
int top = -1;    /* Declaring and initializing the stack pointer */

void push(char ch);
char pop();

void main()
{
    char str[MAX];
    int i, len;

    clrscr();

    printf("Enter a string to reverse: ");
    scanf("%s",str); 

    len = strlen(str);

    for (i = 0; i < len; i++)
    {
        push(str[i]);
    }

    for (i = 0; i < len; i++)
    {
        str[i] = pop();
    }

    printf("\nReversed String is: %s\n", str);

    getch();
}

void push(char ch)
{
    if (top == MAX - 1)
    {
        printf("\nStack Overflow!\n");
        return;
    }
    top++;
    stack[top] = ch;
}

char pop()
{
    if (top == -1)
    {
        printf("\nStack Underflow!\n");
        return '\0';
    }
    return stack[top--];
}

			
Output
Question 3
Write a C program to check whether the given string is palindrome or not using stack.
strpalindrom.c
C
				#include <stdio.h>
#include <conio.h>
#include <string.h>

#define MAX 50

char stack[MAX]; 
int top = -1;    

void push(char ch);
char pop();

void main()
{
    char str[MAX];
    int i, len;
    int flag = 0; 

    clrscr();

    printf("Enter a string: ");
    scanf("%s",str); 

    len = strlen(str);

    for (i = 0; i < len; i++)
    {
        push(str[i]);
    }

    for (i = 0; i < len; i++)
    {
        if (str[i] != pop())
        {
            flag = 1;
            break;
        }
    }

    if (flag == 0)
    {
        printf("\nThe string \"%s\" is a Palindrome.\n", str);
    }
    else
    {
        printf("\nThe string \"%s\" is NOT a Palindrome.\n", str);
    }

    getch();
}

void push(char ch)
{
    if (top == MAX - 1)
    {
        printf("\nStack Overflow!\n");
        return;
    }
    top++;
    stack[top] = ch;
}

char pop()
{
    if (top == -1)
    {
        printf("\nStack Underflow!\n");
        return '\0';
    }
    return stack[top--];
}

			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *