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

Lab Manual 03 DS

Data structure manual

Uploaded by

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

Lab Manual 03 DS

Data structure manual

Uploaded by

ahmedilyas5440
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1|Page

Lab Manual
Submitted by: Abdullah

Roll no: 004

Department: BS Software Engineering

Section: Blue

Subject: Data Structures

Submitted To: Mam Azka Mir


2|Page

-----( EXERCISE 3.1 )-----


Program:
#include <iostream>
using namespace std;

class Calculator{
public:

int sum(int n){


if(n==1){
return n;
}
else{
return n+sum(n-1);
}
}

int fac(int b){


if(b==1){
return b;
}
else{
return b*fac(b-1);
}
}

int Fibonacci(int fib){


3|Page

if(fib<=1){
return fib;
}
else{
return Fibonacci(fib-1)+Fibonacci(fib-2);
}
}

int Power(int base,int pow){


if(pow==0){
return 1;
}
else{
return base*Power(base,pow-1);
}
}

};

int main(int argc, char** argv) {


Calculator c1;
int ch;
while(true){
cout<<""<<endl;
cout<<"Menu"<<endl;
cout<<"1. Sum"<<endl;
cout<<"2. Factorial"<<endl;
cout<<"3. Number for find Fibonacci series"<<endl;
cout<<"4. Power of number"<<endl;
4|Page

cout<<"5. Exit"<<endl;
cout<<"Enter your choice"<<endl;
cin>>ch;
switch(ch){
case 1:
int ss;
cout<<"Enter Value to which you want to sum"<<endl;
cin>>ss;
int su;
su=c1.sum(ss);
cout<<"Sum is: "<<su<<endl;
break;

case 2:
int ff;
cout<<"Enter Value of which you want to find factorial"<<endl;
cin>>ff;
long long f;
f=c1.fac(ff);
cout<<"factorial is: "<<f<<endl;
break;

case 3:
cout<<"Enter the number of terms for fibonacci sequence"<<endl;
int terms;
cin>>terms;
if(terms<=0){
cout<<"Enter the positive number"<<endl;
}
5|Page

else{
for(int i=0;i<terms;i++){
cout<<c1.Fibonacci(i)<<" ";
}
}
break;

case 4:
int val,pow;
cout<<"Enter the number "<<endl;
cin>>val;
cout<<"Enter the power "<<endl;
cin>>pow;
int powe;
powe=c1.Power(val,pow);
cout<<"Power is "<<powe<<endl;
break;

case 5:
return 0;
break;
default:
cout<<"Invalid input....."<<endl;
}
}

return 0;
}
6|Page

Output:
7|Page

-----( EXERCISE 3.2 )-----

Program:
#include <iostream>
using namespace std;
struct Node{
string title;
Node *Next;
};

class SongPlaylist{
public:
Node *Head;
Node *Tail;
public:
SongPlaylist(){
Head=NULL;
Tail=NULL;
}

void insert(){
Node *temp= new Node;
cout<<"Enter the song name: ";
cin>>temp->title;
temp->Next=NULL;
if(Head==NULL){
Head=temp;
Tail=temp;
}
else{
Tail->Next=temp;
8|Page

Tail=temp;
}
}

void Forward_Display(Node *temp){


if(temp==NULL){

}
else{
cout<<temp->title<<" "<<endl;
Forward_Display(temp->Next);
}
}

void Reverse_Display(Node *temp){


if(temp==NULL){

}
else{
Reverse_Display(temp->Next);
cout<<temp->title<<" "<<endl;
}
}

};

int main(int argc, char** argv) {


SongPlaylist l1;
int ch;
while(true){
cout<<"Menu"<<endl;
cout<<"1.Insert song"<<endl;
cout<<"2.Forward_Display"<<endl;
cout<<"3.Reverse_Display"<<endl;
9|Page

cout<<"4.Exit"<<endl;
cout<<"Enter your choice"<<endl;
cin>>ch;
switch(ch){
case 1:
l1.insert();
break;
case 2:
l1.Forward_Display(l1.Head);
break;
case 3:
l1.Reverse_Display(l1.Head);
break;
case 4:
return 0 ;
break;
default:
cout<<"Invalid input..."<<endl;
}

return 0;
}
10 | P a g e

Output:

You might also like