zoukankan      html  css  js  c++  java
  • 顺序栈

    顺序栈和顺序表基本相同,所以就当复习了

    #include<stdio.h>
    #include<stdlib.h> 
    #define maxsize 30
    int i;
    typedef int datatype;
    typedef struct{
    	datatype *data;
    	int top;
    }SeqStack; 
    
    int Initial(SeqStack *S){
    	S->data = (datatype*)malloc(maxsize*sizeof(datatype));
    	if(!S->data){
    		printf("内存分配失败");
    		exit(1); 
    	}
    	int x;
    	printf("请输入元素"); 
    	scanf("%d",&x);
        while(x!=0){
    		S->data[++S->top] = x;
    		scanf("%d",&x);
    		if(S->top>maxsize){
    			return 0;
    		}
    		}
    		return 1;
    }
    
    datatype Gettop(SeqStack *S){
    	if(S->top==-1)
    		return 0;
    	return S->data[S->top];
    } 
    
    int length(SeqStack *S){
    	return  S->top+1;
    }
    
    void show(SeqStack *S){
    	int i;
    	for(i = 0;i<=S->top;i++){
    		printf("栈中第%d个元素为:
    ",i+1);
    		printf("%d
    ",S->data[i]);
    	}
    }
    
    int Isfull(SeqStack *S){
    	return S->top==maxsize-1;
    }
    
    int Isempty(SeqStack *S){
    	  return S->top==-1;
    }
    
    int Push(SeqStack *S,datatype x){
    	if(S->top == maxsize-1)
    	   return 0;
    	   S->data[++S->top] = x;
    	   return 1;
    }
    
    int Pop(SeqStack *S){
    	if(S->top==-1)
    	  return 0;
    	S->top--;
    	return 1;
    }
    
    int DestroyStack(SeqStack *S){
    	S->top = -1;
    	return 1;
    }
    void menu(SeqStack *S)
    {
      	system("cls");
      	printf("
    
    
    
    ");
      	printf("		|---------------顺序栈--------------------------|
    ");
      	printf("		|					        |
    ");
      	printf("		|		 1.顺序栈初始化 	        |
    ");
      	printf("		|		 2.顺序栈的栈顶元素	        |
    ");
      	printf("		|		 3.顺序栈的长度	                |
    ");
      	printf("		|		 4.遍历顺序栈	                |
    ");
     	printf("		|		 5.判断顺序栈是否满栈	        |
    ");
     	printf("		|		 6.判断顺序栈是否为空	        |
    ");
      	printf("		|		 7.进栈	                        |
    ");
      	printf("		|		 8.出栈	                        |
    ");
      	printf("		|		 9.摧毁顺序栈	                |
    ");
        printf("		|					        |
    ");
      	printf("		|-----------------------------------------------|
    
    ");
      	printf("			请选择(1-9):");
      	int n;
    	scanf("%d",&n);
      	switch(n){
      		case 1:
      		system("cls");
    		  if(Initial(S))
    		    printf("初始化成功
    ");
    		  printf("按任意键返回菜单....");	
    		  getchar();
    		  getchar();
    		  menu(S);
      		case 2:
      			system("cls");
      			 if(!Gettop(S))
      			   printf("栈空"); 
      		    else
      		       printf("栈顶元素为:%d
    ",Gettop(S));
      		  printf("按任意键返回菜单....");	
    		  getchar();
    		  getchar();
    		  menu(S);
      		case 3:
      			system("cls");
      			  printf("栈的长度为:%d
    ",length(S));
      		  printf("按任意键返回菜单....");	
    		  getchar();
    		  getchar();
    		  menu(S);
      		case 4:
      			system("cls");
      			show(S);
    		  printf("按任意键返回菜单....");	
    		  getchar();
    		  getchar();
    		  menu(S);
      	    case 5:
      	    	system("cls");
      	    	  if(Isfull(S))
      	    	     printf("栈满");
      	    	  else
      	    	     printf("栈未满");
      	      printf("按任意键返回菜单....");
    		  getchar();
    		  getchar();
    		  menu(S);
      	    case 6:
      	    	system("cls");
      	    	  if(Isempty(S))
      	    	    printf("栈空
    ");
      	    	  else
    				printf("栈不为空
    ");  
    		  printf("按任意键返回菜单....");	
    		  getchar();
    		  getchar();
    		  menu(S); 
    		case 7:
    		    system("cls");
    		    printf("请输入进栈元素:
    ");
      	    	scanf("%d",&i);
    		     if(Push(S,i))
    			    printf("进栈成功
    ");
    			 else
    			    printf("栈满,进栈失败
    ");
    		  printf("按任意键返回菜单....");	
    		  getchar();
    		  getchar();
    		  menu(S);
    		case 8:
    			system("cls");
    			 if(Pop(S))
    			   printf("出栈成功
    "); 
    			else
    			   printf("栈空,请先初始化
    ");
    		  printf("按任意键返回菜单....");	
    		  getchar();
    		  getchar();
    		  menu(S);
    		case 9:
    		   system("cls"); 
    		   if(DestroyStack(S))
    		     printf("摧毁成功");
    		  printf("按任意键退出程序....");	
    		  getchar();
    		  getchar();
    		  exit(1);	
      		} 
    		  }
      		
    int main(){
    	int i;
    	SeqStack *S;
    	S = (SeqStack *)malloc(sizeof(SeqStack));
    	S->top = -1;
        menu(S);
    }
    
  • 相关阅读:
    camp待补
    ZOJ
    ZOJ
    ZOJ
    CodeForces
    CodeForces
    POJ 3278 Catch That Cow(简单BFS)
    POJ 2251 Dungeon Master(三维BFS)
    POJ 1218 THE DRUNK JAILER(类开灯问题,完全平方数)
    HDU 2053 Switch Game(开灯问题,完全平方数)
  • 原文地址:https://www.cnblogs.com/susususu/p/10753618.html
Copyright © 2011-2022 走看看