Linear Data Structures: Implementation and Applications
Sequence List Range Deletion Given a sequence of integers, remove all elements that fall within a specified rangee [x, y] and output the remaining elements.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> numbers(n);
for (int i = 0; i < n; i++) {
cin >> numbers[i];
}
int lower, upper;
cin >> lower >> upper;
vector<int> result;
for (int num : numbers) {
if (num < lower || num > upper) {
result.push_back(num);
}
}
for (size_t i = 0; i < result.size(); i++) {
if (i > 0) cout << " ";
cout << result[i];
}
return 0;
}
Merging Sorted Linked Lists Merge two sorted linked lists in to a single sorted linked list.
#include <iostream>
using namespace std;
struct Node {
int value;
Node* next;
Node(int val) : value(val), next(nullptr) {}
};
Node* mergeLists(Node* list1, Node* list2) {
Node dummy(0);
Node* tail = &dummy;
while (list1 && list2) {
if (list1->value <= list2->value) {
tail->next = list1;
list1 = list1->next;
} else {
tail->next = list2;
list2 = list2->next;
}
tail = tail->next;
}
tail->next = list1 ? list1 : list2;
return dummy.next;
}
void printList(Node* head) {
while (head) {
cout << head->value;
if (head->next) cout << " ";
head = head->next;
}
}
int main() {
Node* list1 = nullptr;
Node* list2 = nullptr;
// Build first list
int val;
while (cin >> val && val != -1) {
Node* newNode = new Node(val);
newNode->next = list1;
list1 = newNode;
}
// Build second list
while (cin >> val && val != -1) {
Node* newNode = new Node(val);
newNode->next = list2;
list2 = newNode;
}
// Reverse lists to get them in correct order
Node* prev = nullptr;
Node* curr = list1;
while (curr) {
Node* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
list1 = prev;
prev = nullptr;
curr = list2;
while (curr) {
Node* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
list2 = prev;
Node* merged = mergeLists(list1, list2);
printList(merged);
return 0;
}
Intersection of Sorted Linked Lists Find the common elements between two sorted linked lists.
#include <iostream>
#include <vector>
using namespace std;
vector<int> findIntersection(const vector<int>& list1, const vector<int>& list2) {
vector<int> result;
int i = 0, j = 0;
while (i < list1.size() && j < list2.size()) {
if (list1[i] == list2[j]) {
result.push_back(list1[i]);
i++;
j++;
} else if (list1[i] < list2[j]) {
i++;
} else {
j++;
}
}
return result;
}
int main() {
vector<int> list1, list2;
int value;
// Read first list
while (cin >> value && value != -1) {
list1.push_back(value);
}
// Read second list
while (cin >> value && value != -1) {
list2.push_back(value);
}
vector<int> intersection = findIntersection(list1, list2);
if (intersection.empty()) {
cout << "NULL";
} else {
for (size_t i = 0; i < intersection.size(); i++) {
if (i > 0) cout << " ";
cout << intersection[i];
}
}
return 0;
}
Polynomial Operations Perform multiplication and addition on polynomials represented by coefficient-exponent pairs.
#include <iostream>
#include <vector>
#include <map>
using namespace std;
void multiplyPolynomials(const vector<pair<int, int>>& poly1,
const vector<pair<int, int>>& poly2,
map<int, int>& result) {
for (const auto& term1 : poly1) {
for (const auto& term2 : poly2) {
int exponent = term1.second + term2.second;
int coefficient = term1.first * term2.first;
result[exponent] += coefficient;
}
}
}
void addPolynomials(const vector<pair<int, int>>& poly1,
const vector<pair<int, int>>& poly2,
map<int, int>& result) {
for (const auto& term : poly1) {
result[term.second] += term.first;
}
for (const auto& term : poly2) {
result[term.second] += term.first;
}
}
void printPolynomial(const map<int, int>& poly) {
bool first = true;
for (auto it = poly.rbegin(); it != poly.rend(); ++it) {
if (it->second != 0) {
if (!first) cout << " ";
cout << it->second << " " << it->first;
first = false;
}
}
if (first) cout << "0 0";
}
int main() {
int n1, n2;
cin >> n1;
vector<pair<int, int>> poly1(n1);
for (int i = 0; i < n1; i++) {
cin >> poly1[i].first >> poly1[i].second;
}
cin >> n2;
vector<pair<int, int>> poly2(n2);
for (int i = 0; i < n2; i++) {
cin >> poly2[i].first >> poly2[i].second;
}
map<int, int> multiplicationResult;
map<int, int> additionResult;
multiplyPolynomials(poly1, poly2, multiplicationResult);
addPolynomials(poly1, poly2, additionResult);
printPolynomial(multiplicationResult);
cout << endl;
printPolynomial(additionResult);
return 0;
}
Polynomial Differentiation Compute the derivative of a polynomial.
#include <iostream>
#include <vector>
using namespace std;
vector<pair<int, int>> differentiate(const vector<pair<int, int>>& polynomial) {
vector<pair<int, int>> derivative;
for (const auto& term : polynomial) {
int coefficient = term.first;
int exponent = term.second;
if (exponent > 0) {
int newCoefficient = coefficient * exponent;
int newExponent = exponent - 1;
derivative.emplace_back(newCoefficient, newExponent);
}
}
return derivative;
}
void printPolynomial(const vector<pair<int, int>>& poly) {
bool first = true;
for (const auto& term : poly) {
if (!first) cout << " ";
cout << term.first << " " << term.second;
first = false;
}
}
int main() {
vector<pair<int, int>> polynomial;
int coefficient, exponent;
while (cin >> coefficient >> exponent) {
polynomial.emplace_back(coefficient, exponent);
}
vector<pair<int, int>> derivative = differentiate(polynomial);
printPolynomial(derivative);
return 0;
}
K-th Element from End of Linked List Find the k-th element from the end of a linked list.
#include <iostream>
#include <vector>
using namespace std;
int findKthFromEnd(const vector<int>& elements, int k) {
if (k <= 0 || k > elements.size()) {
return -1; // Or handle as appropriate for your use case
}
return elements[elements.size() - k];
}
int main() {
vector<int> elements;
int value, k;
cin >> k;
while (cin >> value && value >= 0) {
elements.push_back(value);
}
if (k > elements.size()) {
cout << "NULL";
} else {
cout << findKthFromEnd(elements, k);
}
return 0;
}