style: format code

This commit is contained in:
yanglbme
2019-08-21 10:10:08 +08:00
parent abc0d365de
commit 69ddc9fc52
101 changed files with 3154 additions and 2984 deletions

View File

@@ -1,30 +1,38 @@
#include<iostream>
#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 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];
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;
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];
else
{
a[j] = a[j + 1];
}
}
}cout<<"Your rotated array is=\t";
for(int j=0;j<n;j++){
cout<<a[j]<<" ";
}
cout << "Your rotated array is=\t";
for (int j = 0; j < n; j++)
{
cout << a[j] << " ";
}
getchar();
return 0;

View File

@@ -1,31 +1,35 @@
#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;
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];}
}
else
{
a[j] = a[j - 1];
}
}
}
cout << "Your rotated array is=\t";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout<<"Your rotated array is=\t";
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
}

View File

@@ -1,4 +1,4 @@
#include<iostream>
#include <iostream>
using namespace std;
struct node
@@ -11,71 +11,69 @@ node *start;
void insert(int x)
{
node *t=start;
if (start!=NULL)
node *t = start;
if (start != NULL)
{
while(t->next!=start)
while (t->next != start)
{
t=t->next;
t = t->next;
}
node *n= new node;
t->next=n;
n->val=x;
n->next=start;
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;
node *n = new node;
n->val = x;
start = n;
n->next = start;
}
}
void remove(int x)
{
node *t=start;
node *t = start;
node *p;
while(t->val!=x)
while (t->val != x)
{
p=t;
t=t->next;
p = t;
t = t->next;
}
p->next=t->next;
p->next = t->next;
delete t;
}
void search(int x)
{
node *t= start;
int found =0;
while(t->next!=start)
node *t = start;
int found = 0;
while (t->next != start)
{
if(t->val==x)
if (t->val == x)
{
cout<<"\nFound";
found=1;
cout << "\nFound";
found = 1;
break;
}
t=t->next;
t = t->next;
}
if(found==0)
if (found == 0)
{
cout<<"\nNot Found";
cout << "\nNot Found";
}
}
void show()
{
node *t=start;
node *t = start;
do
{
cout<<t->val<<"\t";
t=t->next;
}
while(t!=start);
cout << t->val << "\t";
t = t->next;
} while (t != start);
}
int main()
@@ -83,27 +81,34 @@ 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;
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;
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);
} while (choice != 0);
return 0;
}

View File

@@ -1,47 +1,46 @@
#include<iostream>
#include <iostream>
using namespace std;
int queue[10];
int front=0;
int rear=0;
int count=0;
int front = 0;
int rear = 0;
int count = 0;
void Enque(int x)
{
if(count==10)
if (count == 10)
{
cout<<"\nOverflow";
cout << "\nOverflow";
}
else
{
queue[rear]=x;
rear=(rear+1)%10;
queue[rear] = x;
rear = (rear + 1) % 10;
count++;
}
}
void Deque()
{
if (front==rear)
if (front == rear)
{
cout<<"\nUnderflow";
cout << "\nUnderflow";
}
else
{
cout<<"\n"<<queue[front]<<" deleted";
front=(front+1)%10;
cout << "\n"
<< queue[front] << " deleted";
front = (front + 1) % 10;
count--;
}
}
void show()
{
for (int i = 0; i<count; i++)
for (int i = 0; i < count; i++)
{
cout<<queue[(i+front)%10]<<"\t";
cout << queue[(i + front) % 10] << "\t";
}
}
@@ -50,28 +49,26 @@ 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 << "\n1. Enque";
cout << "\n2. Deque";
cout << "\n3. Print";
cout << "\nEnter Your Choice : ";
cin >> ch;
if (ch == 1)
{
cout<<"\nInsert : ";
cin>>x;
cout << "\nInsert : ";
cin >> x;
Enque(x);
}
else if (ch==2)
else if (ch == 2)
{
Deque();
}
else if (ch==3)
else if (ch == 3)
{
show();
}
}
while(ch!=0);
} while (ch != 0);
return 0;
}

View File

@@ -1,28 +1,29 @@
#include <iostream>
int main()
{
int i,j,m,n;
cout <<"Enter size of array 1:";
int i, j, m, n;
cout << "Enter size of array 1:";
cin >> m;
cout <<"Enter size of array 2:";
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))
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++;
if (a[i] < b[j])
i++;
else if (a[i] > b[j])
j++;
else
{
cout << a[i++]<<" ";
cout << a[i++] << " ";
j++;
}
}

View File

@@ -1,4 +1,4 @@
#include<iostream>
#include <iostream>
using namespace std;
struct node
@@ -11,24 +11,24 @@ node *start;
void insert(int x)
{
node *t=start;
if (start!=NULL)
node *t = start;
if (start != NULL)
{
while(t->next!=NULL)
while (t->next != NULL)
{
t=t->next;
t = t->next;
}
node *n= new node;
t->next=n;
n->val=x;
n->next=NULL;
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;
node *n = new node;
n->val = x;
n->next = NULL;
start = n;
}
}
@@ -36,30 +36,27 @@ void reverse(node *p, node *q)
{
if (q->next == NULL)
{
q->next=p;
p->next=NULL;
start=q;
q->next = p;
p->next = NULL;
start = q;
return;
}
else
{
reverse(q, q->next);
q->next=p;
p->next=NULL;
q->next = p;
p->next = NULL;
}
}
void show()
{
node *t=start;
while(t!=NULL)
node *t = start;
while (t != NULL)
{
cout<<t->val<<"\t";
t=t->next;
cout << t->val << "\t";
t = t->next;
}
}
int main()
@@ -75,6 +72,5 @@ int main()
show();
return 0;
}

View File

@@ -1,33 +1,34 @@
#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++)
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++] <<" ";
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;
}
while (i < m)
cout << a[i++] << " ";
while (j < n)
cout << b[j++] << " ";
return 0;
}

View File

