zoukankan      html  css  js  c++  java
  • 用一个数组实现两个堆栈

    #include <stdio.h>
    #include <stdlib.h>
    #define ElementType int
    const int MAXSIZE = 10;
    typedef struct Node *DStack;
    typedef struct Node{
    	ElementType Data[MAXSIZE];
    	int last0;
    	int last1;
    };
    
    bool Push(int flag,DStack &S, ElementType X){//0代表插入栈0,1代表插入栈1 
    	//printf("------------------------------------
    ");
    	if(S->last0==S->last1-1){
    		printf("栈满
    ");
    		return 0;
    	}
    	//printf("------------------------------------
    ");
    	if(flag==0){
    		S->Data[++S->last0] = X;
    		return true;
    	}else if(flag == 1){
    		S->Data[--S->last1]=X;
    		return true;
    	}else{
    		printf("插入序号有误
    ");
    		return false;
    	}
    }
    ElementType Pop(int flag,&DStack S){
    	if((flag==0 && S->last0==-1)||(flag==1 && S->last1 == MAXSIZE)){
    		printf("栈空");
    		return 0;
    	}
    	if(flag ==0)
    		return S->Data[S->last0--];
    	else if(flag ==1)
    		return S->Data[S->last1++];
    	else{
    		printf("插入序号有误
    ");
    		return 0;
    	}
    }
    void InintDStack(DStack &S){
    	S = (DStack)malloc(sizeof(struct Node));
    	S->last0 = -1;
    	S->last1= MAXSIZE;
    }
    int main(){
    	DStack S;
    	InintDStack(S);
    	
    	for(int i = 0;i<12;i++){
    		Push(i%2,S,i);
    	}
    	for(int i=0;i<12;i++){
    		printf("%d
    ",Pop(i%2,S));
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    利用LibreOffice进行WORD转PDF
    SpringBoot实践
    Solr学习笔记(一)
    HashMap原理(转)
    PDF.js展示本地文件
    设计模式之代理模式
    (一)DUBBO基本学习
    如何架构一个框架
    冒泡排序
    js 函数传数组参数
  • 原文地址:https://www.cnblogs.com/zangkuo/p/6143462.html
Copyright © 2011-2022 走看看