NO

Author Topic: palindrome stack  (Read 6173 times)

manichandra

  • Guest
palindrome stack
« on: February 14, 2012, 04:31:37 AM »
can anyone tell  me about how to write a code which will tell a string whether its a palindrome or no using stacks  ( which ignores case , space and punctuation ??

i did this part


#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct node
{
        char ch;
        struct node* link;
}NODE;


typedef struct stack
{
        int count;
        NODE *top;
}STACK;

STACK* CreateStack();
char PushStack(STACK* sk, char ch);
char PopStack(STACK* sk);
int EmptyStack(STACK* sk);
int CheckPalindrome(STACK* s,STACK* r);
void PopTop(STACK* sk, char *chOut);

STACK* CreateStack()
{
    STACK* sk = (STACK*) malloc(sizeof(STACK));
    sk->count = 0;
    sk->top = NULL;
    return sk;
}
char PushStack(STACK* sk, char ch)
{
    NODE* pnew = (NODE*) malloc (sizeof(NODE));
    pnew->ch = ch;         
    pnew->link = sk->top; 
    sk->top = pnew;       
    sk->count++;           
    return pnew->ch;
}

char PopStack(STACK* sk)
{
    NODE* temp;
    char pop;
    pop=sk->top->ch;       
    temp = sk->top;         
    sk->top = sk->top->link;
    sk->count--;           
    free(temp);
    return pop;
}

int EmptyStack(STACK* sk)
{
    return sk->count==0;
}

int CheckPalindrome(STACK* s,STACK* r)
{
    char st,re,temp;
    int x=0;
    while (!EmptyStack(s))
    {
          st=PopStack(s); 
          re=PopStack(r); 
          if (st!=re)
             x++;
     }
     return x;   
}

void PopTop(STACK* sk, char *chOut)
{
    *chOut = sk->top->ch;
}

main()
{
      char str[50],rev[50],temp; 
      int i=0,x;
      char *ptr, *revptr;         
      printf ("Enter the string\n");
      scanf("%[^\n]s",str);       
      STACK* stk=CreateStack();   
      STACK* tempstk=CreateStack();
      ptr = str;                   
      printf("\nHere is the stack using linked list: ");
      while(*ptr)       
      {
        temp=PushStack(tempstk, *ptr);
        temp=PushStack(stk, *ptr); 
        printf("%c ",temp);
        ptr++;
      }
      ptr = str;

    while (!EmptyStack(tempstk))
    {
          temp=PopStack(tempstk);
          rev=temp;
          i++;
    }
    rev='\0';
    STACK* revstk=CreateStack();
    printf("\nHere is the Reverse Stack using Linked list: ");
    revptr = rev;
      while(*revptr)
      {
       temp=PushStack(revstk, *revptr);
       printf("%c ",temp);
       revptr++;
      }
    x=CheckPalindrome(stk,revstk);
    if(x==0)
    printf("\n The string is Palindrome.");
    else
    printf("\n The string is not Palindrome.");
}

CommonTater

  • Guest
Re: palindrome stack
« Reply #1 on: February 14, 2012, 05:10:39 AM »
A stack/linked list is massive overkill for that....

1) get your input string.
2) remove all whitespace
3) compare first character and last character
4) exit if different
5) move first and last pointers
6) loop back to step 3
7) when first and last pointers reach the middle of the string, it's a palendrome.

Roughly 30 lines of code required...




manichandra

  • Guest
Re: palindrome stack
« Reply #2 on: February 14, 2012, 05:24:08 AM »
thanks tator, is there any way using stack ??

CommonTater

  • Guest
Re: palindrome stack
« Reply #3 on: February 14, 2012, 07:30:21 AM »
thanks tator, is there any way using stack ??

There probably is... but why? 

If you want to experiment with stacks that's one thing but you should try to find something more reasonably suited to the storage technique... like storing temporary variables or building a call stack.  Stacks are basically first in - last out mechanisms where you push something on then pop it off in the reverse order... not very well suited to a double ended array function.

As I said before... simple is smart.   You should always look for the simplest solution to any problem...

Compare the complexity of your half-finished stacks implementation with this palendrome test...
Code: [Select]
// untested

int isPalendrome(char *str)
  {  char *l = str;
      char *r = str + strlen(str);
      do
         {  if ( *r != *l )
               return 0; }
      while ( r-- > l++ );
      return 1; }

Which do you think is better?
« Last Edit: February 14, 2012, 07:35:10 AM by CommonTater »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: palindrome stack
« Reply #4 on: February 14, 2012, 07:10:53 PM »
thanks tator, is there any way using stack ??

There probably is... but why?
Maybe because it is a requirement of the assignment given by the teacher?  ::)

Ralf

CommonTater

  • Guest
Re: palindrome stack
« Reply #5 on: February 14, 2012, 07:30:56 PM »
thanks tator, is there any way using stack ??

There probably is... but why?
Maybe because it is a requirement of the assignment given by the teacher?  ::)

Ralf

Didn't think of that... good point.

Well Mani... is it homework?


manichandra

  • Guest
Re: palindrome stack
« Reply #6 on: February 19, 2012, 03:43:54 AM »
yes that was an assignment.  but i am really interested in learning this . thats y i asked u guys.

and from this forum i am learning impotanant points and i am gaining knowledge. thanks guys..

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: palindrome stack
« Reply #7 on: February 19, 2012, 05:53:21 AM »
yes that was an assignment.  but i am really interested in learning this . thats y i asked u guys.

and from this forum i am learning impotanant points and i am gaining knowledge. thanks guys..
No problem in asking as long as it is noticeable that you are trying to do your part as well.

It is kind of an unwritten "law" that in a forum (or mailing list for that matter) like this, nobody will do the homework/assignment for you. You will get hints/suggestions to a certain part of a problem you encounter, but nobody should give you the complete answer, doing all the work for you.

Ralf

CommonTater

  • Guest
Re: palindrome stack
« Reply #8 on: February 19, 2012, 05:33:18 PM »
yes that was an assignment.  but i am really interested in learning this . thats y i asked u guys.

and from this forum i am learning impotanant points and i am gaining knowledge. thanks guys..


All well and good, my friend...  But it might help if you say you're working on an assignment instead of something personal.  Had I known, my answers would have been *very* different.  As Bitbessier points out, helping someone with homework is a different task than helping someone solve a real world programming problem.