Data structure lab file

 

Data structure lab

Practical file

1Aim to finding students grade

Algorithm  step1 Start

Step2 enter m1,m2,m3

Step3 avg= ( m1+m2+m3)/3

Step4 if(avg>=75)

{cout<<"A+ with distinction";}

else if(avg>=60 && avg<75)

{cout<<"grade is A";}

else if(avg>=45 && avg<60)

{cout<<"grade is B";}

else if(avg>=40 && avg<45)

{cout<<"grade is C";}

else

{cout<<"grade is f";}

Step5  stop

Code #include <iostream>

using namespace std;

int main()

{

int m1,m2,m3,avg;

cout<<"enter the marks";

cin>>m1>>m2>>m3;

avg=(m1+m2+m3)/3;

if(avg>=75)

{cout<<"A+ with distinction";}

else if(avg>=60 && avg<75)

{cout<<"grade is A";}

else if(avg>=45 && avg<60)

{cout<<"grade is B";}

else if(avg>=40 && avg<45)

{cout<<"grade is C";}

else

{cout<<"grade is f";}

}


Output

 


2Aim tax system

Algorithm step1 Start

Step2. Enter money

Step3  if(salary>=800000)

{

tax=salary*30/100;

netpay=salary-tax;

}

else if(salary>=500000)

{

tax=salary*20/100;

netpay=salary-tax;

 

}

else if(salary>=250000)

{

tax=salary*20/100;

netpay=salary-tax;

 

}

 else

netpay=salary;

cout<<"____________________"<<endl;

cout<<"netpay salary is= "<<netpay<<endl;

cout<<"tax is : "<<tax<<endl;

}

Step4 stop

Code

#include <iostream>

Using namespace std;

Int main()

{

 Int netpay, tax=0, salary;

Cout<<”enter your salary”;

Cin>>salary;

If(salary>=800000)

{

Tax=salary*30/100;

Netpay=salary-tax;

}

Else if(salary>=500000)

{

Tax=salary*20/100;

Netpay=salary-tax;

 

}

Else if(salary>=250000)

{

Tax=salary*20/100;

Netpay=salary-tax;

 

}

 Else

Netpay=salary;

Cout<<”____________________”<<endl;

Cout<<”netpay salary is= “<<netpay<<endl;

Cout<<”tax is : “<<tax<<endl;

}

 


Output

 


3Aim 1 to 10 print

Step1 start

Step2  read i

Step3 for(i=1; i<=10; i++)

{

cout<<i;

 cout<<"\n";

Step4 stop

Code #include <iostream>

using namespace std;

int main()

{

 int i;

for(i=1; i<=10; i++)

{

cout<<i;

 cout<<"\n";

}

 

}


 

Output


4Aim input by user get table

Step1 start

Step2 Read i,j,k

Step3 cout<<"enter any number";

cin>>k;

for(i=1; i<=10; i++)

{

cout<<k<<"*"<<i<<"="<<k*i;

cout<<"\n";

}

Step4 stop

Code   #include <iostream>

using namespace std;

int main()

{

 int i,j,k;

cout<<"enter any number";

cin>>k;

for(i=1; i<=10; i++)

{

cout<<k<<"*"<<i<<"="<<k*i;

cout<<"\n";

}

}


 

Output


5Aim  perfect number

Algorithm

 

Step1 start

Step2 read i,sum,n

Step3 for(i=1;i<n;i++)

{

if(n%i==0)

{

sum=sum+i;

}

else

{

sum=sum;

 

}

}

if(n==sum)

{

cout<<"number is perfect";

}

else

{

cout<<"number is not perfect";

}

}

Code #include <iostream>

using namespace std;

int main()

{

int i,sum,n;

cout<<"enter any number";

cin>>n;

for(i=1;i<n;i++)

{

if(n%i==0)

{

sum=sum+i;

}

else

{

sum=sum;

 

}

}

if(n==sum)

{

cout<<"number is perfect";

}

else

{

cout<<"number is not perfect";

}

}


Output


6Aim Armstrong number

Step1 start

Step 2 read n,r,sum=0,temp

Step3 while(n>0)

 

{

R=n%10;

Sum=sum+(r*r*r);

N=n/10;

 

}

If(temp==sum)

{

Cout<<”Armstrong number”;

}

Else

{

Cout<<”not Armstrong number”;

}

}

Step4 stop

Code  #include <iostream>

using namespace std;

int main()

{

int n,r,sum=0,temp;

cout<<"enter any number";

cin>>n;

temp=n;

 

while(n>0)

 

{

r=n%10;

sum=sum+(r*r*r);

n=n/10;

 

}

if(temp==sum)

{

cout<<"Armstrong number";

}

else

{

cout<<"not Armstrong number";

}

}


Output


