1 /*
2 * question:
3 * design a stack containing min
4 * defines the stack data structure. A Min function must be added to obtain the minimum element of the stack.
5 * the time complexity of the min, push, and pop functions is required to be O (1)
6 */
7
8 /*
9 * analysis:
10 * I used to spend a day without coming up with a solution. After reading other people's analysis, I found a better solution.
11 * this is a typical problem of changing the time complexity with space complexity. The key point is how to save the smallest element.
12 * since the time complexity is low, the space complexity is high. The method is to design another parallel stack to save the minimum elements of the current stack.
13 * consider the normal Stack P and the stack pmin that saves the minimum values of P. In the initial state, P and pmin are empty. When the first element is pushed, P and pmin are pushed.
14 * for each next push, compare the elements to be pushed with the stack top elements in pmin. If the former is greater than the latter, only push P; otherwise,
15 * Push it to P and pmin. When taking pop into consideration, the elements at the top of the P Stack may only be greater than or equal to those at the top of the pmin stack.
16 * When the elements at the top of the P and pmin stacks are equal, both of them pop out. Otherwise, only the elements at the top of the P stack pop out.
17 * the following code is the implementation of the above idea.
18 */
19
20 # include "stdafx. H"
21 # include <stdio. h>
22 # include <stdlib. h>
23 /*
24 * define basic node pointers
25 */
26 typedef struct node * link;
27 struct Node
28 {
29 int value;
30 link next;
31 link Prev;
32 };
33 /*
34 * node initialization Function
35 */
36 link initnode (int I)
37 {
38 link t = (Link) malloc (sizeof (* t ));
39 t-> next = NULL;
40 TB-> Prev = NULL;
41 t-> value = I;
42 return T;
43}
44 /*
45 * Top P and pmin of stack P and stack pmin
46 */
47 link P, pmin;
48 /*
49 * Push Function
50 */
51 void push (int I)
52 {
53 If (P = NULL)
54 {
55 p = initnode (I );
56 pmin = initnode (I );
57}
58 else
59 {
60 p-> next = initnode (I );
61 p-> next-> Prev = P;
62 p = p-> next;
63 If (p-> value <= pmin-> value)
64 {
65 pmin-> next = initnode (I );
66 pmin-> next-> Prev = pmin;
67 pmin = pmin-> next;
68}
69}
70}
71 /*
72 * Pop Functions
73 */
74 link POP ()
75 {
76 link ret;
77 If (P = NULL)
78 {
79 printf ("has reached the bottom of the stack ");
80 ret = NULL;
81}
82 else
83 {
84 If (p-> Prev = NULL)
85 {
86 ret = P;
87 p = NULL;
88 pmin = NULL;
89}
90 else
91 {
92 If (p-> value = pmin-> value)
93 {
94 pmin = pmin-> Prev;
95 pmin-> next = NULL;
96}
97 ret = P;
98 P = p-> Prev;
99 p-> next = NULL;
100}
101}
102 return ret;
103}
104
105 int min ()
106 {
107 If (pmin = NULL)
108 {
109 printf ("has reached the bottom of the stack ");
110 return-1;
111}
112 else
113 return pmin-> value;
114}
115 int main (INT argc, char * argv [])
116 {
117 pushing (10 );
118 printf ("Min: % d \ n", min ());
119 pushing (7 );
120 printf ("Min: % d \ n", min ());
121 push (3 );
122 printf ("Min: % d \ n", min ());
123 Push (3 );
124 printf ("Min: % d \ n", min ());
125 pushing (8 );
126 printf ("Min: % d \ n", min ());
127 pushing (5 );
128 printf ("Min: % d \ n", min ());
129 push (2 );
130 printf ("Min: % d \ n", min ());
131 pushing (7 );
132 printf ("Min: % d \ n", min ());
133 printf ("START pop element \ n ");
134 printf ("Min: % d \ n", min ());
135 POP ();
136 printf ("Min: % d \ n", min ());
137 POP ();
138 printf ("Min: % d \ n", min ());
139 POP ();
140 printf ("Min: % d \ n", min ());
141 POP ();
142 printf ("Min: % d \ n", min ());
143 POP ();
144 printf ("Min: % d \ n", min ());
145 POP ();
146 printf ("Min: % d \ n", min ());
147 POP ();
148 printf ("Min: % d \ n", min ());
149 POP ();
150 printf ("Min: % d \ n", min ());
151 return 0;
152}
Test data: continuous push: 10, 7, 3, 3, 8, 5, 2, 6; then continuous POP: