STRING PALINDROME USING STACK AND QUEUE

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

void push(char );
char pop();
void nq(char );
char dq();

typedef struct list
{
    char data;
    struct list *next;
}node;
node *top=NULL,*front=NULL,*rear=NULL;

int main()
{
    char word[20];
    printf("Type the Word:");
    scanf("%s",word);
    int length=strlen(word),i=0;
    while(word[i]!='\0')
    {
        push(word[i]);
        nq(word[i]);
        i++;
    }
    for(i=0;i<length;i++)
    {
        if(pop()!=dq())
        break;
       
    }
    if(i==length)
        printf("%s IS A PALINDROME WORD.\n",word);
    else
    {
        printf("%s IS NOT A PALINDROME WORD.\n",word);
    }
    return 0;
}
void push(char ch)
{
    node *new=(node*)malloc(sizeof(node));
    new->data=ch;
    new->next=top;
    top=new;
}

char pop()
{
    char ch;
    if(top==NULL)
    printf("Stack is empty,can't Pop\n");
    else
    {
        ch=top->data;
        top=top->next;
    }
    return ch;
}

void nq(char ch)
{
    node *new=(node*)malloc(sizeof(node));
    new->data=ch;
    new->next=NULL;
    if(front==NULL)
    {
        front=new;
        rear=new;
    }
    else
    {
        rear=front;
        while(rear->next!=NULL)
        {
            rear=rear->next;
        }
        rear->next=new;
    }   
}

char dq()
{
    char ch;
    if(front==NULL)
    printf("Queue is Empty, can't Deque\n");
    else
    {
        ch=front->data;
        front=front->next;
    }
    return ch;
}
Output:-
 sau@sau-desktop:~/snl$ cc ispalindrom.c
sau@sau-desktop:~/snl$ ./a.out
Type the Word:nitin
nitin IS A PALINDROME WORD.

Popular posts from this blog

8 Bit Plane Slicing of an image in Image Processing

Code to upload multiple files simultaneously using JSP, Servlet .