@@ -1,41 +1,39 @@
#include <iostream>
using namespace std;
//node defined
class node
{
public:
int data;
node* link;
node *link;
node(int d)
{
data = d;
link = NULL;
}
};
//printing the linked list
void print(node* head)
void print(node *head)
{
node* current = head;
node *current = head;
while (current != NULL)
{
cout << current->data << " ";
current = current-> link;
current = current->link;
}
cout << endl;
}
//creating the linked list with 'n' nodes
node* createlist(int n)
node *createlist(int n)
{
node* head = NULL;
node* t = NULL;
node *head = NULL;
node *t = NULL;
for (int i = 0; i < n; i++)
{
node* temp = NULL;
node *temp = NULL;
int num;
cin >> num;
temp = new node(num);
@@ -45,58 +43,58 @@ node* createlist(int n)
t = temp;
continue;
}
if (t->link == NULL) t->link = temp;
if (t->link == NULL)
t->link = temp;
t = temp;
}
return head;
}
//performing selection sort on the linked list in an iterative manner
void my_selection_sort_linked_list(node* &head)
void my_selection_sort_linked_list(node *&head)
{
node* min = head; //throughout the algorithm 'min' is used to denote the node with min value out of all the nodes left for scanning
//while scanning if we find a node 'X' with value lesser than min,
//then we update the pointers in such a way that 'X' becomes the predecessor of 'min'
node* current = min->link; // 'current' refers to the current node we are scanning
node* previous = min; //'previous' refers to the node that is previous to the current node
node* temp = NULL; // 'temp' in this algo is used to point to the last node of the sorted part of the linked list.
//eg. If at any time instance the state of the linked list is suppose 1->2->5->3->8->NULL
//then, we see that "1->2" is the sorted part of the LL, and therefore temp will be pointing to the last node of the sorted part,i.e,'2'
//We keep on arranging the Linked list in such a way that after each iteration the node with 'min' value is placed at its correct position.
//Eg. Let suppose initially we have 5->4->1->3->2->NULL
//After 1st iteration : 1->4->5->3->2->NULL and so on
while (min->link != NULL) //so that all the nodes are scanned or until there exists a node
{
//pick the first node from the unsorted part and assume that it is the minimum and then start scanning from the next node
node *min = head; //throughout the algorithm 'min' is used to denote the node with min value out of all the nodes left for scanning
//while scanning if we find a node 'X' with value lesser than min,
//then we update the pointers in such a way that 'X' becomes the predecessor of 'min'
node *current = min->link; // 'current' refers to the current node we are scanning
node *previous = min; //'previous' refers to the node that is previous to the current node
node *temp = NULL; // 'temp' in this algo is used to point to the last node of the sorted part of the linked list.
//eg. If at any time instance the state of the linked list is suppose 1->2->5->3->8->NULL
//then, we see that "1->2" is the sorted part of the LL, and therefore temp will be pointing to the last node of the sorted part,i.e,'2'
//We keep on arranging the Linked list in such a way that after each iteration the node with 'min' value is placed at its correct position.
//Eg. Let suppose initially we have 5->4->1->3->2->NULL
//After 1st iteration : 1->4->5->3->2->NULL and so on
while (current != NULL) //suppose you choose the min node to be X, then scan starts from the (X+1)th node until its NULL. current = (X+1)th node and min = X
while (min->link != NULL) //so that all the nodes are scanned or until there exists a node
{
//pick the first node from the unsorted part and assume that it is the minimum and then start scanning from the next node
while (current != NULL) //suppose you choose the min node to be X, then scan starts from the (X+1)th node until its NULL. current = (X+1)th node and min = X
{
if (current->data < min->data) //if the current node is smaller than the presumed node 'min'
if (current->data < min->data) //if the current node is smaller than the presumed node 'min'
{
if (temp == NULL) //temp stays null for the first iteration, therefore it symbolizes that we are scanning for the first time
if (temp == NULL) //temp stays null for the first iteration, therefore it symbolizes that we are scanning for the first time
{
if (previous == min) //if the 'previous' is pointing to the 'min' node
if (previous == min) //if the 'previous' is pointing to the 'min' node
{
//Update the pointers
head = current; //update the head pointer with the current node
//Update the pointers
head = current; //update the head pointer with the current node
min->link = current->link;
current->link = previous;
min = current;
current = previous->link;
}
else //if the 'previous' is not pointing to the 'min' node
else //if the 'previous' is not pointing to the 'min' node
{
//Update the pointers
head = current; //update the head pointer with the current node
//Update the pointers
head = current; //update the head pointer with the current node
previous->link = current->link;
current->link = min;
min = current;
current = previous->link;
}
}
else //if 'temp' is not NULL, i.e., its not the 1st iteration
else //if 'temp' is not NULL, i.e., its not the 1st iteration
{
temp->link = current;
previous->link = current->link;
@@ -105,15 +103,15 @@ void my_selection_sort_linked_list(node* &head)
current = previous->link;
}
}
else //if the current node is greater than min, just move the previous and the current pointer a step further
else //if the current node is greater than min, just move the previous and the current pointer a step further
{
previous = previous->link;
current = current->link;
}
}
//update the pointers. Set 'temp' to the last node in the sorted part. Make 'min' move a step further so that 'min' points to the 1st node of the unsorted part
//start the iteration again
//update the pointers. Set 'temp' to the last node in the sorted part. Make 'min' move a step further so that 'min' points to the 1st node of the unsorted part
//start the iteration again
temp = min;
min = min->link;
previous = min;
@@ -133,7 +131,6 @@ void my_selection_sort_linked_list(node* &head)
// original list is : -1 -2 -3
// sorted list is : -3 -2 -1
// enter the no. of nodes : 8
// 8 7 6 5 4 3 2 1
// original list is : 8 7 6 5 4 3 2 1
@@ -144,19 +141,19 @@ void my_selection_sort_linked_list(node* &head)
// original list is : 5 3 4 1 -2 -4
// sorted list is : -4 -2 1 3 4 5
int main()
{
node* head = NULL;
node *head = NULL;
int n;
cout << "enter the no. of nodes : "; //taking input from user about the number of nodes in linked list
cout << "enter the no. of nodes : "; //taking input from user about the number of nodes in linked list
cin >> n;
if (n == 0) return 0;
head = createlist(n); //creating the list
if (n == 0)
return 0;
head = createlist(n); //creating the list
cout << "original list is : ";
print(head); //printing the original linked list
my_selection_sort_linked_list(head); //applying selection sort
print(head); //printing the original linked list
my_selection_sort_linked_list(head); //applying selection sort
cout << "sorted list is : ";
print(head); //printing the sorted linked list
print(head); //printing the sorted linked list
return 0;
}