mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-02 02:02:23 +08:00
Removed duplicate files
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
#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]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int binary_search(int a[],int l,int r,int key){
|
||||
while(l<=r){
|
||||
int m = l+(r-l)/2;
|
||||
if(key==a[m])
|
||||
return m;
|
||||
else if(key<a[m])
|
||||
r = m-1;
|
||||
else
|
||||
l = m+1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int n,key;
|
||||
cout<<"Enter size of array: ";
|
||||
cin>>n;
|
||||
cout<<"Enter array elements: ";
|
||||
int a[n];
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
cin>>a[i];
|
||||
}
|
||||
cout<<"Enter search key: ";
|
||||
cin>>key;
|
||||
int res = binary_search(a,0,n-1,key);
|
||||
if(res != -1)
|
||||
cout<<key<<" found at index "<<res<<endl;
|
||||
else
|
||||
cout<<key<<" not found"<<endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
//A buzz number is a number that is either divisble by 7 or has last digit as 7.
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int n,t;
|
||||
cin >> t;
|
||||
while(t--)
|
||||
{
|
||||
cin >> n;
|
||||
if((n%7==0)||(n%10==7))
|
||||
cout << n << " is a buzz number" << endl;
|
||||
else
|
||||
cout << n << " is not a buzz number" << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -1,125 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *prev;
|
||||
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->prev=t;
|
||||
n->val=x;
|
||||
n->next=NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
node *n= new node;
|
||||
n->val=x;
|
||||
n->prev=NULL;
|
||||
n->next=NULL;
|
||||
start=n;
|
||||
}
|
||||
}
|
||||
|
||||
void remove(int x)
|
||||
{
|
||||
node *t=start;
|
||||
while(t->val!=x)
|
||||
{
|
||||
t=t->next;
|
||||
}
|
||||
t->prev->next=t->next;
|
||||
t->next->prev=t->prev;
|
||||
delete t;
|
||||
}
|
||||
|
||||
void search(int x)
|
||||
{
|
||||
node *t= start;
|
||||
int found =0;
|
||||
while(t!=NULL)
|
||||
{
|
||||
if(t->val==x)
|
||||
{
|
||||
cout<<"\nFound";
|
||||
found=1;
|
||||
break;
|
||||
}
|
||||
t=t->next;
|
||||
}
|
||||
if(found==0)
|
||||
{
|
||||
cout<<"\nNot Found";
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
node *t=start;
|
||||
while(t!=NULL)
|
||||
{
|
||||
cout<<t->val<<"\t";
|
||||
t=t->next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void reverseShow()
|
||||
{
|
||||
node *t=start;
|
||||
while(t->next!=NULL)
|
||||
{
|
||||
t=t->next;
|
||||
}
|
||||
while(t!=NULL)
|
||||
{
|
||||
cout<<t->val<<"\t";
|
||||
t=t->prev;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int choice, x;
|
||||
do
|
||||
{
|
||||
cout<<"\n1. Insert";
|
||||
cout<<"\n2. Delete";
|
||||
cout<<"\n3. Search";
|
||||
cout<<"\n4. Forward print";
|
||||
cout<<"\n5. Reverse 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;
|
||||
case 5 : reverseShow(); break;
|
||||
}
|
||||
}
|
||||
while(choice!=0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
//This program aims at calculating the GCD of n numbers by division method
|
||||
#include <iostream>
|
||||
using namepsace std;
|
||||
int main()
|
||||
{
|
||||
cout <<"Enter value of n:"<<endl;
|
||||
cin >> n;
|
||||
int a[n];
|
||||
int i,j,gcd;
|
||||
cout << "Enter the n numbers:" << endl;
|
||||
for(i=0;i<n;i++)
|
||||
cin >> a[i];
|
||||
j=1; //to access all elements of the array starting from 1
|
||||
gcd=a[0];
|
||||
while(j<n)
|
||||
{
|
||||
if(a[j]%gcd==0) //value of gcd is as needed so far
|
||||
j++; //so we check for next element
|
||||
else
|
||||
gcd=a[j]%gcd; //calculating GCD by division method
|
||||
}
|
||||
cout << "GCD of entered n numbers:" << gcd;
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int LinearSearch(int *array, int size, int key)
|
||||
{
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
if (array[i]==key)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int size;
|
||||
cout<<"\nEnter the size of the Array : ";
|
||||
cin >> size;
|
||||
|
||||
int array[size];
|
||||
int key;
|
||||
|
||||
//Input array
|
||||
cout<<"\nEnter the Array of " << size << " numbers : ";
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
cin>>array[i];
|
||||
}
|
||||
|
||||
cout<<"\nEnter the number to be searched : ";
|
||||
cin>>key;
|
||||
|
||||
int index=LinearSearch(array, size, key);
|
||||
if (index!=-1)
|
||||
{
|
||||
cout<<"\nNumber found at index : "<<index;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"\nNot found";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
185
List Array.cpp
185
List Array.cpp
@@ -1,185 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
struct list
|
||||
{
|
||||
int data[50];
|
||||
int top=0;
|
||||
bool isSorted=false;
|
||||
|
||||
int BinarySearch(int *array, int first, int last, int x)
|
||||
{
|
||||
if(last<first)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
int mid=(first+last)/2;
|
||||
if(array[mid]==x)
|
||||
return mid;
|
||||
else if(x<array[mid])
|
||||
return (BinarySearch(array, first, mid-1, x));
|
||||
else if(x>array[mid])
|
||||
return (BinarySearch(array, mid+1, last, x));
|
||||
}
|
||||
|
||||
int LinarSearch(int *array, int x)
|
||||
{
|
||||
for (int i = 0; i < top; i++)
|
||||
{
|
||||
if (array[i]==x)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int Search(int x)
|
||||
{
|
||||
int pos=-1;
|
||||
|
||||
if (isSorted)
|
||||
{
|
||||
pos=BinarySearch(data, 0, top-1, x);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
pos=LinarSearch(data, x);
|
||||
}
|
||||
|
||||
if (pos!=-1)
|
||||
{
|
||||
cout<<"\nElement found at position : "<<pos;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"\nElement not found";
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
void Sort()
|
||||
{
|
||||
int i, j, pos;
|
||||
for(i=0; i<top; i++)
|
||||
{
|
||||
int min=data[i];
|
||||
for(j=i+1; j<top; j++)
|
||||
{
|
||||
if(data[j]<min)
|
||||
{
|
||||
pos=j;
|
||||
min=data[pos];
|
||||
}
|
||||
}
|
||||
|
||||
int temp=data[i];
|
||||
data[i]=data[pos];
|
||||
data[pos]=temp;
|
||||
}
|
||||
isSorted=true;
|
||||
}
|
||||
|
||||
void insert(int x)
|
||||
{
|
||||
if(!isSorted)
|
||||
{
|
||||
|
||||
if(top==49)
|
||||
{
|
||||
cout<<"\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
data[top]=x;
|
||||
top++;
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
int pos=0;
|
||||
|
||||
for (int i = 0; i < top-1; i++)
|
||||
{
|
||||
if(data[i]<=x && x<=data[i+1])
|
||||
{
|
||||
pos=i+1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (pos==0)
|
||||
{
|
||||
pos=top-1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (int i = top; i > pos; i--)
|
||||
{
|
||||
data[i]=data[i-1];
|
||||
}
|
||||
top++;
|
||||
data[pos]=x;
|
||||
}
|
||||
}
|
||||
|
||||
void Remove(int x)
|
||||
{
|
||||
int pos=Search(x);
|
||||
cout<<"\n"<<data[pos]<<" deleted";
|
||||
for (int i = pos; i < top; i++)
|
||||
{
|
||||
data[i]=data[i+1];
|
||||
}
|
||||
top--;
|
||||
}
|
||||
|
||||
void Show()
|
||||
{
|
||||
for (int i = 0; i < top; i++)
|
||||
{
|
||||
cout<<data[i]<<"\t";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
list L;
|
||||
int choice;
|
||||
int x;
|
||||
do
|
||||
{
|
||||
cout<<"\n1.Insert";
|
||||
cout<<"\n2.Delete";
|
||||
cout<<"\n3.Search";
|
||||
cout<<"\n4.Sort";
|
||||
cout<<"\n5.Print";
|
||||
cout<<"\n\nEnter Your Choice : ";
|
||||
cin>>choice;
|
||||
switch (choice)
|
||||
{
|
||||
case 1: cout<<"\nEnter the element to be inserted : ";
|
||||
cin>>x;
|
||||
L.insert(x);
|
||||
break;
|
||||
case 2: cout<<"\nEnter the element to be removed : ";
|
||||
cin>>x;
|
||||
L.Remove(x);
|
||||
break;
|
||||
case 3: cout<<"\nEnter the element to be searched : ";
|
||||
cin>>x;
|
||||
L.Search(x);
|
||||
break;
|
||||
case 4: L.Sort();
|
||||
break;
|
||||
case 5: L.Show();
|
||||
break;
|
||||
}
|
||||
}
|
||||
while(choice!=0);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int queue[10];
|
||||
int front=0;
|
||||
int rear=0;
|
||||
|
||||
void Enque(int x)
|
||||
{
|
||||
if(rear==10)
|
||||
{
|
||||
cout<<"\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
queue[rear++]=x;
|
||||
}
|
||||
}
|
||||
|
||||
void Deque()
|
||||
{
|
||||
if (front==rear)
|
||||
{
|
||||
cout<<"\nUnderflow";
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
cout<<"\n"<<queue[front++]<<" deleted";
|
||||
for (int i = front; i < rear; i++)
|
||||
{
|
||||
queue[i-front]=queue[i];
|
||||
}
|
||||
rear=rear-front;
|
||||
front=0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
for (int i = front; i < rear; i++)
|
||||
{
|
||||
cout<<queue[i]<<"\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;
|
||||
}
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *next;
|
||||
};
|
||||
|
||||
|
||||
node *front, *rear;
|
||||
|
||||
|
||||
void Enque(int x)
|
||||
{
|
||||
if (rear==NULL)
|
||||
{
|
||||
node *n= new node;
|
||||
n->val=x;
|
||||
n->next=NULL;
|
||||
rear=n;
|
||||
front=n;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
|
||||
node *n = new node;
|
||||
n->val=x;
|
||||
n->next=NULL;
|
||||
rear->next=n;
|
||||
rear=n;
|
||||
}
|
||||
}
|
||||
|
||||
void Deque()
|
||||
{
|
||||
if (rear==front)
|
||||
{
|
||||
cout<<"\nUnderflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
node *t = front;
|
||||
cout<<"\n"<<t->val<<" deleted";
|
||||
front=front->next;
|
||||
delete t;
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
node *t=front;
|
||||
while(t!=NULL)
|
||||
{
|
||||
cout<<t->val<<"\t";
|
||||
t=t->next;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *next;
|
||||
};
|
||||
|
||||
|
||||
node *top;
|
||||
|
||||
void push(int x)
|
||||
{
|
||||
node *n = new node;
|
||||
n->val=x;
|
||||
n->next=top;
|
||||
top=n;
|
||||
}
|
||||
|
||||
void pop()
|
||||
{
|
||||
if (top==NULL)
|
||||
{
|
||||
cout<<"\nUnderflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
node *t = top;
|
||||
cout<<"\n"<<t->val<<" deleted";
|
||||
top=top->next;
|
||||
delete t;
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
node *t=top;
|
||||
while(t!=NULL)
|
||||
{
|
||||
cout<<t->val<<"\n";
|
||||
t=t->next;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int ch, x;
|
||||
do
|
||||
{
|
||||
cout<<"\n1. Push";
|
||||
cout<<"\n2. Pop";
|
||||
cout<<"\n3. Print";
|
||||
cout<<"\nEnter Your Choice : ";
|
||||
cin>>ch;
|
||||
if (ch==1)
|
||||
{
|
||||
cout<<"\nInsert : ";
|
||||
cin>>x;
|
||||
push(x);
|
||||
}
|
||||
else if (ch==2)
|
||||
{
|
||||
pop();
|
||||
}
|
||||
else if (ch==3)
|
||||
{
|
||||
show();
|
||||
}
|
||||
}
|
||||
while(ch!=0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
Multiply(int A[][], int B[][], int n)
|
||||
{
|
||||
if (n==2)
|
||||
{
|
||||
int p1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
|
||||
int p2= (a[1][0]+a[1][1])*b[0][0];
|
||||
int p3= a[0][0]*(b[0][1]-b[1][1]);
|
||||
int p4= a[1][1]*(b[1][0]-b[0][0]);
|
||||
int p5= (a[0][0]+a[0][1])*b[1][1];
|
||||
int p6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
|
||||
int p7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
|
||||
|
||||
|
||||
int c[n][n];
|
||||
c[0][0]=p1+p4-p5+p7;
|
||||
c[0][1]=p3+p5;
|
||||
c[1][0]=p2+p4;
|
||||
c[1][1]=p1-p2+p3+p6;
|
||||
|
||||
return c[][];
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int p,q,r,s;
|
||||
cout<<"Enter the dimensions of Matrices";
|
||||
cin>>n;
|
||||
int A[n][n],;
|
||||
int B[n][n],;
|
||||
cout<<"Enter the elements of Matrix A";
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j <n ; j++)
|
||||
{
|
||||
cin>>A[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
cout<<"Enter the elements of Matrix B";
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j <n ; j++)
|
||||
{
|
||||
cin>>B[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
Multiply(A, B, n);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
struct tower
|
||||
{
|
||||
int values[10];
|
||||
int top;
|
||||
}F, U, T;
|
||||
|
||||
|
||||
|
||||
void show()
|
||||
{
|
||||
cout<<"\n\n\tF : ";
|
||||
for(int i=0; i<F.top; i++)
|
||||
{
|
||||
cout<<F.values[i]<<"\t";
|
||||
}
|
||||
cout<<"\n\tU : ";
|
||||
for(int i=0; i<U.top; i++)
|
||||
{
|
||||
cout<<U.values[i]<<"\t";
|
||||
}
|
||||
cout<<"\n\tT : ";
|
||||
for(int i=0; i<T.top; i++)
|
||||
{
|
||||
cout<<T.values[i]<<"\t";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void mov(tower &From, tower &To)
|
||||
{
|
||||
--From.top;
|
||||
To.values[To.top]=From.values[From.top];
|
||||
++To.top;
|
||||
}
|
||||
|
||||
void TH(int n, tower &From, tower &Using, tower &To)
|
||||
{
|
||||
|
||||
if (n==1)
|
||||
{
|
||||
mov(From, To);
|
||||
show();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
TH(n-1, From, To, Using);
|
||||
mov(From, To);
|
||||
show();
|
||||
TH(n-1, Using, From, To);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
F.top=0;
|
||||
U.top=0;
|
||||
T.top=0;
|
||||
|
||||
int no;
|
||||
|
||||
cout << "\nEnter number of discs : " ;
|
||||
cin >> no;
|
||||
|
||||
for (int i = no; i >0; i--)
|
||||
{
|
||||
F.values[F.top++]=i;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
show();
|
||||
TH(no, F, U, T);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
//An efficient way to calculate nth fibonacci number faster and simpler than O(nlogn) method of matrix exponentiation
|
||||
//This works by using both recursion and dynamic programming.
|
||||
//as 93rd fibonacci exceeds 19 digits, which cannot be stored in a single long long variable, we can only use it till 92nd fibonacci
|
||||
//we can use it for 10000th fibonacci etc, if we implement bigintegers.
|
||||
//This algorithm works with the fact that nth fibonacci can easily found if we have already found n/2th or (n+1)/2th fibonacci
|
||||
//It is a property of fibonacci similar to matrix exponentiation.
|
||||
|
||||
#include <iostream>
|
||||
#include<cstdio>
|
||||
using namespace std;
|
||||
|
||||
const long long MAX = 93;
|
||||
|
||||
|
||||
long long f[MAX] = {0};
|
||||
|
||||
|
||||
long long fib(long long n)
|
||||
{
|
||||
|
||||
if (n == 0)
|
||||
return 0;
|
||||
if (n == 1 || n == 2)
|
||||
return (f[n] = 1);
|
||||
|
||||
|
||||
if (f[n])
|
||||
return f[n];
|
||||
|
||||
long long k = (n%2!=0)? (n+1)/2 : n/2;
|
||||
|
||||
f[n] = (n%2!=0)? (fib(k)*fib(k) + fib(k-1)*fib(k-1))
|
||||
: (2*fib(k-1) + fib(k))*fib(k);
|
||||
return f[n];
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
//Main Function
|
||||
for(long long i=1;i<93;i++)
|
||||
{
|
||||
cout << i << " th fibonacci number is " << fib(i) << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace std;
|
||||
char paragraph;
|
||||
|
||||
int main()
|
||||
{
|
||||
string paragraph;
|
||||
cout << "Please enter your paragraph: \n";
|
||||
getline (cin,paragraph);
|
||||
cout << "\nHello, your paragraph is:\n " << paragraph << "!\n";
|
||||
cout << "\nThe size of your paragraph = " << paragraph.size() << " characters. \n\n";
|
||||
|
||||
if (paragraph.empty())
|
||||
{
|
||||
cout << "\nThe paragraph is empty" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (true) {
|
||||
string word;
|
||||
cout << "Please enter the word you are searching for: ";
|
||||
getline (cin,word);
|
||||
cout << "Hello, your word is " << word << "!\n";
|
||||
if (paragraph.find(word) == string::npos)
|
||||
{
|
||||
cout << word << " does not exist in the sentence" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "The word " << word << " is now found at location " << paragraph.find(word) << endl << endl;
|
||||
}
|
||||
system("pause");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user