用棧和佇列實現迴文判斷
阿新 • • 發佈:2019-02-04
#include<stdio.h>
#include<stdlib.h>
struct node;
typedef struct node *pnode;
struct node
{
char info;
pnode link;
};
struct linkstack
{
pnode top;
};
typedef struct linkstack *plinkstack;
plinkstack createnullstack()
{
plinkstack plstack;
plstack=(plinkstack)malloc(sizeof(struct linkstack));
if(plstack!=NULL)
{
plstack->top=NULL;
}
else printf("空棧建立失敗\n");
return plstack;
}
int isnullstack(plinkstack plstack)
{
return (plstack->top==NULL);
}
void push_stack(plinkstack plstack,char x)
{
pnode p;
p=(pnode)malloc(sizeof(struct node));
if(p==NULL)
{
printf("進棧結點申請失敗\n");
}
else
{
p->info=x;
p->link=plstack->top;
plstack->top=p;
}
}
void pop_stack(plinkstack plstack)
{
pnode p;
if(isnullstack(plstack))
{
printf("當前棧沒有元素\n");
}
else
{
p=plstack->top;
plstack->top=plstack->top->link;
free(p);
}
}
char top_stack(plinkstack plstack)
{
if(plstack->top==NULL)
{
printf("棧已空\n");
}
else return(plstack->top->info);
}
struct linkqueue
{
char data;
linkqueue *next;
};
typedef struct linkqueue *plinkqueue;
struct queue
{
plinkqueue r;
};
typedef struct queue *pqueue;
pqueue createnullqueue()
{
pqueue paqu;
paqu=(pqueue)malloc(sizeof(struct linkqueue));
if(paqu==NULL)
{
printf("建立空佇列失敗\n");
}
else paqu->r=NULL;
return paqu;
}
int isnullqueue(pqueue paqu)
{
return (paqu->r==NULL);
}
void enqueue(pqueue paqu,char x)
{
plinkqueue p,q;
p=(plinkqueue)malloc(sizeof(struct linkqueue));
if(p==NULL)
printf("申請入隊結點失敗\n");
else
{
p->data=x;
p->next=NULL;
if(paqu->r==NULL)
{
paqu->r=p;
paqu->r->next=paqu->r;
}
else
{
q=paqu->r->next;
paqu->r->next=p;
p->next=q;
paqu->r=p;
}
}
}
void dequeue(pqueue paqu)
{
plinkqueue p;
if(paqu->r->next==paqu->r)
{
p=paqu->r;
paqu->r=NULL;
free(p);
}
else
{
p=paqu->r->next;
paqu->r->next=paqu->r->next->next;
free(p);
}
}
char front_queue(pqueue paqu)
{
if(paqu->r==NULL)
{
printf("當前隊為空\n");
}
else return (paqu->r->next->data);
}
int main()
{
plinkstack stack;
pqueue paqu;
char data;
paqu=createnullqueue();
stack=createnullstack();
printf("請輸入迴文數,以#結束\n\n");
scanf("%c",&data);
while(data!='#')
{
push_stack(stack,data);
enqueue(paqu,data);
scanf("%c",&data);
}
while(!isnullqueue(paqu))
{
if(front_queue(paqu)==top_stack(stack))
{
dequeue(paqu);
pop_stack(stack);
}
else
{
printf("NO\n");
break;
}
}
if(isnullstack(stack))
printf("YES\n");
}
#include<stdlib.h>
struct node;
typedef struct node *pnode;
struct node
{
char info;
pnode link;
};
struct linkstack
{
pnode top;
};
typedef struct linkstack *plinkstack;
plinkstack createnullstack()
{
plinkstack plstack;
plstack=(plinkstack)malloc(sizeof(struct linkstack));
if(plstack!=NULL)
{
plstack->top=NULL;
}
else printf("空棧建立失敗\n");
return plstack;
}
int isnullstack(plinkstack plstack)
{
return (plstack->top==NULL);
}
void push_stack(plinkstack plstack,char x)
{
pnode p;
p=(pnode)malloc(sizeof(struct node));
if(p==NULL)
{
printf("進棧結點申請失敗\n");
}
else
{
p->info=x;
p->link=plstack->top;
plstack->top=p;
}
}
void pop_stack(plinkstack plstack)
{
pnode p;
if(isnullstack(plstack))
{
printf("當前棧沒有元素\n");
}
else
{
p=plstack->top;
plstack->top=plstack->top->link;
free(p);
}
}
char top_stack(plinkstack plstack)
{
if(plstack->top==NULL)
{
printf("棧已空\n");
}
else return(plstack->top->info);
}
struct linkqueue
{
char data;
linkqueue *next;
};
typedef struct linkqueue *plinkqueue;
struct queue
{
plinkqueue r;
};
typedef struct queue *pqueue;
pqueue createnullqueue()
{
pqueue paqu;
paqu=(pqueue)malloc(sizeof(struct linkqueue));
if(paqu==NULL)
{
printf("建立空佇列失敗\n");
}
else paqu->r=NULL;
return paqu;
}
int isnullqueue(pqueue paqu)
{
return (paqu->r==NULL);
}
void enqueue(pqueue paqu,char x)
{
plinkqueue p,q;
p=(plinkqueue)malloc(sizeof(struct linkqueue));
if(p==NULL)
printf("申請入隊結點失敗\n");
else
{
p->data=x;
p->next=NULL;
if(paqu->r==NULL)
{
paqu->r=p;
paqu->r->next=paqu->r;
}
else
{
q=paqu->r->next;
paqu->r->next=p;
p->next=q;
paqu->r=p;
}
}
}
void dequeue(pqueue paqu)
{
plinkqueue p;
if(paqu->r->next==paqu->r)
{
p=paqu->r;
paqu->r=NULL;
free(p);
}
else
{
p=paqu->r->next;
paqu->r->next=paqu->r->next->next;
free(p);
}
}
char front_queue(pqueue paqu)
{
if(paqu->r==NULL)
{
printf("當前隊為空\n");
}
else return (paqu->r->next->data);
}
int main()
{
plinkstack stack;
pqueue paqu;
char data;
paqu=createnullqueue();
stack=createnullstack();
printf("請輸入迴文數,以#結束\n\n");
scanf("%c",&data);
while(data!='#')
{
push_stack(stack,data);
enqueue(paqu,data);
scanf("%c",&data);
}
while(!isnullqueue(paqu))
{
if(front_queue(paqu)==top_stack(stack))
{
dequeue(paqu);
pop_stack(stack);
}
else
{
printf("NO\n");
break;
}
}
if(isnullstack(stack))
printf("YES\n");
}