-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeqstack.cpp
162 lines (126 loc) · 2.68 KB
/
Seqstack.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "iostream"
using namespace std;
#include "stdlib.h"
#define maxsize 10
typedef struct stack{
int data[maxsize];
int top;
}Stack;
//===============init a stack==========
void initStack(Stack *&s)
{
s=(Stack *)malloc(sizeof(Stack));
s->top=-1;
}
//==============push()===============
int push(Stack *&s,int val)
{
if(s->top==maxsize-1)
{
cout<<"The stack is up overflow! Can't write more!"<<endl;
return 0;
}
else{
s->top++;
// cout<<s->top<<' '; test 'top'
s->data[s->top]=val;
return 1;
}
}
//====================================
//==========pop operation=============
int pop(Stack *&s) //
{
if(s->top==-1)
{
cout<<"Down overflow occured!"<<endl;
return 0;
}
else{
int temp;
temp=s->data[s->top];
s->top--;
cout<<temp<<endl;
return temp;
}
}
//========================destory a array stack============
void destroyStack(Stack *&s)
{
free(s);
}
//========================dispay a stack============
void displayStack(Stack *&s)
{
int pointer=s->top;
while(pointer!=-1)
{
cout<<s->data[pointer]<<" ";
pointer--;
}
cout<<endl<<endl;
}
//===================return lenth of the stack===========
int getLength(Stack *s)
{
int len;
len=(s->top)+1;
cout<<"Stack length is: "<<len<<endl;
return len;
}
//===========is empty===========================
void isEmpty(Stack *&s)
{
if(s->top==-1)
{
cout<<"Empty stack!"<<endl;
}
else
{
cout<<"Stack is Not Empty."<<endl<<endl;
}
}
//==============================================
//====================Algortham================
// write a function, test the string wheather a symmetrical string , try to use Stack to solve it.
//because the data is 'int',so I define a number array to replace it;
void symmetry(int *str)
{
Stack *p;
initStack(p);
int flag;
for(int i=0;i<7;i++)
{
push(p,str[i]);
}
displayStack(p);
int m=0;
while(p->top!=-1)
{
int temp=pop(p);
if(temp==str[m])
{
flag=1;
}
else{
flag=0;
break;
}
m++;
}
flag?cout<<"It is a symmetrical string."<<endl:cout<<"Not a symmetrical string."<<endl;
}
//============================================
int main(){
Stack *s;
initStack(s);
for(int i=0;i<12;i++)
{
push(s,i);
}
displayStack(s);
isEmpty(s);
int strings[]={5,2,3,4,3,2,1};
symmetry(strings);
return 0; //2018.5.24
}