Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the stack data structure on page 222. Use it to determine whether a se

ID: 3558493 • Letter: I

Question

Implement the stack data structure on page 222. Use it to determine whether a sequence of 10 digits form a palindrome. Digits are entered from a keyboard with a space in between them.

Enter 10 digits:

1 2 3 4 8 4 2 5 3 3

THIS IS A PALINDROME

This is whats on page 222

#include <stdio.h>

#define STACK_SIZE 100

int contents [STACK_SIZE];

int top = 0;

void make_empty(void)

{

top=0;

}

bool is_full (void)

{

return top == STACK_SIZE;

}

void push (int i)

{

if (is_full())

stack_overflow();

else

contents[top++]=I;

}

int pop (void)

{

if (is_empty()

stack_underflow();

else

return contents [--top];

}

Explanation / Answer

Code:

#include<stdio.h>

#define STACK_SIZE 100

int contents[STACK_SIZE];

int top = 0;

void make_empty(void)

{

    top=0;

}

int is_empty(void)

{

    return top==0;

}

int is_full(void)

{

    return top == STACK_SIZE;

}

void push(int i)

{

    if(is_full()) printf("Stack overflow!");

    else contents[top++]=i;

}

int pop(void)

{

    if(is_empty()) printf("Stack underflowflow!");

    else return contents[--top];

}

int main()

{

    int i, size, num;

    printf("Enter number of digits you want to take : ");

    scanf("%d",&size);

    printf("Enter %d digits: ",size);

    for(i=0; i<size/2; i++)

    {

        scanf("%d",&num);

        push(num);

    }

    if(size%2) scanf("%d",&num);

    for(; i<size; i++)

    {

        scanf("%d",&num);

        if(num!=pop()) break;

    }

    if(i==size) printf("THIS IS A PALINDROME!");

    else printf("THIS IS NOT A PALINDROME!");

    return 0;

}