-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprogram2.cpp
More file actions
75 lines (71 loc) · 1.55 KB
/
program2.cpp
File metadata and controls
75 lines (71 loc) · 1.55 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
64
65
66
67
68
69
70
71
72
73
74
75
#include<iostream>
#include<stack>
#include<conio.h>
using namespace std;
void check(stack <int>s)
{
/*
objective:given a stack of integers,check whether each successive pair of numbers in the stack is consecutive or not.
input parameters:
s-integer object of stack
return value:none
approach:-if size of stack is odd
pop an element from stack
while stack is not empty
pop two elements from stach
check whether they are consecutive or not
if elements of atleast one pair is not consecutive
print FALSE
otherwise
print TRUE
*/
int a,b,temp=1;
if(s.size()==0)
cout<<"ERROR!! (stack is empty)";
else if(s.size()==1)
cout<<"ERROR!! (ELEMENTS SHOULD BE ATLEAST 2)";
else
{
if(s.size()%2!=0)
s.pop();
while(!s.empty())
{
a=s.top();
s.pop();
b=s.top();
s.pop();
if(a!=b-1&&a!=b+1)
{
temp=0;
break;
}
}
if(temp)
cout<<"\tTRUE";
else
cout<<"\tFALSE";
}
}
int main()
{
/*
objective:given a stack of integers,check whether each successive pair of numbers in the stack is consecutive or not.
input parameters:
n-(integer value)-number of elements to be pushed into stack
ele-(integer value)-element to be pushed into stack
return value:none
approach:-by calling function check
*/
stack <int>s;
int ele,n;
cout<<"\n\tENTER SIZE(how much elements do want to insert into stack)";
cin>>n;
cout<<"\n\tENTER ELEMENTS:";
for(int i=0;i<n;i++)
{
cin>>ele;
s.push(ele);
}
check(s);
getch();
}