?
Solved

How to determine palindrome using stack and queue?

Posted on 2004-08-12
2
Medium Priority
?
3,412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
Jase-Coder earned 150 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
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 …
Six Sigma Control Plans
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

765 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