By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 749 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

linked list

P: 5
I want to check if the string entered is a palindrome or not using the queues only
but I have done this using the queue and the array .I want to know how I can do this problem by using only the queues
code i have written is:


struct queue *getnode(struct queue *,struct queue*,char );
void display(struct queue *,int nc);
void pop(struct queue *front,char *k,int nc) ;
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct queue
{
char data;
struct queue *link;
};
void main()
{
struct queue *rear,*front;
char *k;
int i,nc;
clrscr();
printf("\n\nEnter the no. of characters you want to enter :");
scanf("%d",&nc);
front=NULL;
rear=NULL;
k=(char*)malloc(nc*sizeof(char));
for(i=0;i<nc;i++)
{
printf("\n\nEnter the %d character : ",i+1);
scanf("%s",k+i);
front=getnode(front,rear,*(k+i));
}
display(front,nc);
pop(front,k,nc);
getch();
}

struct queue *getnode(struct queue *front ,struct queue *rear ,char k)
{
struct queue *temp,*p;
if(front==NULL)
{
temp=(struct queue*)malloc(sizeof(struct queue));
front=temp;
rear=temp;
temp->data=k;
temp->link=front;
}
else
{
temp=front;
while(temp->link!=front)
{
temp=temp->link;
}
temp->link=(struct queue*)malloc(sizeof(struct queue));
temp=temp->link;
temp->data=k;
rear=temp;
temp->link=front;
}
return(front);
}

void display(struct queue *p,int nc)
{
int i;
struct queue *q;
printf("\n\n");
q=p;
for(i=0;i<nc;i++)
{
printf("%c\t",q->data);
q=q->link;
}
}

void pop(struct queue *front,char *k,int nc)
{
struct queue *temp;
int i,b=0;
temp=front;
for(i=0;i<nc/2;i++)
{
if(temp->data==*((k+nc-1)-i))
b=b+1;
temp=temp->link;
}
if(b==nc/2)
printf("\n\nString is a palindrome");
else
printf("\n\nString is not a palindrome");
}
Sep 26 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.