You are on page 1of 8

COMSATS University Islamabad Sahiwal Campus

(Department of Computer Sciences)

Course Title: Data Structures And Algorithms Course Code: CSC211 Credit Hours: 4.0
Course Instructor: Ali Sher Kashif Programme Name: BS - CS
Semester: 3rd Batch: FA19 Section: C Date: 21-01-2022
Time Allowed: Maximum Marks: 50
Student’s Name: Reg. No. CUI/ /SWL
Important Instructions / Guidelines:
Read the question paper carefully and answer the questions according to their statements.
Mobile phones are not allowed. Calculators must not have any data/equations etc. in their memory.
Terminal Examinations FA21 – LAB

Question No. 1 [15]


Write a program which should implement a queue using a static array of size 10. Elements of
array used for stack are objects of “student” class. “student” class contains attributes
(privately defined) reg_no(int), st_name (string) and cgpa (float). “student” class should also
contain member functions (publicly defined); constructor, input and output functions. User
will insert objects of class “student” at rear of queue, values of attributes of objects will be
provided by user. When an object will be removed from front of queue, value of its attributes
should be displayed on screen.

#include<iostream>
using namespace std;
class student
{
private:
int front;
int rear;
int reg_no[10];
string st_name[10];
float cgpa[10];
public:
student();
eq();
deq();
disq();

};
student::student()
{
front=rear=-1;
}
student::eq()
{
front=0;
for(int i=0;i<2;i++)
{
rear++;
cout<<"Enter reg no"<<endl;
cin>>reg_no[rear];
cout<<"Enter name"<<endl;
cin>>st_name[rear];
cout<<"Enter cgpa"<<endl;
cin>>cgpa[rear];
cout<<" ";
}
}
student::deq()
{
cout<<"The deleted value is"<<endl;
cout<<"reg no = "<<reg_no[front]<<endl;
cout<<"Name = "<<st_name[front]<<endl;
cout<<"cgpa = "<<cgpa[front]<<endl;
cout<<"---------------------------------"<<endl;
front++;
}
student::disq()
{
cout<<"Queue is :"<<endl;
cout<<"-------------------------------"<<endl;
for(int i=front;i<=rear;i++)
{

cout<<"reg no is : "<<reg_no[i]<<endl;
cout<<"name is : "<<st_name[i]<<endl;
cout<<"cgpa is : "<<cgpa[i]<<endl;
cout<<"--------------------------------------------"<<endl;

}
}
int main()
{
student s;
s.eq();
s.disq();
s.deq();
s.disq();

Question No. 2 [20]


Write a program to implement two binary trees using arrays. User is required to insert
elements in each of these trees. Both the trees contain same number of elements. Your
program should use the in order traversal method on both the trees to identify that whether
two trees are equivalent or not.

Note:
Two trees are identical when they have same data and arrangement of data is also same. To identify if two trees
are identical, we need to traverse both trees simultaneously, and while traversing we need to compare data and
children of the trees.

#include <iostream>
using namespace std;
struct Node
{
int data;
Node* left, * right;
};
Node* newNode(int data)
{
Node* n = new Node;
n->data = data;
n->left = n->right = NULL;
return (n);
}
Node* insertLevelOrder(int arr[], Node* root,int i, int n)
{
if (i < n)
{
Node* temp = newNode(arr[i]);
root = temp;
root->left = insertLevelOrder(arr,root->left, 2 * i + 1, n);
root->right = insertLevelOrder(arr,root->right, 2 * i + 2, n);
}

return root;
}
Node* insertLevelOrder1(int arr1[], Node* root1,int i, int c)
{
if (i < c)
{
Node* temp=newNode(arr1[i]);
root1=temp;
root1->left = insertLevelOrder1(arr1,root1->left, 2 * i + 1, c);
root1->right = insertLevelOrder1(arr1,root1->right, 2 * i + 2, c);

}
return root1;
}

void inOrder(Node* root)


{
if (root != NULL)
{
inOrder(root->left);
cout << root->data <<" ";
inOrder(root->right);
}
}
void inOrder1(Node* root1)
{
if (root1 != NULL)
{
inOrder(root1->left);
cout << root1->data <<" ";
inOrder(root1->right);
}
}
int main()
{
cout<<"enter array no 1 :"<<endl;
int arr[9];
for (int i=0;i<9;i++)
{
cin>>arr[i];
}
cout<<"enter array no 2 : "<<endl;
int arr1[9];
for (int j=0;j<9;j++)
{
cin>>arr1[j];
}
int n = sizeof(arr)/sizeof(arr[0]);
Node* root = insertLevelOrder(arr, root, 0, n);
int c = sizeof(arr1)/sizeof(arr1[0]);
Node* root1 = insertLevelOrder1(arr1, root1, 0, c); inOrder(root);
cout<<"\n\n "<<endl; inOrder1(root1); cout<<"\n\n "<<endl;

if ( arr[0]==arr1[0] && arr[1]==arr1[1] && arr[2]==arr1[2] && arr[3]==arr1[3] &&


arr[4]==arr1[4] && arr[5]==arr1[5] && arr[6]==arr1[6])
{
cout<<"Both are Equivalent"<<endl;
}
else
{
cout<<"Not Equivalent"<<endl;
}

}
Question No. 3 [15]
Write a program which should implement a linear linked list. Elements of this linked list
should be of integer type, user will provide values as input for elements of this linked list.
Your program should allow searching of a value specified by user in linked list and it will
also allow deletion of a value specified by user.

#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
Node(int val)
{
data=val;
next=NULL;
}
};
void insertatfront(Node *&head,int val)
{
Node *n= new Node(val);
Node* ptr=n;
ptr->next=head;
head=ptr;
while(ptr!=NULL)
{
ptr=ptr->next;
}
}
void insertatend(Node *&head,int val)
{
Node *n=new Node(val);
Node *ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=n;
}
void delnode(Node**head,int pos)
{
Node* current=*head;
Node* previous=*head;
if(*head==NULL)
{
cout<<"The list is empty"<<endl;
}
else if(pos==1)
{
*head=current->next;
delete(current);
}
else
{
while(pos!=1)
{
previous=current;
current=current->next;
pos--;
}
previous->next=current->next;
delete(current);
}
}
void display(Node* head)
{
Node *ptr=head;
while(ptr!=NULL)
{
cout<<ptr->data;
ptr=ptr->next;
}
}
void search(Node *&head,int val)
{
Node *ptr=head;
while(ptr->next!=NULL)
{
if(val==ptr->data)
{
cout<<"Value is found"<<endl;
ptr=ptr->next;
break;
}
else
{
cout<<"Value is not found"<<endl;
break;
}

}
}
int main()
{
Node*head=NULL;
int val,ch;
while(true)
{
cout<<"Enter 1 to insert at front"<<endl;
cout<<"Enter 2 to insert at end"<<endl;
cout<<"Enter 3 to delete node at any position"<<endl;
cout<<"Enter 4 to search"<<endl;
cout<<"Enter 5 to display"<<endl;
cout<<"Enter 6 to EXIT"<<endl;
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"Enter the value to insert"<<endl;
cin>>val;
insertatfront(head,val);
break;
}
case 2:
{
cout<<"Enter the value to insert"<<endl;
cin>>val;
insertatend(head,val);
break;
}
case 3:
{
int pos;
cout<<"Enter the position of node to delete"<<endl;
cin>>pos;
delnode(&head,pos);
break;
}
case 4:
{
cout<<"Enter the value to search"<<endl;
cin>>val;
search(head,val);
break;
}
case 5:
{
cout<<"The list is"<<endl;
display(head);
cout<<endl;
break;
}
case 6:
{
return 0;
}
default:
{
break;
}
}

Best Wishes for Exams! 

You might also like