Exam 2: Code Snippet (memorization) Flashcards

1
Q

Stack Implementation (First-In-Last-Out)

A

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; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Queue Implementation (First-In-First-Out)

A

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; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Iteration to Find Sum of Series (without for/while loops)
cpp

A

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 &laquo_space;“Sum of the series: “ &laquo_space;result &laquo_space;endl;
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Recursion to Find Sum of Series (without for/while loops)

A

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 &laquo_space;“Sum of the series: “ &laquo_space;result &laquo_space;endl;
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Linear Search Implementation

A

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; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly