-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathQ3-1.cpp
94 lines (85 loc) · 1.4 KB
/
Q3-1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/****************************************************
题目描述:
实现一个栈的push、pop操作和min操作(返回栈中最小值),
要求push,pop和min函数的时间复杂度都为O(1)
Date:2014-03-26
*****************************************************/
/*
本程序采用数组模拟栈
*/
typedef int ElemType;
#define MAX 100 //栈的深度
#include<stdio.h>
/*
在栈顶索引指针为top时,向栈A中压入数据data
*/
bool push(int *A,int &top,ElemType data)
{
if(top>=MAX-1 || top<-1)
return false;
A[++top] = data;
return true;
}
/*
在栈顶索引指针为top时,出栈
*/
bool pop(int &top)
{
if(top<0)
return false;
top--;
return true;
}
/*
栈顶当前索引指针为top,Min数组最大深度也为MAX,
且Min的有效元素数与栈A中的元素个数相同,
它的对应位置用来保存栈A对应位置到栈底这一部分元素中的最小值
*/
void minAll(int *A,int *Min,int top)
{
if(top>MAX-1)
return ;
Min[0] = A[0];
int i;
for(i=1;i<=top;i++)
{
if(Min[i-1] > A[i])
Min[i] = A[i];
else
Min[i] = Min[i-1];
}
}
/*
返回栈顶为top时栈中元素的最小值
*/
int min(int *Min,int top)
{
return Min[top];
}
int main()
{
int A[MAX];
int top = -1;
push(A,top,4);
push(A,top,7);
push(A,top,2);
push(A,top,6);
push(A,top,3);
push(A,top,8);
push(A,top,5);
push(A,top,1);
int Min[MAX];
minAll(A,Min,7);
int i;
for(i=0;i<=top;i++)
printf("%d ",Min[i]);
printf("\n");
/*
int min7 = min(Min,7);
printf("%d\n",min7);
pop(top);
int min6 = min(Min,6);
printf("%d\n",min6);
*/
return 0;
}