You are on page 1of 40

Riphah International University

LAB MANUAL
Submitted to: Sir. Muhammad USMAN & Sir ASAD
Submitted by: Hanzla
Subject: Data Structures & Algorithms

Class: BSCS 3rd (B)


Registration No.: 13304
Date: 28-JANUARY-2021
Data Structures and Algorithms Lab
Riphah International University Faisalabad Campus
Riphah College of Computing
Fall 2020

LAB MANUAL
Experiment No. 1: Your task is to implement the concepts of inheritance and
Polymorphism. The code must be in Visual C++.

#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape( int a = 0, int b = 0){
width = a;
height = b;
}
virtual int area() {
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public Shape {
public:
Rectangle( int a = 0, int b = 0):Shape(a, b) { }
int area () {
cout << "Rectangle class area :";
cout<< (width * height)<<endl;
}
};
class Triangle: public Shape {
public:
Triangle( int a = 0, int b = 0):Shape(a, b) { }

int area () {
cout << "Triangle class area :";
cout<< (width * height / 2);
}
};
int main() {
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);
shape = &rec;
shape->area();
shape = &tri;
shape->area();
}
Experiment No. 2: A phone number consists of three parts. The area code, the
exchange and the number. Write a program that uses an array to store 3 parts of the
number separately. Call the structure Phone. Create two array variables of type
Phone. Initialize one, and have the user to input a number for the other one. Then
display both numbers. The interchange might look like this Enter your area, code
and exchange number 92-333-1234567. Also you need to search the phone
number.
Description:
You need to Save the Phone number in the Array Variable in the format given below Enter
Phone Number: 92-348-1235675
The program should be able to enter the number and the later show the number on the
console as well.

#include<iostream>
using namespace std;
int area_code[2];
int exchange_number[10];
void input(){
cout<<"Enter Your Area Code (Use Space After Each digit): ";
for(int i = 0; i<2 ; i++){
cin>>area_code[i];
}

cout<<"Enter Your Exchange Number (Use Space After Each digit): ";
for(int j = 0; j<10 ; j++){
cin>>exchange_number[j];
}
}
void output(){
for(int i = 0; i<2 ; i++){
cout<<area_code[i];
}
cout<<"-";
for(int j = 0 ; j<10 ; j++){
cout<<exchange_number[j];
if(j == 2){
cout<<"-";
}
}
}
int main(){
int number;
input();
cout<<"Your Number Is : ";
output();
cout<<"\nSearch Any Digit In Your Phone Number : ";
cin>>number;
for(int k = 0 ; k<2 ; k++){
if(number == area_code[k]){

cout<<"Your Number is Available at Index "<<k<<" in AREA CODE ARRAY


"<<endl;
}
}
for(int l = 0 ; l<10 ; l++){
if(number == exchange_number[l]){
cout<<"Your Number is Available at Index "<<l<<" in EXCHANGE NUMBER
ARRAY "<<endl;
}
}

}
Experiment No. 3(A): Implement the concept of Linear Search using the array
given below
Array: RiphahFSD.

#include<iostream>
using namespace std;
void linear_search(){
char a[9] = {'r' , 'i' , 'p' , 'h' ,'a' ,'h' ,'f' ,'s' ,'d'};
char key;
int s=0;
cout<<"ENTER A Character U WANT TO FIND IN ARRAY : ";
cin>>key;
for(int i=0 ; i<9 ; i++){
if(key == a[i]){
cout<<"YOUR Character IS AVAILABLE AT INDEX "<<" "<<i<<"
THAT IS "<<" "<<key;
s = 1;
}
}
if(s == 0){
cout<<"YOUR Charachter IS NOT PRESENT IN ARRAY ";
}
}

int main(){
linear_search();}
Experiment No. 3(B): Implement the concept of BINARY Search using the User
Input Array:

#include<iostream>
using namespace std;
int temp;
int arr[10];
int binarysearch(int arr[] ,int left , int right , int key){
if(right >=left){
int mid = (left +right) / 2;
if(key == arr[mid]){
return mid;
}
if(arr[mid] > key){
return binarysearch( arr , left , mid-1 , key);
}
else{
return binarysearch( arr , mid+1 , right , key);
}
}
return -1;
}
void bubblesort(){
for(int k=0; k<10 ; k++){
for(int i=0;i<10-1;i++){
if(arr[i] > arr[i+1]){
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}
}
int main(){
int i;
int key;
for( i=0;i<10;i++){
cout<<"ENTER VALUE AT INDEX : "<<i<<" ";
cin>>arr[i];
}

bubblesort();
cout<<"-----------------SORTED AARAY ---------------------"<<endl;
for(i=0 ; i<10 ; i++){
cout<<"VALUE AT INDEX "<<i<<" is "<<arr[i]<<endl;
}
cout<<"ENTER A NUMBER TO FIND :";
cin>>key;
int size = sizeof(arr) / sizeof(arr[0]);
int result = binarysearch( arr , 0 , size-1 , key);
(result==-1)?cout<<"YOUR NUMBER IS NOT PRESENT IN ARRAY ":cout<<"YOUR
NUMBER IS PRESENT AT INDEX : "<<result;
}
Experiment No. 4: Write a Program to Implement the Concept of Simple Sorting
(Ascending and Descending) on user Entered Array.
SIMPLE SORTING ALGORITHM IS BUBBLE SORT
Description:
Make the Program Menu Based
You need to implement both Techniques in single Program
 If User Press 1 then Sort the Array in Ascending Order
 If User Press 2 then Sort the Array using Descending Order

#include<iostream>
using namespace std;
int arr[10];
void bubblesort_asc(){
int temp;
for(int k=0; k<10 ; k++){
for(int i=0;i<10-1;i++){
if(arr[i] > arr[i+1]){
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}
}

void bubblesort_dsc(){
int temp;
for(int k=0; k<10 ; k++){
for(int i=0;i<10-1;i++){
if(arr[i] < arr[i+1]){
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}
}
int main(){
int choice;
for(int i=0 ; i<10;i++){
cout<<"Enter Elements of array At index: "<<i<<" ";
cin>>arr[i];
}
cout<<"For Sorting in Ascending Order : PRESS 1 "<<endl;
cout<<"For Sorting in Descending Order : PRESS 2"<<endl;
cin>>choice;
switch(choice){
case 1:
bubblesort_asc();
cout<<"-------------------SORTED ARRAY IN Ascending Order-----------------------
"<<endl;
for(int i=0 ; i<10;i++){
cout<<"Elements of array At index "<<i<<" is : "<<arr[i]<<endl;}
break;
case 2:
bubblesort_dsc();
cout<<"-------------------SORTED ARRAY IN Descending Order-----------------------
"<<endl;
for(int i=0 ; i<10;i++){
cout<<"Elements of array At index "<<i<<" is : "<<arr[i]<<endl; }
break;
}}

OUTPUT
Experiment No. 5: Implement the Concept of Bubble Sort and Selection Sort
algorithms on the given array for sorting.
The Array is:
-1 3 2 -10 11 3 2 1 0 -16

Description:
Make the Program Menu Based and Implement the Concepts
If User Press 1 then Sort the Array in Bubble Sort
After that gain Program ask in which technique ascending or descending
If User Press A then sort the array In Ascending Order
If User Press D then sort the Array in Descending Order
If User Press 2 then sort this Array in Selection sort
After that gain Program ask in which technique ascending or descending
If User Press A then sort the array In Ascending Order
If User Press D then sort the Array in Descending Order.

#include<iostream>
using namespace std;
int arr[10] = {-1 , 3 , 2 , -10 , 11 , 3 , 2 , 1 , 0 , -16};
void bubblesort_asc(){
int temp;
for(int k=0; k<10 ; k++){
for(int i=0;i<10-1;i++){
if(arr[i] > arr[i+1]){
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}
}
void bubblesort_dsc(){
int temp;
for(int k=0; k<10 ; k++){
for(int i=0;i<10-1;i++){
if(arr[i] < arr[i+1]){
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}
}
//----------------SELECTION SORT START---------------------

void selection_asc(){
int min , temp;
for(int i=0;i<10-1;i++){
min=i;
for(int j=i+1 ; j<10 ; j++){
if(arr[j] < arr[min]){
min = j;
}
}
if(min!=i){
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
for(int i=0;i<10;i++){
cout<<"value at "<<i<<" is "<<arr[i]<<endl;
}
}
void selection_dsc(){
int min , temp;
for(int i=0;i<10-1;i++){
min=i;
for(int j=i+1 ; j<10 ; j++){
if(arr[j] > arr[min]){
min = j;
}
}
if(min!=i){
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
for(int i=0;i<10;i++){
cout<<"value at "<<i<<" is "<<arr[i]<<endl;
}
}
int main(){
int choice;
char choice2;
cout<<"FOR BUUBLE SORTING : PRESS 1: "<<endl;
cout<<"FOR SELECTION SORTING : PRESS 2: "<<endl;
cin>>choice;
switch(choice){
case 1:
cout<<"-----------BUBBLE SORTING ALGORITHM START--------------
"<<endl;
cout<<"For Sorting in Ascending Order : PRESS a "<<endl;
cout<<"For Sorting in Descending Order : PRESS d"<<endl;
cin>>choice2;
switch(choice2){
case 'a':
bubblesort_asc();
cout<<"-------------------SORTED ARRAY IN Ascending Order-----------------------
"<<endl;
for(int i=0 ; i<10;i++){
cout<<"Elements of array At index "<<i<<" is : "<<arr[i]<<endl;}
break;
case 'd':
bubblesort_dsc();
cout<<"-------------------SORTED ARRAY IN Descending Order-----------------------
"<<endl;
for(int i=0 ; i<10;i++){
cout<<"Elements of array At index "<<i<<" is : "<<arr[i]<<endl; }
break;
}
break;
case 2:
cout<<"-----------SELECTION SORTING ALGORITHM START--------------"<<endl;
cout<<"For Sorting in Ascending Order : PRESS a "<<endl;
cout<<"For Sorting in Descending Order : PRESS d"<<endl;
cin>>choice2;
switch(choice2){
case 'a':
selection_asc();
break;
case 'd':
selection_dsc();
break;
}
}}
Experiment 6: Implement the Concept of Insertion Sort Algorithm using array
given below
Array:
D A T A STRUCTURES
Description:
Make the Program Menu Based and Implement the Concepts
First Program asks from the User in which technique you want to Sort
If user Press A then sort the Array in ascending Order
If User Press D then Sort the Array in descending order

#include<iostream>
using namespace std;
char arr[14] = {'d','a','t','a','s','t','r','u','c','t','u','r','e','s'};
void insertion_sort_asc(){
int j , key;
for(int i=1 ; i<14 ; i++){
key = arr[i];
j = i-1;
while(j>=0 && arr[j] > key){
arr[j+1] = arr[j];
j= j-1;
}
arr[j+1] = key;
}
for(int i=0;i<14;i++){
cout<<"value at "<<i<<" is "<<arr[i]<<endl;
}
}
void insertion_sort_dsc(){
int j , key;
for(int i=1 ; i<14 ; i++){
key = arr[i];
j = i-1;
while(j>=0 && arr[j] < key){
arr[j+1] = arr[j];
j= j-1;
}
arr[j+1] = key;
}
for(int i=0;i<14;i++){
cout<<"value at "<<i<<" is "<<arr[i]<<endl;
}
}
int main(){
char choice;
cout<<"For Sorting in Ascending Order : PRESS a "<<endl;
cout<<"For Sorting in Descending Order : PRESS d"<<endl;
cin>>choice;
switch(choice){
case 'a':

cout<<"-------------------SORTED ARRAY IN Ascending Order-----------------------


"<<endl;
insertion_sort_asc();
break;
case 'd':
cout<<"-------------------SORTED ARRAY IN Descending Order-----------------------
"<<endl;
insertion_sort_dsc();
break;
}
}

OUTPUT:
Experiment No. 7: Queue is a data structure that can be used to store data which
can later be retrieved in the first in first out (FIFO) order. Queue is an ordered-list in
which all the insertions and deletions are made at two different ends to maintain the
FIFO order. You have to implement these Functions by using an array.
Enqueue () Store onto a Queue
Dequeue () retrieve (delete) from Queue
Is_empty () check if the Queue is empty
Is_Full () check if the Queue is full

As you know Queue is based on the Concept of First in First out Technique So Now you need to
implement the Queue on the Given Criteria.

R I P H A H F S D C A M P U S.
Now Call the Functions Dequeue three times and after that traverse the Queue
Now again enter the deleted Items using Enqueue Function and show the entire Queue.

#include <iostream>
using namespace std;
int main()
{
int size=15;
int num;
int flag=1;
int front = -1;
int rear = 15;
char array[size]={'r','i','p','h','a','h','f','s','d','c','a','m','p','u','s'};
cout<<"Press 1 for enqueue \n Press 2 for dequeue \n Press 3 for display\n Press 4 to exit
\n";
do{
cout<<"ENTER CHOICE ";
cin>>num;
if(num==1)
{
if(rear==size)
{
cout<<"Queue is full "<<endl; }
else
{ front=0;
rear++;
cout<<"Enter "<<rear<<" element \n";
cin>>array[rear];
}
}
if(num==2)
{
if(front > rear)
cout<<"\nQueue is Empty!!";
else{
for(int j=0;j<=rear;j++)
{
array[j-1]=array[j];
}
cout<<"\nDequeued Element is "<<array[front]<<endl;
rear--;
}
}
if(num==3)
{
for(int i=0; i<rear; i++)
{
cout<<"Elements of Queue at index "<<i<<" is "<<array[i]<<endl;
}
}
if(num==4)
{
flag=0;
}
}
while(flag==1);
}
Experiment No. 8: You have to implement a stack by using array with following
function
Push()
Pop()
Isfull()
Isempty()
Your output should be like this: This is a Stack program by using array!
Press 1 to push an element
Press 2 to pop an element
Press 3 to display stack
Press 4 to exit

Description:
As you know the Stack is work on the basis of Last in First out LIFO so you need to implement
the stack operations on the scenario given below

COM P U T E R S C I E N C E
Call POP Functions three times and show the stack
And after that call the PUSH Function three time and also show the stack complete.

#include<iostream>
using namespace std;
int top=14;
char value;
char stack[15]={'c','o','m','p','u','t','e','r','s','c','i','e','n','c','e'};
void push() {
if(top>=15-1)
cout<<"Stack Overflow!!Cannot Push an Element :("<<endl;
else {
cout<<"Enter value to be pushed:"<<endl;
cin>>value;
top++;
stack[top]=value;
}
}
void pop() {
if(top<=-1)
cout<<"Stack is Underflow!!Cannot Pop an Element :("<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
for(int i=top; i>=0; i--)
cout<<"Stack elements at index: "<<i<<" is "<<stack[i]<<endl;

} else
cout<<"EMPTY STACK !! Nothing to Display :("<<endl;
}
int main() {
int ch;
cout<<"Enter 1 to Push in stack"<<endl;
cout<<"Enter 2 to Pop from stack"<<endl;
cout<<"Enter 3 to Display stack"<<endl;
cout<<"Enter 4 to Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {

push();
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
OUTPUT
Experiment No. 10: A linked list is a data structure consisting of a group of Nodes which
together represent a sequence. Under the simplest form, each node is composed of a data
and a reference (in other words, a link) to the next node in the sequence; more complex
variants add additional links. This structure allows for efficient insertion or removal of
elements from any position in the sequence.
Singly linked lists contain nodes which have a data field as well as a next field, which
points to the next node in line of nodes. Operations that can be performed on singly linked
lists include insertion, deletion and traversal. You have to implement these operations in
singly linked list.
Insertion ()
Deletion ()
Description:
As you know the complete concept of the Linked List now implement the Functions on the given
scenario

ABC FSD 123 FGH LHR 125 FJK KHR 657


NULL NI NULL

#include<iostream>
using namespace std;
//Node Declaration
struct node
{
int data;
struct node *address;
}*start;

//Class Declaration
class singlelist
{
public:
node* create_node(int);
void insert_begin();
void insert_pos();
void insert_last();
void delete_pos();
void delete_beg();
void delete_end();
void update();
void display();
singlelist()
{
start = NULL;
}
};
//Main :contains menu
int main()
{
int choice, nodes, element, position, i;
singlelist sl;
start = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Operations on singly linked list"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert Node at beginning"<<endl;
cout<<"2.Insert node at last"<<endl;
cout<<"3.Insert node at Given Position"<<endl;
cout<<"4.Display Linked List"<<endl;
cout<<"5.Delete at beginning"<<endl;
cout<<"6.Delete at Last"<<endl;
cout<<"7.Update a Given Node "<<endl;
cout<<"8.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
sl.insert_begin();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at Last: "<<endl;
sl.insert_last();
cout<<endl;
break;
case 3:
cout<<"Inserting Node at a given position:"<<endl;
sl.insert_pos();
cout<<endl;
break;
case 4:
cout<<"Display elements of link list"<<endl;
sl.display();
cout<<endl;
break;
case 5:
cout<<"Deleting Node at Beginning: "<<endl;
sl.delete_beg();
cout<<endl;
break;
case 6:
cout<<"Deleting Node at Last: "<<endl;
sl.delete_end();
cout<<endl;
break;
case 7:
cout<<"Updating Node At Given Position: "<<endl;
sl.update();
break;
case 8:
cout<<"Exiting..."<<endl;
exit(1);
break;

default:
cout<<"Wrong choice"<<endl;
}
}
}

//Creating Node
node *singlelist::create_node(int value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->data = value;
temp->address = NULL;
return temp;
}
}

//Inserting element in beginning


void singlelist::insert_begin()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->address = NULL;
}
else
{
p = start;
start = temp;
start->address = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
//delete Node at beginning
void singlelist::delete_beg(){
if(start == NULL){
cout<<"LIST IS EMPTY "<<endl;
}
else{
node *ptr=start;
start = start -> address;
delete(ptr);

}
}
//Delete Node at last
void singlelist::delete_end(){
node *p , *prev;
node *temp = start;
if(start == NULL){
cout<<"list is empty "<<endl;
}
while(temp -> address!=NULL){
prev = temp;
temp = temp -> address;
}
prev->address = NULL;
delete(temp);
}
//Inserting Node at last
void singlelist::insert_last()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *f;
temp = create_node(value);
f = start;
while (f->address != NULL)
{
f = f->address;
}
temp->address = NULL;
f->address = temp;
cout<<"Element Inserted at last"<<endl;
}
//Insertion of node at a given position
void singlelist::insert_pos()
{
int value, pos, counter = 0;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *f, *ptr;
temp = create_node(value);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
int i;
f = start;
while (f != NULL)
{
f = f->address;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->address = NULL;
}
else
{
ptr = start;
start = temp;
start->address = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
f = start;
for (i = 1; i < pos; i++)
{
ptr = f;
f = f->address;
}
ptr->address = temp;
temp->address = f;
}
else
{
cout<<"Positon out of range"<<endl;
}
}
//Update a given Node
void singlelist::update()
{
int value, pos, i;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the node postion to be updated: ";
cin>>pos;
cout<<"Enter the new value: ";
cin>>value;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start->data = value;
}
else
{
for (i = 0;i < pos - 1;i++)
{
if (s == NULL)
{
cout<<"There are less than "<<pos<<" elements";
return;
}
s = s->address;
}
s->data = value;
}
cout<<"Node Updated"<<endl;
}
//Display Elements of a link list
void singlelist::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->data<<"->";
temp = temp->address;
}
cout<<"NULL"<<endl;
}

INSERTION
DELETION

UPDATING

You might also like