堆栈定义

#include"stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
// #include <opencv2/opencv.hpp>
#define MAXSIZE 100
/*
// 定义堆栈的结构体
typedef struct SNode*Stack;
struct SNode {
// 数据域
int Data[MAXSIZE];
// 栈顶指针
int top;
};
// 入栈操作
void push(Stack s, int e) {
// 判断栈是否为满
if (s->top == MAXSIZE - 1)
printf("Stack if full.");
else {
s->Data[++(s->top)] = e;
}
}
// 出栈操作
int pop(Stack s) {
// 判断栈是否为空
if (s->top == -1)
return -1;
else {
return s->Data[s->top--];
}
}
// 双向栈
typedef struct SNode*DStack;
struct DStack {
int Data[MAXSIZE];
int top1;
int top2;
};
void Dpush(DStack*s, int e, bool tag) {
if (s->top2 - s->top1 == 1)
printf("Dstack is full.");
if (tag == 1)
s->Data[++(s->top1)] = e;
else {
s->Data[--(s->top2)] = e;
}
}
int pop(DStack*s, bool tag) {
if (tag == 1) {
if (s->top1 == -1){
printf("DStack 1 is empty.");
return NULL;
}else {
return s->Data[(s->top1)--];
}
}
else {
if (s->top2 == MAXSIZE) {
printf("DStack 2 is empty.");
return NULL;
}
else {
return s->Data[(s->top2)++];
}
}
}
*/
// 链栈
typedef struct SNode*StackLink;
struct SNode {
	// 数据域和指向下一个节点的指针
	int Data;
	struct SNode*next;
};
// 创建一个栈
StackLink createStack() {
	StackLink s;
	// 开辟空间
	s = (StackLink)malloc(sizeof(struct SNode));
	s->next = NULL;
	return s;
}
// 判空
int isEmpty(StackLink s) {
	return (s->next == NULL) ? 0 : 1;
}
// 入栈操作,在末尾插入
void push(StackLink s, int e) {
	struct SNode*p;
	p = (struct SNode*)malloc(sizeof(SNode));
	p->Data = e;
	p->next = s->next;
	s->next = p;
}
// 出栈操作,删除第一个节点
int pop(StackLink s) {
	struct SNode*p;
	int top_e;
	if (isEmpty(s)) {
		printf("Stack link is empty.");
		return NULL;
	}
	else {
		p = s->next;
		s->next = p->next;
		top_e = p->Data;
		free(p);
		return top_e;
	}
}


int main(int, char**)
{


	return 0;
}

猜你喜欢

转载自blog.csdn.net/oliverkingli/article/details/79801387