Solved

How to determine palindrome using stack and queue?

Posted on 2004-08-12
2
3,400 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Using YubiKey with REST API application 2 82
endX challenge 2 50
wordmultiple challenge 12 92
groovy example issue 10 70
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now