-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq2.cpp
More file actions
168 lines (140 loc) · 2.54 KB
/
Copy pathq2.cpp
File metadata and controls
168 lines (140 loc) · 2.54 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
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
163
164
165
166
167
168
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
template <typename elem>
class slist;
template <typename elem>
class linkstack;
template <typename elem>
class slist {
private:
class node {
public:
elem val;
node* next;
node* prev;
};
public:
slist();
~slist();
bool empty() const;
void addfront(const elem& e);
void removefront();
const elem& front() const;
private:
node* head;
};
template <typename elem>
slist<elem>::slist() : head(NULL) {}
template <typename elem>
slist<elem>::~slist() {
while (!empty()){
removefront();
}
}
template <typename elem>
bool slist<elem>::empty() const{
return (head== NULL);
}
template <typename elem>
void slist<elem>::addfront(const elem& e){
node* toadd = new node;
toadd->val = e;
toadd->next = head;
head = toadd;
}
template <typename elem>
void slist<elem>::removefront() {
node* torem = head;
head = head->next;
delete torem;
}
template <typename elem>
const elem& slist<elem>::front() const{
return (head->val);
}
template <typename elem>
class linkstack{
public:
linkstack();
~linkstack();
int size() const;
bool empty() const;
void push(const elem& e);
void pop();
const elem& top() const;
private:
slist<elem> l;
int n;
};
template <typename elem>
linkstack<elem>::linkstack(): l(),n(0) {}
template <typename elem>
linkstack<elem>::~linkstack() {}
template <typename elem>
int linkstack<elem>::size() const{
return n;
}
template <typename elem>
bool linkstack<elem>::empty() const{
return (n==0);
}
template <typename elem>
void linkstack<elem>::push(const elem& e){
l.addfront(e);
n++;
}
template <typename elem>
void linkstack<elem>::pop(){
l.removefront();
n--;
}
template <typename elem>
const elem& linkstack<elem>::top() const{
return (l.front());
}
int main(){
ifstream input;
input.open("input2.txt",ios::in);
string line;
ofstream output;
output.open("2015csb1021-output2.txt", ios::out | ios::trunc);
while(getline(input,line)){
stringstream input_stream(line);
vector<int> inArray;
vector<int> ans;
linkstack<int> s;
int n=0;
int temp;
while(input_stream>>temp){
inArray.push_back(temp);
n++;
}
for(int i=0; i<n ; i++){
while (!s.empty() && inArray[i] > s.top()){
s.pop();
}
if (s.empty()){
ans.push_back(-1);
}
else {
ans.push_back(s.top());
}
s.push(inArray[i]);
}
for(int k=0; k<n;k++){
output<<ans[k];
if (k<n-1){
output<<" ";
}
else if (k == n-1){
output<<endl;
}
}
}
input.close();
output.close();
return 0;
}