DSA lab 7a
DSA lab 7a
Islamabad, Pakistan
OBJECTIVE:
Obtained Marks:
Remarks:
struct Node
Node * next; // the next pointer of type “Node”, pointing to the next Node
in the sequence.
};
Write a function printList() that takes input a linked list's head pointer and iterates
through it and prints all node values in the sequence.
LIST A LIST B
Write a function that takes input two linked lists, and returns a union of them. Use
buildOneTwoThree() function implemented in 3 to get two lists and pass them to this
function to get the union.
8. Deleting a list
Remember, since the nodes are created on a heap, its your responsibility to release
the memory a list is occupying by deleting each and every node in that list.
Write a function named deleteList() which is when given a pointer to a list deletes all
of its nodes.
Implementation
#include <iostream>
Using namespace std;
// Node structure for the linked list
struct Node {
int data;
Node* next;
};
if (head == nullptr) {
// If the list is empty, make the new node the head
head = newNode;
} else {
// Traverse the list to find the last node and append the new
node
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
}
}
if (position == 0) {
// Insert at the beginning
newNode->next = head;
head = newNode;
} else {
// Traverse the list to the position-1 node
Node* current = head;
for (int i = 0; i < position - 1 && current != nullptr; ++i) {
current = current->next;
}}}
int main() {
// Create a linked list
Node* myList = nullptr;
return 0;
}