Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Structures and data algorithms / Exam 1 / ex3.cpp
blob: d1f0a31577c61c15e992f3895e457fe2e3021df6 (plain) (blame)
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
#include <iostream>
#include <queue>
#include <stack>
#include <string>

bool readAndDelete(std::stack<char>& st, std::string& word) {
	std::queue<char> backlog;
	int index = 0;
	while (!st.empty()) {
		if (st.top() == word[index])
			index++;
		else
			backlog.push(st.top());
		st.pop();
	}

	while (!backlog.empty()) {
		st.push(backlog.front());
		backlog.pop();
	}

	return index == word.length();
}

void printStack(std::stack<char>& st) {
	// Знам че принтирането е в обратен ред, бързам!
	while (!st.empty()) {
		std::cout << st.top() << std::endl;
		st.pop();
	}
	std::cout << std::endl;
}

int main() {
	std::string word1 = "mom";
	std::stack<char> st1;
	st1.push('m');
	st1.push('o');
	st1.push('o');
	st1.push('f');
	st1.push('m');
	std::cout << readAndDelete(st1, word1) << std::endl;
	printStack(st1);


	std::string word2 = "car";
	std::stack<char> st2;
	st2.push('y');
	st2.push('a');
	st2.push('d');
	st2.push('c');
	st2.push('c');
	std::cout << readAndDelete(st2, word2) << std::endl;
	printStack(st2);
}