Stack Amd Queue
Stack Amd Queue
stack in/
s out
Methods
vector<T> v; // khoi tao
v.size();
v.push_back(x); // nap x vao dau phai
v.push_back(v.begin(), x); // nap x vao dau trai
v.push_back(v.begin()+p, x); // xen x vao truoc vi tri p
v.back(x); // xem dau phai
v.front(); // xem dau trai
v.pop_back(); // xoa dau phai
v.erase(v.end()-1); // xoa dau phai
v.erase(v.begin()); // xoa dau trai
sort(v.begin(), v.end());
Hien thi
for(int i = 0; i < v.size(); ++i) cout << " " << v[i];
VI w(v); // VI w; copy v -> w: Tao moi va gan tri
w.assign(v); // copy v -> w
v.clear(); // xoa noi dung van con bien v
v.empty();
Stack demo 1
/*
Name: stack
Copyright: (C) 2023
Author: Devcpp Fan
Date: 22-06-23 11:50
Description:
Tinh tri cua bieu thuc hau to (Lukasiewicz)
*/
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
void Go() {
cout << " ? ";
fflush(stdin);
if (cin.get()=='.') exit(0);
}
void Run() {
VI s;
for(int x = 1; x < 10; ++x)
s.push_back(x);
Print(s);
for(int i = 1; i <= 3; ++i) {
int x = s.back();
cout << "\n i = " << i << ". x = " << x;
s.erase(s.end()-1);
}
Print(s, "\n s: ");
}
main() {
Run();
cout << endl << "\n\n T h e E n d \n";
return 0;
}
Output 1
1 2 3 4 5 6 7 8 9
i = 1. x = 9
i = 2. x = 8
i = 3. x = 7
s: 1 2 3 4 5 6
Stack Demo 2
/*
Name: stack
Copyright: (C) 2023
Author: Devcpp Fan
Date: 22-06-23 11:50
Description:
Dung stack lat mang a[d..c]
*/
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> Stack;
#define Push(s,x) s.push_back(x)
void Go() {
cout << " ? ";
fflush(stdin);
if (cin.get()=='.') exit(0);
}
main() {
Run();
cout << endl << "\n\n T h e E n d \n";
return 0;
}
Output 2
Init a: 0 1 2 3 4 5 6 7 8 9
stack s: 3 4 5 6
stack s = empty:
Rev(a[3..6]): 0 1 2 6 5 4 3 7 8 9
stack s: 0 1 2 6 5 4 3 7 8 9
stack s = empty:
Rev(a[0..n-1]): 9 8 7 3 4 5 6 2 1 0
T h e E n d
Stack Demo3
/*
Name: stack
Copyright: (C) 2023
Author: Devcpp Fan
Date: 22-06-23 11:50
Description:
Tinh tri cua bieu thuc hau to (Lukasiewicz)
*/
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> Stack;
#define Push(s,x) s.push_back(x)
void Go() {
cout << " ? ";
fflush(stdin);
if (cin.get()=='.') exit(0);
}
void Run() {
// a bcdef ghijk lmnop qrstu vwxyz
// e = "(k+f)*(j+b)"; // (10+5)*(9+1) = 15*10 = 150
string h = "kf+jb+*"; // Hau to
cout << "\n (k+f)*(j+b) -> " << h;
Stack s;
int a, b;
for(int i = 0; i < h.length(); ++i) {
char c = h[i];
if(c >= 'a' && c <= 'z') {
Push(s,c-'a');
continue;
}
switch(c) {
case '+': a = Pop(s); b = Pop(s);
Push(s,b+a);
break;
case '-': a = Pop(s); b = Pop(s);
Push(s,b-a);
break;
case '*': a = Pop(s); b = Pop(s);
Push(s,b*a);
break;
case '/': a = Pop(s); b = Pop(s);
Push(s,b/a);
break;
} // switch
} // for
cout << "\n Final result: " << Pop(s);
}
main() {
Run();
cout << endl << "\n\n T h e E n d \n";
return 0;
}
Output 3
(k+f)*(j+b) -> kf+jb+*
Final result: 150
T h e E n d
1 2 3 4 5 6 7 8 9 10 11 12 13 14
m = 14, k = 3, res = 2
#include <bits/stdc++.h>
using namespace std;
void Go() {
cout << " ? ";
fflush(stdin);
if (cin.get()=='.') exit(0);
}
main() {
int n = 14, k = 3;
JP5(n,k);
cout << endl << "\n\n T h e E n d \n";
return 0;
}
If you want...Ver 6
/*
Name: JP.CPP (Ver. 6)
Copyright: (C) 2023
Author: Devcpp Fan
Date: 22-06-23 11:50
Description: Josephus Problem
'''
*/
#include <bits/stdc++.h>
using namespace std;
/*
vector<T> q;
q.push_back(x); // nap x vao dau phai
x = q.front(); // xem dau trai
q.erase(q.begin()); // xoa dau trai
v.size();
v.clear();
v.back();
v.front();
v.empty();
*/
class MyQueue {
public:
vector<int> Data;
MyQueue() { Data.clear(); }
void Go() {
cout << " ? ";
fflush(stdin);
if (cin.get()=='.') exit(0);
}
// Using Queue
int JP4(int n, int k) {
MyQueue q;
for(int i = 1; i <= n; ++i) q.Push(i);
q.Print("\n Init: ");
for(int i = 1; i < n; ++i) { // lap n-1 lan
// chuyen k-1 phan tu dau ve cuoi
for(int j = 1; j < k; ++j) q.Move();
q.Erase(); // bo phan tu thu k
q.Print("\n q: "); //Go();
} // i
int res = q.Front();
cout << "\n Result: " << res;
return res;
}
main() {
int n = 14, k = 3;
JP4(n,k);
cout << endl << "\n\n T h e E n d \n";
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int L, R, N;
int *a;
void Go() {
cout << " ? ";
fflush(stdin);
if (cin.get()=='.') exit(0);
}
main() {
int n = 14, k = 3;
JP7(n,k);
cout << endl << "\n\n T h e E n d \n";
return 0;
}
MyQueue Again
/*
Name: JP.CPP (Ver 8)
Copyright: (C) 2023
Author: Devcpp Fan
Date: 22-06-23 11:50
Description: Josephus Problem
simplest queue
'''
*/
#include <bits/stdc++.h>
using namespace std;
void Go() {
cout << " ? ";
fflush(stdin);
if (cin.get()=='.') exit(0);
}
class MyQueue {
public:
// Data
int * Data;
int Len;
int L, R;
// Methods
inline MyQueue(int n) {
Len = n;
Data = new int[Len];
fill(Data, Data + Len, 0);
L = 0; R = Len-1;
}
main() {
int n = 14, k = 3;
JP7(n,k);
cout << endl << "\n\n T h e E n d \n";
return 0;
}
Using stringstream
/*
Name: JP.CPP (Ver 9)
Copyright: (C) 2023
Author: Devcpp Fan
Date: 22-06-23 11:50
Description: Josephus Problem
stringstream
'''
*/
#include <bits/stdc++.h>
using namespace std;
void Go() {
cout << " ? ";
fflush(stdin);
if (cin.get()=='.') exit(0);
}
main() {
int n = 14, k = 3;
JP9(n,k);
cout << endl << "\n\n T h e E n d \n";
return 0;
}