mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-28 06:33:20 +08:00
Add files via upload
This commit is contained in:
31
Operations on Datastructures/Array Left Rotation.cpp
Normal file
31
Operations on Datastructures/Array Left Rotation.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
int n,k;
|
||||
cout<<"Enter size of array=\t";
|
||||
cin>>n;
|
||||
cout<<"Enter Number of indeces u want to rotate the array to left=\t";
|
||||
cin>>k;
|
||||
int a[n];
|
||||
cout<<"Enter elements of array=\t";
|
||||
for(int i=0;i<n;i++){
|
||||
cin>>a[i];
|
||||
}
|
||||
int temp=0;
|
||||
for(int i=0;i<k;i++){
|
||||
temp=a[0];
|
||||
for(int j=0;j<n;j++){
|
||||
if(j==n-1){
|
||||
a[n-1]=temp;
|
||||
}
|
||||
else{
|
||||
a[j]=a[j+1];
|
||||
}
|
||||
}
|
||||
}cout<<"Your rotated array is=\t";
|
||||
for(int j=0;j<n;j++){
|
||||
cout<<a[j]<<" ";
|
||||
}
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
31
Operations on Datastructures/Array Right Rotation.cpp
Normal file
31
Operations on Datastructures/Array Right Rotation.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
int n,k;
|
||||
cout<<"Enter size of array=\t";
|
||||
cin>>n;
|
||||
cout<<"Enter Number of indices u want to rotate the array to right=\t";
|
||||
cin>>k;
|
||||
int a[n];
|
||||
cout<<"Enter elements of array=\t";
|
||||
for(int i=0;i<n;i++)
|
||||
cin>>a[i];
|
||||
int temp=0;
|
||||
for(int i=0;i<k;i++){
|
||||
temp=a[n-1];
|
||||
for(int j=n-1;j>=0;j--){
|
||||
if(j==0){
|
||||
a[j]=temp;
|
||||
}
|
||||
else{
|
||||
a[j]=a[j-1];}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
cout<<"Your rotated array is=\t";
|
||||
for(int i=0;i<n;i++){
|
||||
cout<<a[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
109
Operations on Datastructures/Circular Linked List.cpp
Normal file
109
Operations on Datastructures/Circular Linked List.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *next;
|
||||
};
|
||||
|
||||
node *start;
|
||||
|
||||
void insert(int x)
|
||||
{
|
||||
node *t=start;
|
||||
|
||||
if (start!=NULL)
|
||||
{
|
||||
while(t->next!=start)
|
||||
{
|
||||
t=t->next;
|
||||
}
|
||||
node *n= new node;
|
||||
t->next=n;
|
||||
n->val=x;
|
||||
n->next=start;
|
||||
}
|
||||
else
|
||||
{
|
||||
node *n= new node;
|
||||
n->val=x;
|
||||
start=n;
|
||||
n->next=start;
|
||||
}
|
||||
}
|
||||
|
||||
void remove(int x)
|
||||
{
|
||||
node *t=start;
|
||||
node *p;
|
||||
while(t->val!=x)
|
||||
{
|
||||
p=t;
|
||||
t=t->next;
|
||||
}
|
||||
p->next=t->next;
|
||||
delete t;
|
||||
}
|
||||
|
||||
void search(int x)
|
||||
{
|
||||
node *t= start;
|
||||
int found =0;
|
||||
while(t->next!=start)
|
||||
{
|
||||
if(t->val==x)
|
||||
{
|
||||
cout<<"\nFound";
|
||||
found=1;
|
||||
break;
|
||||
}
|
||||
t=t->next;
|
||||
}
|
||||
if(found==0)
|
||||
{
|
||||
cout<<"\nNot Found";
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
node *t=start;
|
||||
do
|
||||
{
|
||||
cout<<t->val<<"\t";
|
||||
t=t->next;
|
||||
}
|
||||
while(t!=start);
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int choice, x;
|
||||
do
|
||||
{
|
||||
cout<<"\n1. Insert";
|
||||
cout<<"\n2. Delete";
|
||||
cout<<"\n3. Search";
|
||||
cout<<"\n4. Print";
|
||||
cout<<"\n\nEnter you choice : ";
|
||||
cin>>choice;
|
||||
switch (choice)
|
||||
{
|
||||
case 1 : cout<<"\nEnter the element to be inserted : ";
|
||||
cin>>x;
|
||||
insert(x); break;
|
||||
case 2 : cout<<"\nEnter the element to be removed : ";
|
||||
cin>>x;
|
||||
remove(x); break;
|
||||
case 3 : cout<<"\nEnter the element to be searched : ";
|
||||
cin>>x;
|
||||
search(x); break;
|
||||
case 4 : show(); break;
|
||||
}
|
||||
}
|
||||
while(choice!=0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
77
Operations on Datastructures/Circular Queue Using Array.cpp
Normal file
77
Operations on Datastructures/Circular Queue Using Array.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int queue[10];
|
||||
int front=0;
|
||||
int rear=0;
|
||||
int count=0;
|
||||
|
||||
void Enque(int x)
|
||||
{
|
||||
if(count==10)
|
||||
{
|
||||
cout<<"\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
queue[rear]=x;
|
||||
rear=(rear+1)%10;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
void Deque()
|
||||
{
|
||||
if (front==rear)
|
||||
{
|
||||
cout<<"\nUnderflow";
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
cout<<"\n"<<queue[front]<<" deleted";
|
||||
front=(front+1)%10;
|
||||
count--;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
for (int i = 0; i<count; i++)
|
||||
{
|
||||
cout<<queue[(i+front)%10]<<"\t";
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int ch, x;
|
||||
do
|
||||
{
|
||||
cout<<"\n1. Enque";
|
||||
cout<<"\n2. Deque";
|
||||
cout<<"\n3. Print";
|
||||
cout<<"\nEnter Your Choice : ";
|
||||
cin>>ch;
|
||||
if (ch==1)
|
||||
{
|
||||
cout<<"\nInsert : ";
|
||||
cin>>x;
|
||||
Enque(x);
|
||||
}
|
||||
else if (ch==2)
|
||||
{
|
||||
Deque();
|
||||
}
|
||||
else if (ch==3)
|
||||
{
|
||||
show();
|
||||
}
|
||||
}
|
||||
while(ch!=0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
30
Operations on Datastructures/Intersection_of_2_arrays.cpp
Normal file
30
Operations on Datastructures/Intersection_of_2_arrays.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
int main()
|
||||
{
|
||||
int i,j,m,n;
|
||||
cout <<"Enter size of array 1:";
|
||||
cin >> m;
|
||||
cout <<"Enter size of array 2:";
|
||||
cin >> n;
|
||||
int a[m];
|
||||
int b[n];
|
||||
cout <<"Enter elements of array 1:";
|
||||
for(i=0;i<m;i++)
|
||||
cin >> a[i];
|
||||
for(i=0;i<n;i++)
|
||||
cin >> b[i];
|
||||
i=0;j=0;
|
||||
while((i<m)&&(j<n))
|
||||
{
|
||||
if(a[i]<b[j])
|
||||
i++;
|
||||
else if(a[i]>b[j])
|
||||
j++;
|
||||
else
|
||||
{
|
||||
cout << a[i++]<<" ";
|
||||
j++;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *next;
|
||||
};
|
||||
|
||||
node *start;
|
||||
|
||||
void insert(int x)
|
||||
{
|
||||
node *t=start;
|
||||
if (start!=NULL)
|
||||
{
|
||||
while(t->next!=NULL)
|
||||
{
|
||||
t=t->next;
|
||||
}
|
||||
node *n= new node;
|
||||
t->next=n;
|
||||
n->val=x;
|
||||
n->next=NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
node *n= new node;
|
||||
n->val=x;
|
||||
n->next=NULL;
|
||||
start=n;
|
||||
}
|
||||
}
|
||||
|
||||
void reverse(node *p, node *q)
|
||||
{
|
||||
if (q->next == NULL)
|
||||
{
|
||||
q->next=p;
|
||||
p->next=NULL;
|
||||
start=q;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
reverse(q, q->next);
|
||||
q->next=p;
|
||||
p->next=NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void show()
|
||||
{
|
||||
node *t=start;
|
||||
while(t!=NULL)
|
||||
{
|
||||
cout<<t->val<<"\t";
|
||||
t=t->next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
insert(1);
|
||||
insert(2);
|
||||
insert(3);
|
||||
insert(4);
|
||||
insert(5);
|
||||
insert(6);
|
||||
|
||||
reverse(start, start->next);
|
||||
|
||||
show();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
33
Operations on Datastructures/Union_of_2_arrays.cpp
Normal file
33
Operations on Datastructures/Union_of_2_arrays.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <iostream>
|
||||
int main()
|
||||
{
|
||||
int m,n,i=0,j=0;
|
||||
cout << "Enter size of both arrays:";
|
||||
cin >> m >> n;
|
||||
int a[m];
|
||||
int b[n];
|
||||
cout << "Enter elements of array 1:";
|
||||
for(i=0;i<m;i++)
|
||||
cin >>a[i];
|
||||
cout << "Enter elements of array 2:";
|
||||
for(i=0;i<n;i++)
|
||||
cin >> b[i];
|
||||
i=0;j=0;
|
||||
while((i<m)&&(j<n))
|
||||
{
|
||||
if(a[i]<b[j])
|
||||
cout << a[i++] <<" ";
|
||||
else if(a[i]>b[j])
|
||||
cout << b[j++] <<" ";
|
||||
else
|
||||
{
|
||||
cout << a[i++];
|
||||
j++;
|
||||
}
|
||||
}
|
||||
while(i<m)
|
||||
cout<< a[i++] <<" ";
|
||||
while(j<n)
|
||||
cout<< b[j++]<<" ";
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user