Maaz Hussain
2023282
SECTION A
ASSIGGNMENT 01
QUESTION 01
A. Output: 18
B. Output: 25
C. Output: 16
D. Output: 35
QUESTION 02
a.list->info >= 18
list->info == 18, so true.
b. list->link == A
If A points to the second node (info = 25), this will be true.
c. A->link->info == 16
If A points to the node with info 25, then A->link points to the
node with 35, so this is false.
d. B->link == NULL
If B is pointing to the node with 10, this is true because it’s the
last node.
e. list->info == 18
list->info is 18, so true.
QUESTION 03
a. A = B;
Valid: You are assigning one pointer to another.
b. list->link = A->link;
Valid: Assigns A->link to list->link.
c. list->link->info = 45;
Valid: Changes the info value of the second node to 45.
d. *list = B;
Invalid: You cannot directly assign one structure to another like
this without defining a deep copy.
e. *A = *B;
Invalid: Same reason as above, direct structure assignment is
not allowed.
f. B = A->link->info;
Invalid: A->link->info is an integer, but B is a pointer.
g. A->info = B->info;
Valid: This is valid since you are assigning one node’s info to
another.
h. list = B->link->link;
Valid: Assigns a pointer to a new position in the list.
i. B = B->link->link->link;
Valid: Advances B three nodes ahead.
QUESTION 04
1.A = list->link->link;
2. list = list->link->link->link;
3. while(B->link != NULL)
B = B->link;
4. list = NULL;
5. A->info = 35;
6. nodeType* newNode = new nodeType;
newNode->info = 10;
newNode->link = A->link;
A->link = newNode;
7. nodeType* temp = A->link;
A->link = A->link->link;
delete temp;
QUESTION 05
OUTPUT…..
QUESTION 02……
1. CODE…..
#include <iostream>
#include <chrono> // For measuring time
using namespace std;
int main() {
int sum = 0;
int n;
// Get input for n
cout << "Enter value of n: ";
cin >> n;
// Start measuring time
auto start = chrono::high_resolution_clock::now();
// Code fragment: O(n)
for (int i = 0; i < n; i++) {
sum++;
// Stop measuring time
auto stop = chrono::high_resolution_clock::now();
auto duration =
chrono::duration_cast<chrono::microseconds>(stop -
start);
// Output the result and time taken
cout << "Sum: " << sum << endl;
cout << "Time taken: " << duration.count() << "
microseconds" << endl;
return 0;
Time Complexity Analysis:
The loop runs n times, and each iteration takes constant time.
Hence, the time complexity is O(n).
2.
CODE……
#include <iostream>
#include <chrono> // For measuring time
using namespace std;
int main() {
int sum = 0;
int n;
// Get input for n
cout << "Enter value of n: ";
cin >> n;
// Start measuring time
auto start = chrono::high_resolution_clock::now();
// Code fragment: O(n^2)
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sum++;
// Stop measuring time
auto stop = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::microseconds>(stop
- start);
// Output the result and time taken
cout << "Sum: " << sum << endl;
cout << "Time taken: " << duration.count() << " microseconds" <<
endl;
return 0;
Time Complexity:
The time complexity of this fragment is O(n²)
because both loops run n times.
3.
CODE……
#include <iostream>
#include <chrono> // For measuring time
using namespace std;
int main() {
// Loop over several values of n
for (int n = 10; n <= 100; n += 10) {
int sum = 0;
// Start measuring time
auto start = chrono::high_resolution_clock::now();
// Code fragment: O(n^3)
for (int i = 0; i < n; i++) {
for (int j = 0; j < n * n; j++) {
sum++;
}
}
// Stop measuring time
auto stop = chrono::high_resolution_clock::now();
auto duration =
chrono::duration_cast<chrono::microseconds>(stop -
start);
// Output the result and time taken
cout << "n: " << n << " | Sum: " << sum << " |
Time taken: " << duration.count() << " microseconds"
<< endl;
}
return 0;
}
Big-O Notation:
Thus, the time complexity of this code is O(n³).
because outer loops is n time and the inner loops n*n,
so the result is n*n*n is that.
4.
Big-O Notation:
The outer loop n time and outer loop is also I time then
we apply the sumation formula and the complexcity
which gives us a time complexity of O(n²).
CODE…..
#include <iostream>
#include <chrono> // For measuring time
using namespace std;
int main() {
// Loop over several values of n
for (int n = 10; n <= 100; n += 10) {
int sum = 0;
// Start measuring time
auto start = chrono::high_resolution_clock::now();
// Code fragment: O(n^2)
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
sum++;
}
}
// Stop measuring time
auto stop = chrono::high_resolution_clock::now();
auto duration =
chrono::duration_cast<chrono::microseconds>(stop -
start);
// Output the result and time taken
cout << "n: " << n << " | Sum: " << sum << " |
Time taken: " << duration.count() << " microseconds"
<< endl;
}
return 0;
}
5.
Outer Loop: The outer loop runs n times (from i = 0
to i < n).
Middle Loop: The middle loop runs i2i^2i2 times
(from j = 0 to j < i * i).
Inner Loop: The inner loop runs j times (from k = 0 to
k < j).
Apply the summation for all three lop they will give a
complexicity which is O(n5).
CODE……
#include <iostream>
#include <chrono> // For measuring time
using namespace std;
int main() {
// Loop over several values of n
for (int n = 1; n <= 10; n++) { // Using smaller n to
avoid long execution times
int sum = 0;
// Start measuring time
auto start = chrono::high_resolution_clock::now();
// Code fragment: O(n^5)
for (int i = 0; i < n; i++) {
for (int j = 0; j < i * i; j++) {
for (int k = 0; k < j; k++) {
sum++;
}
}
}
// Stop measuring time
auto stop = chrono::high_resolution_clock::now();
auto duration =
chrono::duration_cast<chrono::microseconds>(stop -
start);
// Output the result and time taken
cout << "n: " << n << " | Sum: " << sum << " |
Time taken: " << duration.count() << " microseconds"
<< endl;
}
return 0;
}
6..
Time Complexity Analysis (Big-O)
1.Outer Loop: The outer loop runs from 1 to n-
1, which is n−1n - 1n−1 iterations
(approximately n).
2.Middle Loop: The middle loop runs from 1 to
i2−1i^2 - 1i2−1, which means it runs i2i^2i2
times for each iteration of the outer loop.
3.Inner Loop: The inner loop runs j times (from
0 to j-1).apply the summation process we get
the below results.
Thus, the time complexity of this code is O(n^5).
CODE……
#include <iostream>
#include <chrono> // For measuring time
using namespace std;
int main() {
// Loop over several values of n
for (int n = 1; n <= 10; n++) { // Using smaller n to
avoid long execution times
int sum = 0;
// Start measuring time
auto start = chrono::high_resolution_clock::now();
// Code fragment: O(n^5)
for (int i = 1; i < n; i++) {
for (int j = 1; j < i * i; j++) {
if (j % 1 == 0) { // This condition is always
true
for (int k = 0; k < j; k++) {
sum++;
}
}
}
}
// Stop measuring time
auto stop = chrono::high_resolution_clock::now();
auto duration =
chrono::duration_cast<chrono::microseconds>(stop -
start);
// Output the result and time taken
cout << "n: " << n << " | Sum: " << sum << " |
Time taken: " << duration.count() << " microseconds"
<< endl;
}
return 0;
}
QUESTION…..03
CODE…..
#include <iostream>
#include <cmath> // For logarithmic functions
using namespace std;
int main() {
// Given constants
double timeForN1000 = 1.0; // 1 ms for N = 1000
int N1000 = 1000; // Input size of 1000
double timeLimit = 60000.0; // 1 minute in
milliseconds
// a) Linear (O(N))
double linearN = timeLimit / (timeForN1000 /
N1000);
cout << "Max N for Linear (O(N)): " << linearN <<
endl;
// b) Logarithmic (O(log N))
double kLog = timeForN1000 / log10(N1000);
double logN = pow(10, (timeLimit * kLog)); // Using
base 10
cout << "Max N for Logarithmic (O(log N)): " <<
logN << " (very large)" << endl;
// c) O(N log N)
double kNLogN = timeForN1000 / (N1000 *
log10(N1000));
double nLogNMax = 0;
for (int i = 1; ; i++) {
double temp = i * log10(i);
if (temp > (timeLimit * kNLogN)) {
nLogNMax = i;
break;
}
}
cout << "Max N for O(N log N): " << nLogNMax <<
endl;
// d) Quadratic (O(N^2))
double quadN = sqrt(timeLimit * (1000 * 1000) /
timeForN1000);
cout << "Max N for Quadratic (O(N^2)): " << quadN
<< endl;
// e) Cubic (O(N^3))
double cubicN = cbrt(timeLimit * (1000 * 1000 *
1000) / timeForN1000);
cout << "Max N for Cubic (O(N^3)): " << cubicN <<
endl;
return 0;
}
QUESTION 04…
CODE……
1.
CODE…
#include <iostream>
using namespace std;
struct node {
int data;
struct node* next;
};
// Function to display all elements of the linked list
void display(struct node* head) {
if (head == nullptr) {
cout << "List is empty." << endl;
return;
}
struct node* current = head;
while (current) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
int main() {
struct node* head = nullptr; // Initialize an empty list
// Assuming nodes are added here; omitted for
brevity
display(head); // Call the display function
return 0;
}
2.
CODE…
#include <iostream>
using namespace std;
struct node {
int data;
struct node* next;
};
// Function to add an element to the end of the linked
list
struct node* addback(struct node* head, int data) {
struct node* newNode = new node();
newNode->data = data;
newNode->next = nullptr;
if (!head) {
return newNode; // If the list is empty, return the
new node
}
struct node* current = head;
while (current->next) {
current = current->next; // Traverse to the end
}
current->next = newNode; // Link the new node
return head; // Return the unchanged head
}
int main() {
struct node* head = nullptr; // Initialize an empty list
head = addback(head, 10); // Add first element
head = addback(head, 20); // Add second element
return 0;
}
3….
CODE…
#include <iostream>
using namespace std;
struct node {
int data;
struct node* next;
};
// Function to find an element in the linked list
struct node* find(struct node* head, int data) {
struct node* current = head;
while (current) {
if (current->data == data) {
return current; // Return the node if found
}
current = current->next;
}
return nullptr; // Return nullptr if not found
}
int main() {
struct node* head = nullptr; // Initialize an empty list
// Assuming nodes are added here; omitted for
brevity
struct node* foundNode = find(head, 10); // Search
for a node
if (foundNode) {
cout << "Node found: " << foundNode->data <<
endl;
} else {
cout << "Node not found." << endl;
}
return 0;
}
4…..
CODE….
#include <iostream>
using namespace std;
// Define the structure for a node in the linked list
struct node {
int data;
struct node* next;
};
// Function to delete a specified node
struct node* delnode(struct node* head, struct node*
pelement) {
if (head == nullptr || pelement == nullptr) {
return head; // Nothing to delete
}
// If the node to be deleted is the head
if (head == pelement) {
struct node* temp = head;
head = head->next; // Move head to the next node
delete temp; // Free the old head
return head; // Return new head
}
// Find the node before the node to be deleted
struct node* current = head;
while (current->next != nullptr && current->next !=
pelement) {
current = current->next;
}
// If the node to be deleted was found
if (current->next == pelement) {
current->next = pelement->next; // Bypass the
node to delete it
delete pelement; // Free the memory
}
return head; // Return the head of the list
}
int main() {
struct node* head = nullptr; // Initialize an empty list
head = addback(head, 10); // Adding elements
head = addback(head, 20);
head = addback(head, 30);
// Delete an element
struct node* foundNode = find(head, 20);
head = delnode(head, foundNode); // Delete the
found node
// Display the list to verify deletion
display(head); // Should show "10 -> 30 -> nullptr"
return 0;
}
5…
CODE…
#include <iostream>
using namespace std;
// Define the structure for a node in the linked list
struct node {
int data;
struct node* next;
};
// Function to free the entire linked list
void freelist(struct node* head) {
struct node* current = head;
struct node* nextNode;
while (current != nullptr) {
nextNode = current->next; // Store the next node
delete current; // Free the current node
current = nextNode; // Move to the next node
}
}
int main() {
struct node* head = nullptr; // Initialize an empty list
head = addback(head, 10); // Adding elements
head = addback(head, 20);
head = addback(head, 30);
// Free the list
freelist(head);
head = nullptr; // Set head to nullptr after freeing
cout << "List freed." << endl;
return 0;
}
6….
CODE…
#include <iostream>
using namespace std;
// Define the structure for a node in the linked list
struct node {
int data;
struct node* next;
};
// Function to add an element to the end of the linked
list
struct node* addback(struct node* head, int data) {
struct node* newNode = new node();
newNode->data = data;
newNode->next = nullptr;
if (head == nullptr) {
return newNode; // If the list is empty, return the
new node as the head
}
struct node* current = head;
while (current->next != nullptr) {
current = current->next; // Traverse to the end of
the list
}
current->next = newNode; // Add the new node at
the end
return head; // Return the unchanged head
}
// Function to display all elements of the linked list
void display(struct node* head) {
struct node* current = head;
while (current != nullptr) {
cout << current->data << " -> ";
current = current->next;
}
cout << "nullptr" << endl; // End of the list
}
// Function to find an element in the linked list
struct node* find(struct node* head, int data) {
struct node* current = head;
while (current != nullptr) {
if (current->data == data) {
return current; // Return the node if found
}
current = current->next;
}
return nullptr; // Return nullptr if not found
}
// Function to delete a specified node from the linked
list
struct node* delnode(struct node* head, struct node*
pelement) {
if (head == nullptr || pelement == nullptr) {
return head; // Nothing to delete
}
// If the node to be deleted is the head
if (head == pelement) {
struct node* temp = head;
head = head->next; // Move head to the next node
delete temp; // Free the old head
return head; // Return new head
}
// Find the node before the node to be deleted
struct node* current = head;
while (current->next != nullptr && current->next !=
pelement) {
current = current->next;
}
// If the node to be deleted was found
if (current->next == pelement) {
current->next = pelement->next; // Bypass the
node to delete it
delete pelement; // Free the memory
}
return head; // Return the head of the list
}
// Function to free the entire linked list
void freelist(struct node* head) {
struct node* current = head;
struct node* nextNode;
while (current != nullptr) {
nextNode = current->next; // Store the next node
delete current; // Free the current node
current = nextNode; // Move to the next node
}
}
// Main function to test the above functions
int main() {
struct node* head = nullptr; // Initialize an empty list
// Test adding elements to the linked list
cout << "Adding elements to the list..." << endl;
head = addback(head, 10);
head = addback(head, 20);
head = addback(head, 30);
// Display the current list
cout << "Current List: ";
display(head); // Expected output: "10 -> 20 -> 30 ->
nullptr"
// Test finding an element
cout << "Finding element 20..." << endl;
struct node* foundNode = find(head, 20);
if (foundNode) {
cout << "Node found: " << foundNode->data <<
endl; // Expected output: "Node found: 20"
} else {
cout << "Node not found." << endl;
}
// Test deleting an element
cout << "Deleting element 20..." << endl;
head = delnode(head, foundNode); // Delete the
found node
cout << "List after deletion: ";
display(head); // Expected output: "10 -> 30 ->
nullptr"
// Test freeing the list
cout << "Freeing the list..." << endl;
freelist(head);
head = nullptr; // Set head to nullptr after freeing
cout << "List freed." << endl;
return 0;
}