Link to home
Start Free TrialLog in
Avatar of lawrence_psk
lawrence_psk

asked on

How to determine palindrome using stack and queue?

I hv do my works, but i have a problem between it. CAn somebody help me to solve it...

#include <stdio.h>
#include <string.h>
#define SIZE 100

typedef char item_t;

typedef struct {
      item_t Items[SIZE];
      int top;
}Stack_t;

typedef struct{
      int count,front, rear;
      item_t Item[SIZE];
}Queue_t;

void InitialiseStack (Stack_t *S)
{      S->top=0;      }

int emptystack (Stack_t S)
{      return (S.top==0);      }

int fullstack (Stack_t S)
{      return (S.top==SIZE);      }


void push (char x,Stack_t *S)
{
      if(fullstack(*S))
            printf("stack overflow\n");
      else{
                  S->Items[S->top]=x;
                  ++(S->top);
      }
}

void pop (Stack_t *S, char *x)
{
      if(emptystack(*S))
            printf("stack empty\n");
      else{
            --(S->top);
            *x=S->Items[S->top];
      }
}

void InitialiseQueue (Queue_t *Q)
{
      Q->count=0;
      Q->front=0;
      Q->rear=-1;
}

int emptyq (Queue_t Q)
{      return (Q.count==0);      }

int fullq (Queue_t Q)
{      return (Q.count==SIZE);      }

void insert(item_t y, Queue_t *Q)
{
      if (fullq(*Q))
            printf("queue overflow\n");
      else{
            Q->rear=(Q->rear+1)%SIZE;
            Q->Item[Q->rear]=y;
            Q->count++;
      }
}

void Remove (Queue_t *Q, item_t *y)
{
      if(emptyq(*Q))
            printf("queue empty\n");
      else{
            *y=Q->Item[Q->front];
            Q->front=(Q->front+1)%SIZE;
            --(Q->count);
      }
}

main ()
{
      Stack_t S;
      Queue_t Q;
      item_t input[SIZE],Q_output[SIZE]="",S_output[SIZE]="";
      int i,length;

      InitialiseStack(&S);
      InitialiseQueue(&Q);

      printf("Enter a words to reverse> ");
      for(i=0,input[i]=getchar();input[i]!='\n';i++,input[i]=getchar())
      {
            insert(input[i],&Q);
            push(input[i],&S);
      }

      length=strlen(input);

      for(i=0;i<length;i++)
      {
            while(!emptystack(S)){
                  Remove(&Q,&Q_output[i]);
                  pop(&S,&S_output[i]);
            }
      
            if(Q_output[i]!=S_output[i])
            {
                  printf("\nThe words are not palindrome\n");
                  return 0;
            }

            else if(Q_output[i]==S_output[i])
                  printf("\n%s",S_output[i]);
      }
      
      putchar('\n');

      return 0;
}
ASKER CERTIFIED SOLUTION
Avatar of Jase-Coder
Jase-Coder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jandhb
jandhb

can you show me what you changed to get this working?