Skip to content

Instantly share code, notes, and snippets.

@bqcuong
Created June 13, 2020 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bqcuong/0bd7e39b0653921d26e3bb22cd38286c to your computer and use it in GitHub Desktop.
Save bqcuong/0bd7e39b0653921d26e3bb22cd38286c to your computer and use it in GitHub Desktop.
LTNC Exam
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Array {
private:
int n;
vector<int> data;
public:
Array(int n) {
this->n = n;
this->data = vector<int>(n);
}
Array(const Array& rhs) {
this->n = rhs.n;
this->data = vector<int>(n);
for (int i = 0; i < this->n; i++) {
this->data[i] = rhs.data[i];
}
}
Array& operator=(const Array& rhs) {
this->n = rhs.n;
this->data = vector<int>(n);
for (int i = 0; i < this->n; i++) {
this->data[i] = rhs.data[i];
}
return *this;
}
bool operator<(const Array& rhs) {
int minLen = this->n < rhs.n ? this->n : rhs.n;
for (int i = 0; i < minLen; i++) {
if (this->data[i] < rhs.data[i])
return true;
else if (this->data[i] > rhs.data[i])
return false;
else
i++;
}
return this->n < rhs.n;
}
Array operator+(const Array& rhs) {
int sumN = this->n + rhs.n;
Array sum(sumN);
int j = 0;
for (int i = 0; i < this->n; i++, j++) {
sum.data[j] = this->data[i];
}
for (int i = 0; i < rhs.n; i++, j++) {
sum.data[j] = rhs.data[i];
}
return sum;
}
Array* sort() {
std::sort(this->data.begin(), this->data.end());
return this;
}
friend istream& operator>>(istream& is, Array& arr) {
for (int i = 0; i < arr.n; i++) {
int element;
is >> element;
arr.data[i] = element;
}
return is;
}
friend ostream& operator<<(ostream& os, const Array& arr) {
for (int i = 0; i < arr.n; i++) {
os << arr.data[i] << " ";
}
os << endl;
}
};
int main() {
int n;
cin >> n;
Array A(n);
cin >> A;
cin >> n;
Array B(n);
cin >> B;
//cout << (A<B) << endl;
//Array C = A + B;
//cout << C;
Array C = *(A.sort()) + *(B.sort());
Array D = *((A+B).sort());
cout << C;
cout << D;
cout << (C<D) << endl;
return 0;
}
#include<iostream>
#include<vector>
using namespace std;
struct Node
{
int value;
Node* next;
};
void print(Node* head) {
while (head != NULL) {
cout << head->value << " ";
head = head->next;
}
cout << endl;
}
int findMax(Node* head) {
int maxElement = INT_MIN;
while (head != NULL) {
if (head->value > maxElement) {
maxElement = head->value;
}
head = head->next;
}
return maxElement;
}
Node* deleteAfter(Node* head, int x) {
Node* found = head;
while (found != NULL) {
if (found->value == x) {
break;
}
found = found->next;
}
if (found == NULL) return head;
if (found->next != NULL && found->next->next != NULL) {
found->next = found->next->next;
}
else {
found->next = NULL;
}
return head;
}
Node* insertBefore(Node* head, int x, int y) {
Node* found = head;
Node* prev = NULL;
while (found != NULL) {
if (found->value == x) {
break;
}
prev = found;
found = found->next;
}
Node* newNode = new Node;
newNode->value = y;
if (prev == NULL || found == NULL) {
newNode->next = head;
return newNode;
}
prev->next = newNode;
newNode->next = found;
return head;
}
int main() {
int n;
cin >> n;
Node* head = NULL;
Node* curr = NULL;
while (n--) {
int x;
cin >> x;
if (head == NULL) {
head = new Node;
head->value = x;
curr = head;
}
else {
Node* newNode = new Node;
newNode->value = x;
curr->next = newNode;
curr = newNode;
}
}
//print(head);
//cout << findMax(head) << endl;
// head = deleteAfter(head, 2);
// print(head);
// head = deleteAfter(head, 6);
// print(head);
// head = deleteAfter(head, 3);
// print(head);
head = insertBefore(head, 2, 10);
print(head);
head = insertBefore(head, 6, 7);
print(head);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment