0% found this document useful (0 votes)
3 views10 pages

Data Structures Final Lab

The document contains code for a final lab exam on data structures submitted by Abdullah Atif. It includes implementations for linked lists and binary trees, featuring functions for inserting nodes, searching, displaying data, and traversing trees in different orders. The document also includes a calculation of the sum of all nodes in a binary tree.

Uploaded by

abdullaatif89
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Data Structures Final Lab

The document contains code for a final lab exam on data structures submitted by Abdullah Atif. It includes implementations for linked lists and binary trees, featuring functions for inserting nodes, searching, displaying data, and traversing trees in different orders. The document also includes a calculation of the sum of all nodes in a binary tree.

Uploaded by

abdullaatif89
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DATA STRUCTURES

Final Lab Exam

Submitted By : Abdullah Atif

(NUML-S25-16574)

BSSE- 3(A)

Submitted To : Dr Maryam Imtiaz Malik

DATE: 19 MAY, 2026

NATIONAL UNIVERSITY OF MODERN LANGUAGES ISLAMABAD

QUESTION NO. 1
CODE
#include <iostream>

using namespace std;

struct student {

int roll;

string name;

student* next;

};

student* head1 = NULL;

student* head2 = NULL;

student* head3 = NULL;

void insert(student*& head, int roll, string name) {

student* newnode = new student();

newnode->roll = roll;

newnode->name = name;

newnode->next = NULL;

if (head == NULL) {

head = newnode;

else {

student* temp = head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newnode;

bool search(student* head, int roll) {


student* temp = head;

while (temp != NULL) {

if (temp->roll == roll) {

return true;

temp = temp->next;

return false;

void display(student* head) {

student* temp = head;

if (temp == NULL) {

cout << "List is empty." << endl;

return;

while (temp != NULL) {

cout << "Roll No: " << temp->roll << " Name: " << temp->name << endl;

temp = temp->next;

void mergelist() {

student* temp = head1;

while (temp != NULL) {

insert(head3, temp->roll, temp->name);

temp = temp->next;
}

temp = head2;

while (temp != NULL) {

if (search(head3, temp->roll) == false) {

insert(head3, temp->roll, temp->name);

temp = temp->next;

int main() {

int n, roll;

string name;

cout << "Enter number of students in List A: ";

cin >> n;

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

cout << "Enter roll number: ";

cin >> roll;

cout << "Enter name: ";

cin >> name;


insert(head1, roll, name);

cout << endl;

cout << "Enter number of students in List B: ";

cin >> n;

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

cout << "Enter roll number: ";

cin >> roll;

cout << "Enter name: ";

cin >> name;

insert(head2, roll, name);

mergelist();

cout << endl;

cout << "Merged Data is: " << endl;

display(head3);

return 0;

}
OUTPUT :
QUESTION NO. 2
CODE
#include<iostream>
using namespace std;

struct treeNode
{
int info;
treeNode* left;
treeNode* right;
};
treeNode* insert(treeNode* root, int item)
{
if(root == NULL)
{
treeNode* newNode = new treeNode;
newNode->info = item;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}
if(item < root->info)
root->left = insert(root->left, item);
else if(item > root->info)
root->right = insert(root->right, item);
return root;
}
void preorder(treeNode* root)
{
if(root == NULL)
return;

cout << root->info << endl;


preorder(root->left);
preorder(root->right);
}
void inorder(treeNode* root)
{
if(root == NULL)
return;
inorder(root->left);
cout << root->info << endl;
inorder(root->right);
}
void postorder(treeNode* root)
{
if(root == NULL)
return;
postorder(root->left);
postorder(root->right);
cout << root->info << endl;
}
int calculatesum(treeNode* root)
{
if(root == NULL)
return 0;
int leftsum = calculatesum(root->left);
int rightsum = calculatesum(root->right);
return root->info + leftsum + rightsum;
}

int main()
{
treeNode* root = NULL;

root = insert(root, 57);


root = insert(root, 94);
root = insert(root, 60);
root = insert(root, 38);
root = insert(root, 72);

cout << "Preorder :" << endl;


preorder(root);

cout << "Inorder :" << endl;


inorder(root);

cout << "Postorder :" << endl;


postorder(root);
cout << "Sum of all nodes is: ";
cout << calculatesum(root);
return 0;
}

OUTPUT

You might also like