-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathex-2-3-stackCheck.cpp
More file actions
63 lines (55 loc) · 1.37 KB
/
ex-2-3-stackCheck.cpp
File metadata and controls
63 lines (55 loc) · 1.37 KB
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
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
void enque(int *, int, int*, int, int);
void deque(int *, int *, int, int);
int main() {
int front=0;
int rear=0;
int N;
cout << "Insert the size of the stack: ";
cin >> N;
while(N>=160 || N<=0 || isalpha(N)!=0){
cin.clear();
while (cin.get() != '\n')
{
continue;
}
cout << "Wrong input, insert again: ";
cin >> N;
}
int stack[++N];
for(int i=0;i<N-1;i++){
int tmp;
cout << "Insert element " << i+1 << ": ";
cin >> tmp;
if(tmp>0)
enque(stack,front,&rear,N,tmp);
else if(tmp<0 && abs(tmp)==stack[front+1])
deque(stack,&front,rear,N);
else {
rear=1;
break;
}
}
if(front==rear)
cout<<"It's series of arrivals and departures"<<endl;
else
cout << "It's not series of arrivals and departures"<<endl;
return 0;
}
void enque(int *stack, int front, int *rear, int nMax, int x) {
if(front==((*rear)+1)%nMax){
cout << "Can't enque" << endl;
return;
}
*rear=(*rear+1)%nMax;
stack[(*rear)]=x;
}
void deque(int *stack, int *front, int rear, int nMax){
if((*front)==rear)
cout << "Can't deque" << endl;
*front=(*front+1)%nMax;
}