栈的操作

顺序栈

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MAX_SIZE 10
 // 顺序栈 
typedef struct{
	int stack[MAX_SIZE];
	int p;
}*Stack,ListStack;


void  initStack(Stack s){ // 初始化栈,将栈指针指向-1; 
	
	s->p = -1;
	
} 
void  pushStack(Stack s,int data[],int length){
	int i;
	for(i=0;i<length;i++){
		s->stack[++(s->p)] = data[i];
	}
}
int pop(Stack S){
	return S->stack[(S->p)--];
}

bool isEmpty(Stack s){
	if(s->p == -1){
		return true;
	}else{
		return false;
	}
}

int main(){
	ListStack S;
	initStack(&S);
	printf("%d\n",sizeof(S));
	int data[] = {1,2,4,5,67,8,90};
	int i ;
	pushStack(&S,data,sizeof(data)/sizeof(int)); 
	for(i=0;i<sizeof(data)/sizeof(int);i++){
		printf("%d\t",pop(&S));
		printf("%d\n",isEmpty(&S));
	}
	printf("%d\n",isEmpty(&S));
}
posted @ 2020-10-23 15:30  philxling  阅读(35)  评论(0)    收藏  举报