风言枫语  

           12321,你是不是你,这样的东西叫回文,由于队列和栈的存储方式不同,栈是LIFO,last in first out ,盘子一个一个堆,堆完后从上面开始拿;队列是FIFO,first  in first out, 就像现实的排队。将数字存进这两种结构中,逐一取出,如果相同,那就是回文数。


          StackAndQueqe.h

 

#include<stdio.h>
typedef char DataType;
typedef struct Node *PNode;
struct Node{
	DataType info;
	PNode link;
};
typedef struct LinkQueqe *PQueqe;
struct LinkQueqe{
	PNode f;
	PNode b;
};
typedef struct LinkStack *PStack;
struct LinkStack{
	PNode top;
};

PStack createStack();
int isEmptyStack(PStack pstack);
void pushStack(PStack pstack,DataType element);
DataType popStack(PStack pstack);
DataType topStack(PStack pstack); 
void printStack(PStack pstack);
PQueqe createQueqe();
int isEmptyQueqe(PQueqe pqueqe);
void pushQueqe(PQueqe pqueqe,DataType element);
DataType popQueqe(PQueqe pqueqe);
DataType topQueqe(PQueqe pqueqe); 
void printQueqe(PQueqe pqueqe);


StackAndQueqe.c

 


 

#include "StackAndQueqe.h"
PQueqe createQueqe(){
	PQueqe pqueqe = (PQueqe)malloc(sizeof(struct LinkQueqe));
	if(pqueqe == NULL) printf("create fail");
	pqueqe ->f = NULL;
	pqueqe ->b = NULL;
	return pqueqe;
}

int isEmptyQueqe(PQueqe pqueqe){
	return (pqueqe ->f == NULL);
}

void pushQueqe(PQueqe pqueqe,DataType element){
	PNode p = (PNode)malloc(sizeof(struct Node));
	if(p == NULL) printf("nothing push");
	p ->info = element;
	p ->link = NULL;
	if(pqueqe ->f == NULL){
		 pqueqe->f = p;
	//printf(" f  %5d\n",pqueqe->f->info);
	}
	else pqueqe ->b ->link = p;
	pqueqe ->b = p;
	//	printf("b   %d\n",pqueqe->b->info);
}

DataType popQueqe(PQueqe pqueqe){
	PNode p;
	DataType temp;
    if(isEmptyQueqe(pqueqe)) printf("queqe is empty");
    p = pqueqe ->f;
    temp = p->info;
    pqueqe ->f = p->link;
    free(p);  
    return temp;
}

DataType topQueqe(PQueqe pqueqe){
	if(pqueqe->f == NULL){
		printf("nothing top");
		return NULL;
	} 
	return pqueqe -> f->info ;
}

void printQueqe(PQueqe pqueqe){
	PNode p = pqueqe ->f;
    if(	isEmptyQueqe(pqueqe)){
		printf("nothing print");
	} 
	while(pqueqe->f!= NULL){
		printf("%3c",pqueqe->f->info);
		pqueqe ->f = pqueqe ->f ->link;
	} 
	pqueqe ->f = p;//此处的f随着link的变化变化  最后需要还原回去! 
}
PStack createStack(){
	PStack pstack = (PStack)malloc(sizeof(struct LinkStack));
	if(pstack == NULL) printf("create fail");
	pstack ->top = NULL;
	return pstack;
}

int isEmptyStack(PStack pstack){
	return(pstack->top == NULL);
}

void pushStack(PStack pstack,DataType element){
	PNode p = (PNode)malloc(sizeof(struct Node));
	if(p == NULL) printf("push fail");
	p ->info = element;
	p ->link = pstack ->top;
	pstack ->top = p;
}

DataType popStack(PStack pstack){
	PNode p;
	DataType temp;
	if(pstack ->top == NULL) printf("nothing pop");
	 p = pstack ->top;
	 temp = p->info;
	pstack ->top = pstack->top->link;
	free(p);
	return temp;	
}

DataType topStack(PStack pstack){
	if(pstack ->top == NULL){
		printf("nothing pop");
		return NULL;
	} 
	return pstack->top->info;
}

void printStack(PStack pstack){
	PNode p= pstack ->top; 
    if(pstack ->top == NULL){
		printf("nothing pop");
	} 
	while(pstack->top != NULL){
		printf("%3c",pstack ->top ->info);
		pstack ->top = pstack ->top ->link;
	} 
	pstack ->top = p;
}

 

#include "StackAndQueqe.h"

int main(){
	int i;
 	char s[100];
    int n=0;
    PQueqe pqueqe ; 
    PStack pstack;
	printf("please input string to judge whether it is a palindrome(回文数):\n");
    scanf("%c",&s[0]);
    while(s[n]!='\n')
    {
        scanf("%c",&s[++n]);
    }
     printf(" the length is %d: \n",n);
    printf(" the string is : ");
    for(i=0;i<n;i++){
    	printf("%c",s[i]);
    } 
 
	pqueqe = createQueqe();
    for(i=0;i<n;i++){
    	//printf("\n%c",s[i]);
    	pushQueqe(pqueqe,s[i]);
    } 
    pstack = createStack();
 	for(i=0;i<n;i++){
   	pushStack(pstack,s[i]);
    } 
     printf(" \nthe queqe is : ");
	printQueqe(pqueqe);
	 printf(" \nthe stack is : ");
	printStack(pstack);
	printf(" \n");
	
	for(i=0;i<n/2;i++){
		if(popQueqe(pqueqe)!= popStack(pstack)){
			printf("is not HUIWEN!\n");
			break;
		}
		else {
			printf("it is  HUIWEN!\n");
			break;
		} 
			
	}
	return 0;
}

 

     

       简单的话只需要取出的数切半对比就行了。


 

posted on 2013-08-17 23:02  风言枫语  阅读(2664)  评论(0编辑  收藏  举报