You are on page 1of 7

Assignment :- Data Structure

1)Length of Linklist
2)Print Linklist
3)Reverse print Linklist
4)Largest Element Linklist
5)Tower of Hanoi
6)Recursive solution for sequential search
7)Recursive solution for
A1=6 A(n)=2*A(n-1)+4

8)Recursive solution for decimal to binary conversion.


Name : Ammar Abdullah
Roll no# 3733
BSSE 4th (b)
Date :9/8/18
Submit to : Mudasar Alam
Length of linklist

Singly:
int len(node * temp ,int length)
{
If(temp->next !=NULL)
{
len(temp->next,length+1);
}
Return length;
}
Doubly:
int len(node * temp ,int length)
{
If(temp->next !=NULL)
{
len(temp->next,length+1);
}
Return length;
}
Circular:
Int len(node * temp ,int length)
{
If(temp->next !=head)
{
len(temp->next,length+1);
}
Return length;
}

Print Linklist

Singly:
Void display(node *temp)
{
If(temp->next!=NULL)
{
Cout<<temp->data;
display(temp->next);
}

Doubly:
Void display(node *temp)
{
If(temp->next!=NULL)
{
Cout<<temp->data;
display(temp->next);
}

Circular:
Void display(node *temp)
{
If(temp->next!=head)
{
Cout<<temp->data;
display(temp->next);
}
}

Reverse Print

Singly:

Void r_prnt(node *temp)

If(temp->next !=NULL)

R_prnt(temp->next);

Cout<<temp->data;

Doubly:

Void r_prnt(node *temp)

If(temp->prev !=head)

Cout<<temp->data;

R_prnt(temp->prev);

Circular:
Void r_prnt(node *temp)

If(temp->tail !=head)

Cout<<temp->data;

R_prnt(temp->prev);}

Largest Element in Linklist

void max_el(node *temp ,int max)

If(temp->data > max)

Max=temp->data;

If(temp->next != NULL)

max_el(temp->next,max);

Tower of Hanoi

Int moves=0;

Void Honoi( int m, char a, char b, char c)

Moves++;

If(m==1){cout<<”Move disk”<<m<<”from”<<a<<”to”<<c;}

Else {

Hanoi(m-1,a,c,b);
cout<<”Move disk”<<m<<”from”<<a<<”to”<<c;

Hanoi(m-1,b,a,c);

Recursive Sequential Search

Int search(int arr[],int index,int max,int data)

If(index!=max)

If(arr[index]==data)

Return data;

Else

Search(arr,index+1,max,data);

Recursive solution for A1=6 A(n)=2*A(n-1)+4

Base case : a[0]=1

Prototype

int fun (int n)

{fun (n)=2* fun(n-1) +4;if(n=0)return 1;}


Recursive solution for decimal to binary conversion.

int convert(int val,int arr[],int index)

{
If(x=0) arr[index]=0;

Else If(x=1) arr[index]=1;

Else{ arr[index]=val%2; index++;convert(val/2)};

Return arr;

You might also like