zoukankan      html  css  js  c++  java
  • 数据结构 用栈和队列判断回文数

               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
    ",pqueqe->f->info);
    	}
    	else pqueqe ->b ->link = p;
    	pqueqe ->b = p;
    	//	printf("b   %d
    ",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(回文数):
    ");
        scanf("%c",&s[0]);
        while(s[n]!='
    ')
        {
            scanf("%c",&s[++n]);
        }
         printf(" the length is %d: 
    ",n);
        printf(" the string is : ");
        for(i=0;i<n;i++){
        	printf("%c",s[i]);
        } 
     
    	pqueqe = createQueqe();
        for(i=0;i<n;i++){
        	//printf("
    %c",s[i]);
        	pushQueqe(pqueqe,s[i]);
        } 
        pstack = createStack();
     	for(i=0;i<n;i++){
       	pushStack(pstack,s[i]);
        } 
         printf(" 
    the queqe is : ");
    	printQueqe(pqueqe);
    	 printf(" 
    the stack is : ");
    	printStack(pstack);
    	printf(" 
    ");
    	
    	for(i=0;i<n/2;i++){
    		if(popQueqe(pqueqe)!= popStack(pstack)){
    			printf("is not HUIWEN!
    ");
    			break;
    		}
    		else {
    			printf("it is  HUIWEN!
    ");
    			break;
    		} 
    			
    	}
    	return 0;
    }

         

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


  • 相关阅读:
    找出互联网符合的产品实例
    以软件周期来说明不同的测试的使用情况
    scrapy多个page爬取, post请求, 通过爬到的URL继续发请求爬页面
    Scrapy 安装, 基础使用, 持久化存储
    Linux nginx+uWSGI+django+virtualenv+supervisor发布web服务器
    Linux Nginx
    Linux virtualenv, virtualenvwrapper, pip freeze
    Linux Python安装
    Redis, Python操作Redis, Linux操作Redis, Redis命令, Redis发布订阅, Redis持久化, Redis主从同步
    爬虫 selenium
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3265412.html
Copyright © 2011-2022 走看看