提出支持栈的Push和Pop操作以及第三种操作FindMin的数据结构,其中FindMin返回该数据结构的最小元素 所有操作在最坏的情况下的运行时间都是O(1)

数据结构与算法分析——c语言描述 练习3.22 a 答案


实现方法真是脑洞大开。。。。作者的思路

提出支持栈的Push和Pop操作以及第三种操作FindMin的数据结构,其中FindMin返回该数据结构的最小元素 所有操作在最坏的情况下的运行时间都是O(1)_第1张图片


stack.h

typedef int ElementType;
#ifndef _stack_h
#define _stack_h

struct StackRecord;
typedef struct StackRecord *Stack;

int IsEmpty(Stack s);

int IsFull(Stack s);
Stack CreateStack(int MaxElements);
void DisposeStack(Stack s);
void MakeEmpty(Stack s);

void Push(ElementType X, Stack s);

ElementType Top(Stack s);

void Pop(Stack s);

ElementType TopAndPop(Stack s);
ElementType FindMin(Stack s);
#endif

stack.c

#include"stack.h"
#include<stdlib.h>
#include"fatal.h"

#define EmptyTOS1 (-1)
#define MinStackSize (5)


static int IsEmpty2(Stack s);
static void MakeEmpty2(Stack s);
static void Push2(ElementType X, Stack s);
static ElementType Top2(Stack s);
static void Pop2(Stack s);
static ElementType TopAndPop2(Stack s);

struct StackRecord {
	int Capacity;
	int TopOfStack1;
	int TopOfStack2;
	ElementType *Array;
};

int IsEmpty(Stack s) {
	return s->TopOfStack1 == EmptyTOS1;
}

static int IsEmpty2(Stack s) {
	return s->TopOfStack2 == s->Capacity;
}

int IsFull(Stack s) {
	return s->TopOfStack1 + 1 == s->TopOfStack2;
}

Stack CreateStack(int MaxElements) {
	Stack s;
	if (MaxElements < MinStackSize)
		Error("Stack size is too small");
	s = malloc(sizeof(struct StackRecord));
	if (s == NULL)
		Error("out of space");
	else {
		s->Array = malloc(sizeof(ElementType)*MaxElements);
		if (s->Array == NULL)
			Error("out of space");
		else {
			s->Capacity = MaxElements;
			MakeEmpty(s);
			MakeEmpty2(s);
		}
	}
	return s;
}

void DisposeStack(Stack s) {
	if (s != NULL) {
		free(s->Array);
		free(s);
	}
}

void MakeEmpty(Stack s) {
	s->TopOfStack1 = -1;
	MakeEmpty2(s);
}

static void MakeEmpty2(Stack s) {
	s->TopOfStack2 = s->Capacity;
}

void Push(ElementType X, Stack s) {
	if (IsEmpty(s) || X < Top2(s)) {
		Push2(X, s);
		if (IsFull(s)) {
			Error("out of space");
			Pop2(s);
		}
		else
			s->Array[++s->TopOfStack1] = X;
	}
	else
	{
		if (IsFull(s)) {
			Error("out of space");
		}
		s->Array[++s->TopOfStack1] = X;
	}
}
static void Push2(ElementType X, Stack s) {
	if (IsFull(s))
		Error("out of space");

	s->Array[--s->TopOfStack2] = X;

}


ElementType Top(Stack s) {
	if (IsEmpty(s))
		Error("Empty stack");
	return s->Array[s->TopOfStack1];
}

static ElementType Top2(Stack s) {
	if (IsEmpty2(s))
		Error("Empty stack");
	return s->Array[s->TopOfStack2];
}

void Pop(Stack s) {
	if (IsEmpty(s))
		Error("Empty stack");
	if (Top(s) == Top2(s))
		Pop2(s);
	s->TopOfStack1--;
}

static void Pop2(Stack s) {
	if (IsEmpty2(s))
		Error("Empty stack");
	s->TopOfStack2++;
}

ElementType TopAndPop(Stack s) {
	if (IsEmpty(s))
		Error("empty stack");
	int x = Top(s);
	Pop(s);
	return x;
}

static ElementType TopAndPop2(Stack s) {
	if (IsEmpty2(s))
		Error("empty stack");
	return s->Array[s->TopOfStack2++];
}

ElementType FindMin(Stack s) {
	return Top2(s);
}



main.c

#include"stack.h"
#include<stdio.h>
int main() {
	Stack s = CreateStack(8);
	
	for (int i = 0; i <5; i++)
		Push(i, s);
	Push(-1, s);
	while (!IsEmpty(s)) {
		printf("%d\n", FindMin(s));
		Pop(s);
	}
	
	
}



你可能感兴趣的:(提出支持栈的Push和Pop操作以及第三种操作FindMin的数据结构,其中FindMin返回该数据结构的最小元素 所有操作在最坏的情况下的运行时间都是O(1))