Exam 2: Code Snippet (memorization) Flashcards
Stack Implementation (First-In-Last-Out)
include <iostream></iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
class Stack {
private:
Node* top;
public:
Stack() : top(nullptr) {}
void push(int value) { Node* newNode = new Node; newNode->data = value; newNode->next = top; top = newNode; } int pop() { if (top == nullptr) { cerr << "Stack is empty!" << endl; return -1; } int value = top->data; Node* temp = top; top = top->next; delete temp; return value; } ~Stack() { while (top != nullptr) { pop(); } } };
int main() {
Stack s;
// Add 7 to 10 elements for (int i = 1; i <= 8; i++) { s.push(i); } // Pop all elements in FILO order cout << "Popped elements: "; for (int i = 1; i <= 8; i++) { cout << s.pop() << " "; } return 0; }
Queue Implementation (First-In-First-Out)
include <iostream></iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
class Queue {
private:
Node* front;
Node* rear;
public:
Queue() : front(nullptr), rear(nullptr) {}
void enqueue(int value) { Node* newNode = new Node; newNode->data = value; newNode->next = nullptr; if (rear == nullptr) { front = rear = newNode; } else { rear->next = newNode; rear = newNode; } } int dequeue() { if (front == nullptr) { cerr << "Queue is empty!" << endl; return -1; } int value = front->data; Node* temp = front; front = front->next; if (front == nullptr) { rear = nullptr; } delete temp; return value; } ~Queue() { while (front != nullptr) { dequeue(); } } };
int main() {
Queue q;
// Add 7 to 10 elements for (int i = 1; i <= 8; i++) { q.enqueue(i); } // Dequeue all elements in FIFO order cout << "Dequeued elements: "; for (int i = 1; i <= 8; i++) { cout << q.dequeue() << " "; } return 0; }
Iteration to Find Sum of Series (without for/while loops)
cpp
include <iostream></iostream>
using namespace std;
double sumSeriesIterative() {
double sum = 0.0;
int denominator = 4;
// Using goto to simulate iteration without for/while loops loop_start: if (denominator <= 61) { sum += 1.0 / denominator; denominator += 3; goto loop_start; } return sum; }
int main() {
double result = sumSeriesIterative();
cout «_space;“Sum of the series: “ «_space;result «_space;endl;
return 0;
}
Recursion to Find Sum of Series (without for/while loops)
include <iostream></iostream>
using namespace std;
double sumSeriesRecursive(int denominator = 1) {
if (denominator > 61) {
return 0.0;
}
if (denominator == 1) {
return 1.0 + sumSeriesRecursive(4);
}
return (1.0 / denominator) + sumSeriesRecursive(denominator + 3);
}
int main() {
double result = sumSeriesRecursive();
cout «_space;“Sum of the series: “ «_space;result «_space;endl;
return 0;
}
Linear Search Implementation
include <iostream></iostream>
using namespace std;
int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i; // Return index of found element
}
}
return -1; // Return -1 if not found
}
int main() {
int arr[] = {23, 45, 21, 11, 36, 11, 42, 29, 33, 42, 16};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 42; // Example target value
int result = linearSearch(arr, size, target); if (result != -1) { cout << "Element " << target << " found at index " << result << endl; } else { cout << "Element " << target << " not found in the array" << endl; } return 0; }