C++之出棧和入棧


頭文件stack.h

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 #define N 10
 5 
 6 typedef struct stack
 7 {
 8     int top;
 9     int data[N];
10 }Stack;
11 
12 
13 void init(Stack p);
14 int isfull(Stack p);
15 int isempty(Stack p);
16 void popStack(Stack p);
17 void pushStack(Stack p, int key);
18 void show(Stack p);
19 int gettop(Stack p);
View Code

 

源文件stack.cpp

全部推入棧,然后再全部推出棧

 1 #include "stack1.h"
 2 #include <memory.h>
 3 
 4 void init(Stack * p)
 5 {
 6     p -> top = -1;
 7     memset(p->data, 0, sizeof(int) * N);
 8 }
 9 
10 int isfull(Stack * p)
11 {
12     if (p->top == N-1)
13     {
14         return 1;
15     }
16     else
17     {
18         return 0;
19     }
20 }
21 
22 int isempty(Stack * p)
23 {
24     if (p->top == -1)
25     {
26         return 1;
27     }
28     else
29     {
30         return 0;
31     }
32 }
33 
34 void popStack(Stack * p)
35 {
36     if (isempty(p))
37     {
38         return;
39     }
40     else
41     {
42         p -> top -= 1;
43     }
44 }
45 
46 void pushStack(Stack * p, int key)
47 {
48     if (isfull(p))
49     {
50         return;
51     }
52     else
53     {
54         p ->top += 1;
55         p->data[p->top] = key;
56     }
57 }
58 
59 
60 void show(Stack * p)
61 {
62     if (isempty(p))
63     {
64         return;
65     }
66     else
67     {
68         for (int i = 0; i <= p->top; i++)
69         {
70             printf("%d", p->data[i]);
71         }
72         printf("\n");
73     }
74 }
75 
76 int main(void)
77 {
78     int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
79     Stack p;
80     init(&p);
81 
82 
83     for (int i = 0; i < N; i++)
84     {
85         //進一個出一個;
86         pushStack(&p, a[i]);
87         show(&p);
88         popStack(&p);
89     }
90 
91     
92 
93     system("pause");
94     return 0;
95 }
View Code

 

源文件stack.cpp

入棧一個然后就推出

 1 #include "stack1.h"
 2 #include <memory.h>
 3 
 4 void init(Stack * p)
 5 {
 6     p -> top = -1;
 7     memset(p->data, 0, sizeof(int) * N);
 8 }
 9 
10 int isfull(Stack * p)
11 {
12     if (p->top == N-1)
13     {
14         return 1;
15     }
16     else
17     {
18         return 0;
19     }
20 }
21 
22 int isempty(Stack * p)
23 {
24     if (p->top == -1)
25     {
26         return 1;
27     }
28     else
29     {
30         return 0;
31     }
32 }
33 
34 void popStack(Stack * p)
35 {
36     if (isempty(p))
37     {
38         return;
39     }
40     else
41     {
42         p -> top -= 1;
43     }
44 }
45 
46 void pushStack(Stack * p, int key)
47 {
48     if (isfull(p))
49     {
50         return;
51     }
52     else
53     {
54         p ->top += 1;
55         p->data[p->top] = key;
56     }
57 }
58 
59 
60 void show(Stack * p)
61 {
62     if (isempty(p))
63     {
64         return;
65     }
66     else
67     {
68         for (int i = 0; i <= p->top; i++)
69         {
70             printf("%d", p->data[i]);
71         }
72         printf("\n");
73     }
74 }
75 
76 
77 int main(void)
78 {
79     int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
80     Stack p;
81     init(&p);
82 
83     for (int i = 0; i < N; i++)
84     {
85         pushStack(&p, a[i]);
86         show(&p);
87     }
88 
89     for (int i = N-1; i >= 0; i++)
90     {
91         popStack(&p);
92         show(&p);
93     }
94 
95     system("pause");
96     return 0;
97 }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM