Final Practice Coding Segments

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/10

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

11 Terms

1
New cards

Traversing a singly LL

Node* temp = head;

while (temp) {

cout << temp->data << " ";

temp = temp->next;

}

2
New cards

Insert node at the head of LL in O(1)

Node* n = new Node{value, head};

head = n;

3
New cards

Insert a node at the end of a singly linked list (no tail pointer)

void insertAtTail(Node*& head, int value) {

Node* newNode = new Node();

newNode->data = value;

newNode->next = nullptr;

if (head == nullptr) { head = newNode; return; }

Node* temp = head;

while (temp->next != nullptr) temp = temp->next;

temp->next = newNode;

}

4
New cards

Insert node at tail of LL with tail pointer in O(1)

Node* newNode = new Node{value, nullptr};

if (!head) {

head = rear = newNode;

} else {

rear->next = newNode;

rear = newNode;

}

5
New cards

Delete first node of LL

if (!head) return;

Node* temp = head;

head = head->next;

delete temp;

6
New cards

Delete a node with specific value

if (!head) return;

if (head->data == value) {

Node* temp = head;

head = head->next;

delete temp;

return;

}

Node* p = head;

while (p->next && p->next->data != value)

p = p->next;

if (!p->next) return;

Node* temp = p->next;

p->next = temp->next;

delete temp;

7
New cards

Function to find duplicates in a 2D integer array

void find_duplicates(int arr[][COLS], int rows, int cols) {

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

int current = arr[i][j];

// Compare current to all later elements

for (int r = i; r < rows; r++) {

for (int c = (r == i ? j + 1 : 0); c < cols; c++) {

if (arr[r][c] == current) {

cout << current << " is duplicated" << endl;

}

}

}

}

}

}

8
New cards

Insert a node at the beginning of a singly linked list.

void insertAtHead(Node*& head, int value) {

Node* newNode = new Node();

newNode->data = value;

newNode->next = head;

head = newNode;

}

9
New cards

Print LL

void printList(Node* head) {
    while (head != nullptr) {
        cout << head->data << " -> ";
        head = head->next;
    }
    cout << "NULL" << endl;
}

10
New cards
Insert at tail function

void insertAtTail(Node*& head, int value) {
    Node* newNode = new Node();
    newNode->data = value;
    newNode->next = nullptr; // step 2

    if (head == nullptr) {   // step 3
        head = newNode;
        return;
    }

    Node* temp = head;
    while (temp->next != nullptr) { // step 4
        temp = temp->next;
    }
    temp->next = newNode; // step 5
}

11
New cards