7 stock implementation using array

Algorithm

Step1 start

Step2 enter 1,2,3,4;

Step3  void push (int val)

 

{

    If (top>=n-1)

    Cout<<”stack overflow”;

    Else{

        Top++;

        Stack[top]=val;

       

   

       

    }

 

   

   

}

  Void pop(){

      If (top==-1)

      Cout<<”stack underflow”;

      Else{

          Cout<<”deleted element=”<<stack[top];

          Top--;

         

      }

  }

   Void display()

       {

       If(top>=0){

  

  

   Cout<<”stack elements are” ;

   For(int i=top; i>=0; i--)

   Cout<<stack[i];

       }

  

   Else

  

   Cout<<”stack is empty”;

    }

  

  

   Int main(){

       Int ch,val;

       Cout<<”1.push in stack\n”;

       Cout<<”2.pop from stack\n”;

       Cout<<”3.display stack\n”;

       Cout<<”4.exit\n”;

      

  

   Do

       {cout<<”\nenter a choice \n”;

       Cin>>ch;

   Switch(ch){

       Case 1:{cout<<”enter elements insult\n”;

       Cin>>val;

       Push(val);

       Break;

       }

       Case 2: {pop();

       Break;}

       Case 3:{display();

       Break;}

       Case 4:{cout<<”exit”;

       Break;}

       Default:{cout<<”invalid choice “;}

   }

   }

   While(ch!=4);

   Return 0;

   }

Step4 stop

Code #include <iostream>

 

using namespace std;

int stack [100],n=100,top=-1;

 void push (int val)

 

{

    if (top>=n-1)

    cout<<"stack overflow";

    else{

        top++;

        stack[top]=val;

       

   

       

    }

 

   

   

}

  void pop(){

      if (top==-1)

      cout<<"stack underflow";

      else{

          cout<<"deleted element="<<stack[top];

          top--;

         

      }

  }

   void display()

       {

       if(top>=0){

  

  

   cout<<"stack elements are" ;

   for(int i=top; i>=0; i--)

   cout<<stack[i];

       }

  

   else

  

   cout<<"stack is empty";

    }

  

  

   int main(){

       int ch,val;

       cout<<"1.push in stack\n";

       cout<<"2.pop from stack\n";

       cout<<"3.display stack\n";

       cout<<"4.exit\n";

      

  

   do

       {cout<<"\nenter a choice \n";

       cin>>ch;

   switch(ch){

       case 1:{cout<<"enter elements insult\n";

       cin>>val;

       push(val);

       break;

       }

       case 2: {pop();

       break;}

       case 3:{display();

       break;}

       case 4:{cout<<"exit";

       break;}

       default:{cout<<"invalid choice ";}

   }

   }

   while(ch!=4);

   return 0;

   }


Output


8Aim array implementation of queue

Algorithm

 step1 start

Step2 enter 1,2,3,4

Step3    if (rear == n - 1)

   cout<<"Queue Overflow";

   else {

      if (front == - 1)

      front = 0;

      cout<<"Insert the element in queue : ";

      cin>>val;

      rear++;

      queue[rear] = val;

   }

}

void Delete() {

   if (front == - 1 || front == rear) {

      cout<<"Queue Underflow ";

      return ;

   } else {

      cout<<"Element deleted from queue is : "<< queue[front];

      front++;;

   }

}

void Display() {

   if (front == - 1)

   cout<<"Queue is empty";

   else {

      cout<<"Queue elements are : ";

      for (int i = front; i <= rear; i++)

      cout<<queue[i]<<" ";

        

   }

}

int main() {

   int ch;

   cout<<"1. Insert element to queue\n";

   cout<<"2. Delete element from queue\n";

   cout<<"3. Display all the elements of queue\n";

   cout<<"4. Exit\n";

   do {

      cout<<"Enter your choice : ";

      cin>>ch;

      switch (ch) {

         case 1: Insert();

         break;

         case 2: Delete();

         break;

         case 3: Display();

         break;

         case 4: cout<<"Exit";

         break;

         default: cout<<"Invalid choice";

      }

   } while(ch!=4);

Step4 stop

Code

#include <iostream>

 

using namespace std;

int queue[100], n = 100, front = - 1, rear = - 1;

void Insert() {

   int val;

   if (rear == n - 1)

   cout<<"Queue Overflow";

   else {

      if (front == - 1)

      front = 0;

      cout<<"Insert the element in queue : ";

      cin>>val;

      rear++;

      queue[rear] = val;

   }

}

void Delete() {

   if (front == - 1 || front == rear) {

      cout<<"Queue Underflow ";

      return ;

   } else {

      cout<<"Element deleted from queue is : "<< queue[front];

      front++;;

   }

}

void Display() {

   if (front == - 1)

   cout<<"Queue is empty";

   else {

      cout<<"Queue elements are : ";

      for (int i = front; i <= rear; i++)

      cout<<queue[i]<<" ";

        

   }

}

int main() {

   int ch;

   cout<<"1. Insert element to queue\n";

   cout<<"2. Delete element from queue\n";

   cout<<"3. Display all the elements of queue\n";

   cout<<"4. Exit\n";

   do {

      cout<<"Enter your choice : ";

      cin>>ch;

      switch (ch) {

         case 1: Insert();

         break;

         case 2: Delete();

         break;

         case 3: Display();

         break;

         case 4: cout<<"Exit";

         break;

         default: cout<<"Invalid choice";

      }

   } while(ch!=4);

   return 0;

}

Input



Output

 

 

9Aim linear search program

Algorithm

Step 1 start

Step2 enter 10 elements

Step3

for(i=0;i<10;i++)

cin>>a[i];

cout<<"enter the number to be searched\n";

cin>>num;

for(i=0;i<10;i++)

{

if(a[i]==num)

{index=i;

break;

}

}

cout<<"\nelement found at index no"<<index;

return 0;

}

Step4  stop

Code   #include <iostream>

using namespace std;

int main()

{

int a[10],i,num,index;

cout<<"\n"<<"enter 10 numbers";

for(i=0;i<10;i++)

cin>>a[i];

cout<<"enter the number to be searched\n";

cin>>num;

for(i=0;i<10;i++)

{

if(a[i]==num)

{index=i;

break;

}

}

cout<<"\nelement found at index no"<<index;

return 0;

}

Input


Output


10Aim bubble sort  program in binary search

Algorithm

step1 start

Step2 enter value

Step3 for(i=0;i<n;i++)

 

{

cin>>arr[i];

 

}

cout<<"sorting using bubble sort";

 

for(i=0;i<(n-1);i++)

 

{

for(j=0;j<(n-i-1);j++)

 

{

if(arr[j]>arr[j+1])

 

{temp=arr[j];

 

arr[j]=arr[j+1];

 

arr[j+1]=temp;

 

}

}

}

cout<<"sorted array is";

 

for(i=0;i<n;i++)

{

 

cout<<arr[i];

 

}

return 0;

}

Step4 stop

Code #include <iostream>

using namespace std;

 

int main()

 

{

 

int n,i,arr[50],j,temp;

 

cout<<"enter size of array";

 

cin>>n;

 

cout<<"enter elements of array";

 

for(i=0;i<n;i++)

 

{

cin>>arr[i];

 

}

cout<<"sorting using bubble sort";

 

for(i=0;i<(n-1);i++)

 

{

for(j=0;j<(n-i-1);j++)

 

{

if(arr[j]>arr[j+1])

 

{temp=arr[j];

 

arr[j]=arr[j+1];

 

arr[j+1]=temp;

 

}

}

}

cout<<"sorted array is";

 

for(i=0;i<n;i++)

{

 

cout<<arr[i];

 

}

return 0;

}

Input


Output

E

11Aim quick sort implementation

Algorithm

Step1 start

Step2 inter elements

Step3 for(i=0;i<n;i++)

 

    {

 

        Cin>>a[i];

 

    }

 

 

 

    For(i=0;i<n-1;i++)

 

    {

 

        Min=a[i];

 

        Loc=i;

 

        For(j=i+1;j<n;j++)

 

        {

 

            If(min>a[j])

 

            {

 

                Min=a[j];

 

                Loc=j;

 

            }

 

        }

 

 

 

        Temp=a[i];

 

        A[i]=a[loc];

 

        A[loc]=temp;

 

    }

 

 

 

    Cout<<”\nSorted list is as follows\n”;

 

    For(i=0;i<n;i++)

 

    {

 

        Cout<<a[i]<<” “;

 

    }

 

 

 

    Return 0;

 

}

Step4 stop

Code

#include<iostream>

 

 

 

using namespace std;

 

 

 

int main()

 

{

 

    int i,j,n,loc,temp,min,a[30];

 

    cout<<"Enter the number of elements:";

 

    cin>>n;

 

    cout<<"\nEnter the elements\n";

 

 

 

    for(i=0;i<n;i++)

 

    {

 

        cin>>a[i];

 

    }

 

 

 

    for(i=0;i<n-1;i++)

 

    {

 

        min=a[i];

 

        loc=i;

 

        for(j=i+1;j<n;j++)

 

        {

 

            if(min>a[j])

 

            {

 

                min=a[j];

 

                loc=j;

 

            }

 

        }

 

 

 

        temp=a[i];

 

        a[i]=a[loc];

 

        a[loc]=temp;

 

    }

 

 

 

    cout<<"\nSorted list is as follows\n";

 

    for(i=0;i<n;i++)

 

    {

 

        cout<<a[i]<<" ";

 

    }

 

 

 

    return 0;

 

}

Input


Output

 


 

12Aim selection sort program using array

Algorithm step1 enter start

Step2 enter value

Step3 for(i=0;i<n;i++)

 

    {

 

        cin>>a[i];

 

    }

 

 

 

    for(i=0;i<n-1;i++)

 

    {

 

        min=a[i];

 

        loc=i;

 

        for(j=i+1;j<n;j++)

 

        {

 

            if(min>a[j])

 

            {

 

                min=a[j];

 

                loc=j;

 

            }

 

        }

 

 

 

        temp=a[i];

 

        a[i]=a[loc];

 

        a[loc]=temp;

 

    }

 

 

 

    cout<<"\nSorted list is as follows\n";

 

    for(i=0;i<n;i++)

 

    {

 

        cout<<a[i]<<" ";

 

    }

Step4 stop

Code

#include<iostream>

 

 

 

using namespace std;

 

 

 

int main()

 

{

 

    int i,j,n,loc,temp,min,a[30];

 

    cout<<"Enter the number of elements:";

 

    cin>>n;

 

    cout<<"\nEnter the elements\n";

 

 

 

    for(i=0;i<n;i++)

 

    {

 

        cin>>a[i];

 

    }

 

 

 

    for(i=0;i<n-1;i++)

 

    {

 

        min=a[i];

 

        loc=i;

 

        for(j=i+1;j<n;j++)

 

        {

 

            if(min>a[j])

 

            {

 

                min=a[j];

 

                loc=j;

 

            }

 

        }

 

 

 

        temp=a[i];

 

        a[i]=a[loc];

 

        a[loc]=temp;

 

    }

 

 

 

    cout<<"\nSorted list is as follows\n";

 

    for(i=0;i<n;i++)

 

    {

 

        cout<<a[i]<<" ";

 

    }

 

 

 

    return 0;

 

}

Input


Output


13Aim merge sort program using array

Algorithm step1 start

Step2 enter value

Step3 for (i = 0; i < n1; i++)

    {

        L[i] = A[p + i];

    }

    for (j = 0; j < n2; j++) {

        R[j] = A[q + j + 1];

    }

    i = 0, j = 0;

    for (k = p; i < n1 && j < n2; k++) {

        if (L[i] < R[j]) {

            A[k] = L[i++];

        }

        else {

            A[k] = R[j++];

        }

    }

    while (i < n1) {

        A[k++] = L[i++];

    }

    while (j < n2) {

        A[k++] = R[j++];

    }

}

void MergeSort(int A[], int p, int r)

{

    int q;

 

    if (p < r) {

        q = (p + r) / 2;

        MergeSort(A, p, q);

        MergeSort(A, q + 1, r);

        Merge(A, p, q, r);

    }

}

int main()

{

    int A_Size;

    cout << "Enter number of elements : ";

    cin >> A_Size;

    int A[A_Size];

    cout << "Enter the array elements : ";

    for (int i = 0; i < A_Size; i++) {

        cin >> A[i];

    }

    MergeSort(A, 0, A_Size - 1);

    for (int i = 0; i < A_Size; i++) {

        cout << A[i] << " ";

    }

    cout << endl;

Step4 stop

Code

#include <iostream>

using namespace std;

void Merge(int A[], int p, int q, int r)

{

    int n1 = q - p + 1, i, j, k;

    int n2 = r - q;

    int L[n1], R[n2];

    for (i = 0; i < n1; i++)

    {

        L[i] = A[p + i];

    }

    for (j = 0; j < n2; j++) {

        R[j] = A[q + j + 1];

    }

    i = 0, j = 0;

    for (k = p; i < n1 && j < n2; k++) {

        if (L[i] < R[j]) {

            A[k] = L[i++];

        }

        else {

            A[k] = R[j++];

        }

    }

    while (i < n1) {

        A[k++] = L[i++];

    }

    while (j < n2) {

        A[k++] = R[j++];

    }

}

void MergeSort(int A[], int p, int r)

{

    int q;

 

    if (p < r) {

        q = (p + r) / 2;

        MergeSort(A, p, q);

        MergeSort(A, q + 1, r);

        Merge(A, p, q, r);

    }

}

int main()

{

    int A_Size;

    cout << "Enter number of elements : ";

    cin >> A_Size;

    int A[A_Size];

    cout << "Enter the array elements : ";

    for (int i = 0; i < A_Size; i++) {

        cin >> A[i];

    }

    MergeSort(A, 0, A_Size - 1);

    for (int i = 0; i < A_Size; i++) {

        cout << A[i] << " ";

    }

    cout << endl;

    return 0;

}

 Input


Output


Comments

Popular Posts