Solved

How to determine palindrome using stack and queue?

Posted on 2004-08-12
2
3,404 Views
Last Modified: 2008-03-17
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;
}
0
Comment
Question by:lawrence_psk
2 Comments
 
LVL 11

Accepted Solution

by:
Jase-Coder earned 50 total points
ID: 11782439
well when you pop stuff off the stack you are getting the data in reverse order so once you have popped the data off check to see if it still spells the same word. If it does then it is a palindrome
0
 
LVL 1

Expert Comment

by:jandhb
ID: 12166215
can you show me what you changed to get this working?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question