0% found this document useful (0 votes)
48 views

Lab 7

The document contains 3 questions asking to write programs using data structures and algorithms. It includes code to perform operations on a doubly linked list such as insertion and deletion at the first position. It also includes code using recursion to calculate factorial, power, Fibonacci series, and print a given series. The output of running the code is included for each question.

Uploaded by

Umar Ali
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)
48 views

Lab 7

The document contains 3 questions asking to write programs using data structures and algorithms. It includes code to perform operations on a doubly linked list such as insertion and deletion at the first position. It also includes code using recursion to calculate factorial, power, Fibonacci series, and print a given series. The output of running the code is included for each question.

Uploaded by

Umar Ali
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/ 8

National University of Modern Languages

Subject: Data Structure and Algorithms

Lab Report#7

Submitted to: Aqib Adeel

Submitted by: Umar Ali

Class: BSSE-3A(Morning)

Roll No:11982

Date: October 18,2019


Q1. Write a program which perform Insertion and Deletion at first position in Doubly Linked
List.

CODE:

#include<iostream>

using namespace std;

struct node{ //we take a structure to create our node.

int nmbr;

node* next;

node* prv;

};

node* head; //we take two pointers of node type to point the nodes of list.

node* tail;

void Insert(int x){//this function store data and address in the nodes.

node* temp=new node();//we take a temporary pointer of node type and store address of
the node created.

temp->nmbr=x;

temp->next=NULL;

temp->prv=NULL;

if(head==NULL){ //if head is null then this if statement is executed.

tail=temp;//we point both head and tail pointers to the first node.

head=temp;

else{ //if any address is stored in head than this statement is executed.

tail->prv=temp; //here the address of the node is stored in the previous block of
previous node.

temp->next=tail;
tail=temp;

void Delete(){// this function deletes the required node.

tail=tail->next; //the address in next block of last node is stored in tail.

tail->prv=NULL; //the previous of first node is make NULL.

void show(char ch){ //this functions prints our list on the screen.

node* temp1;

temp1=tail; //we store the address stored in tail to temp1.

if(ch=='f'){ //if user want to print in forward direction than this block is executed.

while(temp1!=head){ //this loop is executed until temp1 is not equal head.

temp1=temp1->next;//we use this loop to move temp1 to first node.

while(temp1!=NULL){//this loop is executed until temp1 is not equal to null.

cout<<temp1->nmbr<<" ";

temp1=temp1->prv;// temp1 prints all the nodes of the list.

} }

else{

while(temp1!=NULL){

cout<<temp1->nmbr<<" ";

temp1=temp1->next;

} }

int main(){
head=NULL;

tail=NULL;

char c;

Insert(2);

Insert(7);

Insert(0);

Insert(4);

cout<<"\nEnter the way you want your list to be shown....Note:f for forward and r for
reverse:",cin>>c;

cout<<"Inserted list is:\n";

show(c);

Delete();

cout<<"\nAfter deletion:\n";

show(c);

OUTPUT:
Q2. Write a program which perform factorial, power and Fibonacci series using Recurrsion.

CODE:

#include<iostream>

using namespace std;

int factorial(int x){ //this function prints the factorial of the number user entered.

if(x>=1){ //this blocke is executed until the value of x is not equal to 0.

return x*factorial(x-1); //the function is called again and the value of x is decremented.

else{ //when x is equal to 0 this statement closes the function.

return 1;

}}

int power(int n, int pow ,int p){ //this function calculates the power of a number user entered.

if(p==1) //if p is equals to 1 this statement returns the value of power.

return pow;

else{ //if p is greater than 1 then this statements executes.

pow=pow*n; //this statement multiply the power to n;

power(n,pow,p-1); //the function is called again.

} }

int fibonacci(int x){ //this functions prints the fabonacci series.

if(x==0 || x==1){ //if x is equal to 0 or 1 then this statement executes and returns the
value of x;

return x;

}
else{

return fibonacci(x-1)+fibonacci(x-2); //the function is called again.

}}

int main(){

int nmbr;

int p;

cout<<"Enter a number:",cin>>nmbr;

cout<<"Factorial of "<<nmbr<<" is:"<<factorial(nmbr);

cout<<"\n\n";

cout<<"\nEnter a number:",cin>>nmbr;

cout<<"Enter power:",cin>>p;

int i=nmbr;

cout<<p<<" power "<<nmbr<<" is:"<<power(nmbr,i,p);

cout<<"\n\n";

cout<<"\nEnter a number:",cin>>nmbr;

cout<<"Fibonacci of "<<nmbr<<" is:"<<fibonacci(nmbr);

OUTPUT:
Q3. Write a program to print the given series using recursion.

CODE:

#include<iostream>

using namespace std;

static int a=-10; //we create two static global variables so that there values can’t be changed.

static int b=1;

int series(int i){ //this functions prints the required series using recurrsion

if(i==10){ //if i is equals to 10 then tis if condition executes.

if(a==0){ //when a equals to zero this function is closed.

return 0;

else{

cout<<-a<<" "; the value of a is prints on the screen.

a=a+b; a is assigned new value.

series(i); //the function is called again.

if(i>0 && i<10){ //when this condition is true, it prints number from 1 to 9.

cout<<i<<" ";

series(i+1); //the function is called again and we increment thee value of i in the
parameters.

int main(){
series(1);

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy