Showing posts with label #data_structure codes. Show all posts
Showing posts with label #data_structure codes. Show all posts

Sunday, December 5, 2021

simple program using circular queue in data structure

 #include<iostream>

using namespace std;

class CircularQueue {

  private:

  int front;

  int rear;

  int arr[5];

  public:

    CircularQueue() {

      front = -1;

      rear = -1;

      for (int i = 0; i < 5; i++)

   {

        arr[i] = 0;

      }

    }

  void enqueue(int val) {

    if (rear==4 && front==0) {

      cout << "Queue full" << endl;

      return;

    }

else if (front==-1 && rear==-1){

      rear = 0;

      front = 0;

      arr[rear] = val;


    } 

else if(front!=0 && rear==4)

{

rear=0;

arr[rear]=val;

}else {

      rear = (rear + 1) % 5;

      arr[rear] = val;


    }

  }


  int dequeue() {

    int x = 0;

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

      cout << "Queue is Empty" << endl;

      return x;

    } else if (rear == front) {

      x = arr[rear];

      rear = -1;

      front = -1;

      return x;

    } else {

      cout << "element delete from queue is: " <<arr[front] << endl;

      x = arr[front];

      arr[front] = 0;

      front = (front + 1) % 5;

      return x;

    }

  }


  void display() {

    cout << "All values in the Queue are - " << endl;

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

      cout << arr[i] << "  ";

    }

  }

};

int main()

{

    CircularQueue obj;

cout<<"\t1.Enquque"<<endl;

cout<<"\t2.Dequeue"<<endl;

cout<<"\t3.Display"<<endl;

int c;

while(true)

{

cout<<"please enter your choice:";

cin>>c;

switch(c)

{

case 1:

int no;

cout<<"please enter element in queue:";

cin>>no;

obj.enqueue(no);

break;

case 2:

obj.dequeue();

break;

case 3:

obj.display();

break;

default:

cout<<"wrong choice:"<<endl;

break;

}

  }

}


Friday, November 26, 2021

simple program using merge sort in data structure

#include<iostream>

using namespace std;

void merge(int A[], int mid, int low, int high)

{

    int i, j, k, B[100];

    i = low;

    j = mid + 1;

    k = low;


    while (i <= mid && j <= high)

    {

        if (A[i] < A[j])

        {

            B[k] = A[i];

            i++;

            k++;

        }

        else

        {

            B[k] = A[j];

            j++;

            k++;

        }

    }

    while (i <= mid)

    {

        B[k] = A[i];

        k++;

        i++;

    }

    while (j <= high)

    {

        B[k] = A[j];

        k++;

        j++;

    }

    for (int i = low; i <= high; i++)

    {

        A[i] = B[i];

    }

}

void mergeSort(int A[], int low, int high){

    int mid; 

    if(low<high){

        mid = (low + high) /2;

        cout<<"Midle point of this array:"<<A[mid]<<endl;

        cout<<"index of midle point is:"<<mid<<endl;

        mergeSort(A, low, mid);

        mergeSort(A, mid+1, high);

        merge(A, mid, low, high);

    }

}


int main()

{

int n;

cout<<"please enter the size of array:";

cin>>n;

int A[n];

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

    {

    cout<<endl;

    cout<<"enter "<<i<<" element in array:";

    cin>>A[i];

}

cout<<endl;

cout<<"\t\tBefore Sorting"<<endl;

  cout<<"[";

  //use loop for print element 

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

  {

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

}

cout<<"]";

////////////////////////////////////

cout<<endl<<endl;

   mergeSort(A, 0, n-1);

//print element of array after sorting

cout<<"\t\tAfter Sorting"<<endl;

  cout<<"[";

  //use loop for print element 

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

  {

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

}

cout<<"]";

} 

Friday, November 19, 2021

simple program using quick sort in data structure

 #include<iostream>

using namespace std;

int partition(int arr[],int low,int high)

{

int pivot=arr[low];

cout<<endl;

cout<<"pivot is:"<<pivot<<endl;

cout<<"index:"<<low<<endl;

int p=low+1;

int q=high;

int temp;

do

{

while(arr[p]<=pivot)

{

p++;

}

while(arr[q]>pivot)

{

q--;

}

if(p<q)

{

temp=arr[p];

arr[p]=arr[q];

arr[q]=temp;

}

}while(p<q);

temp=arr[low];

arr[low]=arr[q];

arr[q]=temp;

return q;

}

void quicksort(int arr[],int low,int high)

{

if(low<high)

{

int par_ind;

par_ind=partition(arr,low,high);

{

quicksort(arr,low,par_ind-1);

quicksort(arr,par_ind+1,high);

}

}

}

int main()

{

int arr[]={4,5,8,3,2};

int n=5;

cout<<"\t\tBefore Sorting"<<endl;

  cout<<"[";

  //use loop for print element

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

  {

  cout<<arr[i]<<",";

}

cout<<"]";

////////////////////////////////////

quicksort(arr,0,n-1);

cout<<endl<<endl;

//print element of array after sorting

cout<<"\t\tAfter Sorting"<<endl;

  cout<<"[";

  //use loop for print element

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

  {

  cout<<arr[i]<<",";

}

cout<<"]";

}

simple program using stack in data structure

#include<iostream>

using namespace std;

class stackid

{

private:

int top;

int arr[5];

public:

stackid()

{

top=-1;

for(int i=0;i<5;i++)

{

arr[i]=0;

}

}

void Push()

{

int pu;

if(top==4)

{

cout<<"Queue is Full:"<<endl;

}

else

{

    cout<<"-------------------------"<<endl;

    cout<<"Enter element in stack:";

cin>>pu;

cout<<"-------------------------"<<endl;

top++;

arr[top]=pu;

}

}

void pop()

{

if(top==-1)

{

cout<<"Queue is already empty"<<endl;

}

else

{

cout<<"-----------------------------------------"<<endl;

cout<<"element deleted from stack is:"<<arr[top]<<endl;

cout<<"-----------------------------------------"<<endl;

top--;

}

}

void display()

{

for(int i=0;i<5;i++)

{

cout<<arr[top]<<"  ";

cout<<endl;

top--;

}

}


};

int main()

{

stackid obj;

cout<<"\t1.Push"<<endl;

cout<<"\t2.pop"<<endl;

cout<<"\t3.display"<<endl;

int c;

while(true)

{

cout<<"please enter your choice:";

cin>>c;

switch(c)

{

case 1:

obj.Push();


break;

case 2:

obj.pop();

break;

case 3:

obj.display();

break;

default:

cout<<"wrong choice:"<<endl;

break;

}

  }

} 

simple program using queue in data structure

#include<iostream>

using namespace std;

class Queue

{

private:

int front;

int rear;

int arr[5];

public:

Queue()

{

front=-1;

rear=-1;

for(int i=0;i<5;i++)

{

arr[i]=0;

}

}

void Enqueue()

{

int en;

if(rear==4)

{

cout<<"Queue is Full:"<<endl;

}

else

{

if(rear==-1 && front==-1)

{

front=0;

}

    cout<<"-------------------------"<<endl;

    cout<<"Enter element in Queue:";

cin>>en;

cout<<"-------------------------"<<endl;

rear++;

arr[rear]=en;

}

}

void Dequeue()

{

if(rear==-1 && front==-1)

{

cout<<"Queue is already empty"<<endl;

}

else

{

cout<<"-----------------------------------------"<<endl;

cout<<"element deleted from queue is:"<<arr[front]<<endl;

cout<<"-----------------------------------------"<<endl;

front++;

}

}

void Display()

{

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

{

cout<<"----------------"<<endl;

cout<<arr[i]<<" "<<endl;

cout<<"----------------"<<endl;

}

}

int count()

{

return(rear-front+1);

}

};

int main()

{

Queue obj;

cout<<"\t1.Enquque"<<endl;

cout<<"\t2.Dequeue"<<endl;

cout<<"\t3.Display"<<endl;

cout<<"\t4.count"<<endl;

int c;

while(true)

{

cout<<"please enter your choice:";

cin>>c;

switch(c)

{

case 1:

obj.Enqueue();

break;

case 2:

obj.Dequeue();

break;

case 3:

obj.Display();

break;

case 4:

cout<<obj.count()<<" values are in queue"<<endl;

break;

default:

cout<<"wrong choice:"<<endl;

break;

}

  }

} 

simple program using insertion sort in data structure

 //insertion sort

 #include<iostream>

 using namespace std;

 int main()

 {

  int n,key,j,elements;

  cout<<"Enter the Size of Array here:";

  cin>>n;

  int arr[n];

  cout<<"Enter Element in Array:"<<endl;

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

  {

  cin>>arr[i];

}

  cout<<"\t\tBefore Sorting"<<endl;

  cout<<"[";

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

  {

  cout<<arr[i]<<",";

}

cout<<"]";

//loop for passes

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

{

key=arr[i];

j=i-1;

//loop for each pass

while(j>=0 && arr[j]>key)

{

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

j--;a

}

arr[j+1]=key;

}

cout<<endl<<endl;

cout<<"\t\tAfter Sorting"<<endl;

cout<<"[";

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

  {

  cout<<arr[i]<<",";

}

cout<<"]";

 }

simple program using linear search algorithm in data structure

 #include<iostream>

using namespace std;

int main()

{

int search,len,count;

cout<<"enter length of arr:";

cin>>len;

int arr[len];

cout<<"please enter the element in array:"<<endl;

for(int i=0;i<len;i++)

{

cin>>arr[i];

}

cout<<"please search element from array:";

cin>>search;

for(int i=0;i<len;i++)

{

if(arr[i]==search)

{

  cout<<"element found on index "<<i<<endl;

  count++;

}

}

}

simple program using Bubble sort in data structure

                                                  //bubble sort

#include<iostream>

using namespace std;

int main()

{

int n,temp,j,swap_counter=0,pass_counter=0,arr0_counter=0,comp_counter=0;

//here we enter the size of array

cout<<"Please Enter The Size Of Array:";

cin>>n;

int arr[n];

//here we enter the element in an array

cout<<"Please Enter Element in Array:";

//use loop for enter element in array

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

{

cin>>arr[i];

}

cout<<endl<<endl;

//print element of array before sorting

  cout<<"\t\tBefore Sorting"<<endl;

  cout<<"[";

  //use loop for print element 

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

  {

  cout<<arr[i]<<",";

}

cout<<"]";

for(int i=0;i<n-1;i++)  //Loop for number of passes

{

for(j=0;j<n-1-i;j++) //Loop for number of comparisons

{

comp_counter++; //here we count the number og comparision

if(j==0)

{

arr0_counter++; //here we count the number of array0 index

}

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

{

swap_counter++; //here we count the number of swaps

cout<<endl;

cout<<"At ["<<j<<"]index :"<<arr[j]<<endl;

cout<<"At ["<<j+1<<"]index :"<<arr[j+1]<<endl;

cout<<arr[j]<<" is greater than "<<arr[j+1]<<"so there will be swaping:"<<endl;

temp=arr[j];

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

arr[j+1]=temp;

cout<<"NOW"<<endl;

cout<<"at ["<<j<<"]index :"<<arr[j]<<endl;

cout<<"at ["<<j+1<<"]index :"<<arr[j+1]<<endl;

}

else

{

cout<<arr[j]<<" is less than "<<arr[j+1]<<"so there will be no swaping:"<<endl;

}

}

pass_counter++; //here we count the number of passes

}

cout<<"Number Of Swaps Are:"<<swap_counter<<endl;

cout<<"Number Of Passes Are:"<<pass_counter<<endl;

cout<<"Go To 0 Index For "<<arr0_counter<<" times"<<endl;

cout<<"Number Of Comparison:"<<comp_counter<<endl;

cout<<endl<<endl;

//print element of array after sorting

cout<<"\t\tAfter Sorting"<<endl;

  cout<<"[";

  //use loop for print element 

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

  {

  cout<<arr[i]<<",";

}

cout<<"]";

}