Solved

How to determine palindrome using stack and queue?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

895 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

16 Experts available now in Live!

Get 1:1 Help Now