From a9eb2a6ad60267858a20a57e5336bc1296a729b1 Mon Sep 17 00:00:00 2001 From: Raj Kamal Shakya <54174560+9759176595@users.noreply.github.com> Date: Sun, 10 Oct 2021 14:33:29 +0530 Subject: [PATCH] Add files via upload --- C++/basic.cpp | 58 +++++++++++++++++++++++++++++ C++/deletion.cpp | 12 ++++++ C++/inplaceRot.cpp | 55 +++++++++++++++++++++++++++ C++/insertion.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++ C++/rotation.cpp | 26 +++++++++++++ C++/test.cpp | 16 ++++++++ C++/test1.cpp | 33 +++++++++++++++++ C++/tree1.cpp | 25 +++++++++++++ C++/vector.cpp | 42 +++++++++++++++++++++ 9 files changed, 359 insertions(+) create mode 100644 C++/basic.cpp create mode 100644 C++/deletion.cpp create mode 100644 C++/inplaceRot.cpp create mode 100644 C++/insertion.cpp create mode 100644 C++/rotation.cpp create mode 100644 C++/test.cpp create mode 100644 C++/test1.cpp create mode 100644 C++/tree1.cpp create mode 100644 C++/vector.cpp diff --git a/C++/basic.cpp b/C++/basic.cpp new file mode 100644 index 0000000..90dfe17 --- /dev/null +++ b/C++/basic.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +//Declare Node +struct Node{ + int num; + Node *next; +}; + +//Declare starting (Head) node +struct Node *head=NULL; + +//Insert node at start +void insertNode(int n){ + struct Node *newNode=new Node; + newNode->num=n; + newNode->next=head; + head=newNode; +} + +//Traverse/ display all nodes (print items) +void display(){ + if(head==NULL){ + cout<<"List is empty!"<num<<" "; + temp=temp->next; + } + cout<num<<" is removed."<next; +} +int main(){ + + display(); + insertNode(10); + insertNode(20); + insertNode(30); + insertNode(40); + insertNode(50); + display(); + deleteItem(); deleteItem(); deleteItem(); deleteItem(); deleteItem(); + deleteItem(); + display(); + return 0; +} \ No newline at end of file diff --git a/C++/deletion.cpp b/C++/deletion.cpp new file mode 100644 index 0000000..7fa2d29 --- /dev/null +++ b/C++/deletion.cpp @@ -0,0 +1,12 @@ +#include +#include +using namespace std; + +struct Node{ + int data; + struct Node* left, *right; +}; +struct Node* newnode(int key){ + struct Node* temp=new Node; + temp->key=key; +} \ No newline at end of file diff --git a/C++/inplaceRot.cpp b/C++/inplaceRot.cpp new file mode 100644 index 0000000..cc27e63 --- /dev/null +++ b/C++/inplaceRot.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +int gcd(int a, int b) +{ + if(b==0) + return a; + else + return gcd(b, a%b); +} + +void ArrayRotate (int A[], int n, int k) +{ + int d=-1,i,temp,j; + for(i=0;i>n; + int A[n]; + cout<<"Enter Array elements\n"; + for(i=0;i>A[i]; + cout<<"Enter the value of k\n"; + cin>>k; + displayArray(A,n); + ArrayRotate(A,n,k); + displayArray(A,n); + return 0; +} \ No newline at end of file diff --git a/C++/insertion.cpp b/C++/insertion.cpp new file mode 100644 index 0000000..3779b8e --- /dev/null +++ b/C++/insertion.cpp @@ -0,0 +1,92 @@ +#include +#include +using namespace std; + +struct Node{ + int data; + Node *left; + Node *right; +}; +//Fun to create a new node +Node *CreateNode(int data){ + Node* newNode=new Node(); + if(!newNode){ + cout<<"Memory Error!\n"; + return NULL; + } + newNode->data=data; + newNode->left=newNode->right=NULL; + return newNode; +} +//Fun to insert element in binary tree +Node* InsertNode(Node* root, int data) +{ + int temp; + // If the tree is empty, assign new node address to root + if(root== NULL) + { + root=CreateNode(data); + return root; + } + // Else, do level order traversal until we find an empty + // place, i.e. either left child or right child of some + // node is pointing to NULL. + queue q; + q.push(root); + + while (!q.empty()) + Node *temp=q.front(); + q.pop(); + + if(temp->left!=NULL) + q.push(temp->left); + else { + temp->left = CreateNode(data); + return root; + } + + if (temp->right != NULL) + q.push(temp->right); + else { + temp->right = CreateNode(data); + return root; + } + } + + +/* Inorder traversal of a binary tree */ + +void inorder(Node* temp) +{ + if (temp == NULL) + return; + + inorder(temp->left); + cout << temp->data << ' '; + inorder(temp->right); +} + +// Driver code +int main() +{ + Node* root = CreateNode(10); + root->left = CreateNode(11); + root->left->left = CreateNode(7); + root->right = CreateNode(9); + root->right->left = CreateNode(15); + root->right->right = CreateNode(8); + + cout << "Inorder traversal before insertion: "; + inorder(root); + cout << endl; + + int key = 12; + root = InsertNode(root, key); + + cout << "Inorder traversal after insertion: "; + inorder(root); + cout << endl; + + return 0; +} + diff --git a/C++/rotation.cpp b/C++/rotation.cpp new file mode 100644 index 0000000..8e54d3f --- /dev/null +++ b/C++/rotation.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + + +void RotArray(int a[],int n, int k){ + int b[n]; + //Moving k position + for(int i=0;i>n>>k; + int a[n]; + for(int i=0;i>a[i]; + } + RotArray(a,n,k); + + return 0; +} \ No newline at end of file diff --git a/C++/test.cpp b/C++/test.cpp new file mode 100644 index 0000000..1fd564a --- /dev/null +++ b/C++/test.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; +struct node +{ + int data; + node *next; +}; +int *head==NULL; +//inserting elements +void insert(int value){ + node *ptr=new node; +} +int main(){ + +} \ No newline at end of file diff --git a/C++/test1.cpp b/C++/test1.cpp new file mode 100644 index 0000000..9c11dc5 --- /dev/null +++ b/C++/test1.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +struct Node +{ + int data; + Node* next; +}; +Node* deleteFirstNode(Node* head){ + if(head==NULL){ + return NULL; + } + //move the head pointer to next node + Node* tempNode=head; + head=head->next; + delete tempNode; + + return head; +} + +Node* removeLastNode(Node* head){ + if(head==NULL){ + return NULL; + } + if(head->next==NULL){ + delete head; + return NULL; + } + //first find second last node + Node* second_last=head; + while(second_last->next->next!=NULL) + second_last=second_last->next; +} \ No newline at end of file diff --git a/C++/tree1.cpp b/C++/tree1.cpp new file mode 100644 index 0000000..fec3b81 --- /dev/null +++ b/C++/tree1.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +struct node{ + int data; + struct node *left; + struct node *right; + + node(int val) //val that is store in data part + { + data=val; + left=NULL; + right=NULL; + } +}; + +int main() +{ + struct node* root = new node(1); + root->left = new node(2); + root->right = new node(3); + root->left->left = new node(4); + + return 0; +} \ No newline at end of file diff --git a/C++/vector.cpp b/C++/vector.cpp new file mode 100644 index 0000000..ff20c7a --- /dev/null +++ b/C++/vector.cpp @@ -0,0 +1,42 @@ +#include +#include +using namespace std; + +void disVector(vector A){ + vector::iterator it; + for(it=A.begin();it!=A.end();it++){ + cout<<*it<<" "; + cout<<"\n"; + } +} +int main(){ + vector A(4,0); + for(int i=0;i<4;i++){ + cin>>A[i]; + } + disVector(A); + + cout<<"Adding 5 in the end\n"; + A.push_back(5); + disVector(A); + + cout<<"Size:"<