Professional Documents
Culture Documents
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
#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();
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;
}
}
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;
}
}