From 817490050c41a4058abb6dd0bed6dad11d996642 Mon Sep 17 00:00:00 2001 From: Samyak Jain <44099124+Samyak2607@users.noreply.github.com> Date: Fri, 4 Oct 2019 01:46:21 +0530 Subject: [PATCH 1/5] Update SinglyLinkedLIst2.cpp --- .../Linked List/SinglyLinkedLIst2.cpp | 211 ++++++++++-------- 1 file changed, 122 insertions(+), 89 deletions(-) diff --git a/Data Structures/Linked List/SinglyLinkedLIst2.cpp b/Data Structures/Linked List/SinglyLinkedLIst2.cpp index c9175d86..78ca52dd 100755 --- a/Data Structures/Linked List/SinglyLinkedLIst2.cpp +++ b/Data Structures/Linked List/SinglyLinkedLIst2.cpp @@ -1,122 +1,155 @@ -#include "iostream" -#include "cstdlib" +#include + using namespace std; -struct Node -{ + +class Node{ + public: int data; - Node* link; + Node *next; }; -Node* A; -void insert() -{ - int n,p; - cout<<"Enter the no to be Inserted:"; - cin>>n; - cout<<"Enter the position:"; - cin>>p; - Node* temp=new Node; - if (p==1) - { - temp->link=A; - temp->data=n; - A=temp; - return; + +void PushFront(Node** head_ref, Node** tail, int key){ + Node* newnode = new Node(); + newnode->next = *head_ref; + newnode->data = key; + + if(*tail == NULL){ + *tail = newnode; } - Node* temp2=A; - for(int i=1;ilink; + + *head_ref = newnode; +} + +void AddAfter(Node* pre_node,Node** tail, int key){ + if(pre_node == NULL){ + cout<<"Node can't be null\n"; + return; } - temp->link=temp2->link; - temp2->link=temp; - temp->data=n; + Node* newnode = new Node(); + newnode->data = key; + newnode->next = pre_node->next; + pre_node->next = newnode; + + if(pre_node == *tail) + *tail = newnode; } -void delet() -{ - int p; - cout<<"Enter the position of the element to be deleted:"; - cin>>p; - Node* temp=A; - if (p==1) - { - A=temp->link; - temp=NULL; + +void TopFront(Node** head_ref){ + if(*head_ref == NULL){ + cout<<"List is Empty\n"; return; } - Node* temp2=new Node; - for(int i=1;ilink; + cout<<"Top element: "<<*head_ref->data<link; - temp->link=temp2->link; - temp2=NULL; + cout<<"Deleted Element: "<<*head_ref->data<next; + + if(*head_ref == NULL) + *tail = NULL; + +} +void PushBack(Node** head_ref, Node** tail, int key){ + Node* newnode = new Node(); + newnode->data = key; + newnode->next = NULL; + + if(*tail == NULL){ + *head_ref = newnode; + *tail = newnode; + } + else{ + Node* temp = *tail; + temp->next = newnode; + *tail = newnode; + } } -void display() -{ - Node* temp=A; - while(temp!=NULL) - { - cout<<"->"<data; - temp=temp->link; + +void printList(Node* node){ + if(node == NULL){ + cout<<"Error: List is Empty"<next != NULL){ + cout<data<<" "; + node=node->next; } - cout<<"\n"<<"---------------------------"<<"\n"; } -void count() -{ - int c=0; - Node* temp=A; - while(temp!=NULL) - { - c++; - temp=temp->link; +void TopBack(Node** tail){ + if(*tail == NULL){ + cout<<"Error: List is Empty\n"; + return; } - cout<data<link; - while(prev!=NULL) - { - temp2=prev->link; - prev->link=temp; - temp=prev; - prev=temp2; + +void PopBack(Node** head_ref, Node** tail){ + if(*head_ref == NULL){ + cout<<"Error: List is Empty\n"; + return; } - A->link=NULL; - A=temp; + Node* p=*head_ref; + while(*p->next->next != NULL); + p->next=NULL; } -int main() -{ - A=NULL; - while(1) - { + +// bool find(int key){ + +// } + +int main(){ + Node *head = NULL; + Node *tail = NULL; + while(1){ int ch; - cout<<"Enter your choice:\n1-Insert\n2-Delete\n3-Display\n4-Reverse\n5-Count\n6-Exit\n->"; - cin>>ch; - switch(ch) - { + cout<<"Enter your choice:\n1-Insert at front\n2-Insert at back\n3-Insert After\n4-delete from front\n5-delete from back\n6-First Element\n7-Last Element\n8-PrintList\n9-Exit->"; + cin>>ch, num; + switch(ch){ case 1: - insert(); + cout<<"Enter the Element: "; + cin>>num; + PushFront(&head, &tail, num); break; case 2: - delet(); + cout<<"Enter the Element: "; + cin>>num; + PushBack(&head, &tail, num); break; case 3: - display(); + cout<<"Enter the Element: "; + cin>>num; + AddAfter(head->next, &tail, num); break; case 4: - rev(); + PopFront(&head, &tail); break; case 5: - count(); + PopBack(&head, &tail); break; case 6: + TopFront(&head); + break; + case 7: + TopBack(&tail); + break; + case 5: + printList(head); + break; + case 9: exit(0); } } -} + PushBack(&head, &tail, 6); + PushFront(&head, &tail, 7); + PushFront(&head, &tail, 1); + PushBack(&head, &tail, 4); + AddAfter(head->next, &tail, 8); + + printList(head); +} From bf59d6694cb6ad338f36e0a6c69865d65e19a2c5 Mon Sep 17 00:00:00 2001 From: Samyak Jain <44099124+Samyak2607@users.noreply.github.com> Date: Fri, 4 Oct 2019 01:50:15 +0530 Subject: [PATCH 2/5] Update SinglyLinkedLIst2.cpp --- Data Structures/Linked List/SinglyLinkedLIst2.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Data Structures/Linked List/SinglyLinkedLIst2.cpp b/Data Structures/Linked List/SinglyLinkedLIst2.cpp index 78ca52dd..a25eb683 100755 --- a/Data Structures/Linked List/SinglyLinkedLIst2.cpp +++ b/Data Structures/Linked List/SinglyLinkedLIst2.cpp @@ -145,11 +145,5 @@ int main(){ } } - PushBack(&head, &tail, 6); - PushFront(&head, &tail, 7); - PushFront(&head, &tail, 1); - PushBack(&head, &tail, 4); - AddAfter(head->next, &tail, 8); - printList(head); } From 573e8ea81378c01df3c990343584b9cc0bbf483b Mon Sep 17 00:00:00 2001 From: Samyak Jain <44099124+Samyak2607@users.noreply.github.com> Date: Fri, 4 Oct 2019 01:58:32 +0530 Subject: [PATCH 3/5] Update SinglyLinkedLIst2.cpp --- .../Linked List/SinglyLinkedLIst2.cpp | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Data Structures/Linked List/SinglyLinkedLIst2.cpp b/Data Structures/Linked List/SinglyLinkedLIst2.cpp index a25eb683..4ef316ea 100755 --- a/Data Structures/Linked List/SinglyLinkedLIst2.cpp +++ b/Data Structures/Linked List/SinglyLinkedLIst2.cpp @@ -39,7 +39,8 @@ void TopFront(Node** head_ref){ cout<<"List is Empty\n"; return; } - cout<<"Top element: "<<*head_ref->data<data<data<next; + Node* temp = *head_ref; + cout<<"Deleted Element: "<data<next; if(*head_ref == NULL) *tail = NULL; @@ -85,7 +87,8 @@ void TopBack(Node** tail){ cout<<"Error: List is Empty\n"; return; } - cout<<"Last Element: "<<*tail->data<data<next->next != NULL); + while(p->next->next != NULL); p->next=NULL; } @@ -106,9 +109,9 @@ int main(){ Node *head = NULL; Node *tail = NULL; while(1){ - int ch; + int ch, num; cout<<"Enter your choice:\n1-Insert at front\n2-Insert at back\n3-Insert After\n4-delete from front\n5-delete from back\n6-First Element\n7-Last Element\n8-PrintList\n9-Exit->"; - cin>>ch, num; + cin>>ch; switch(ch){ case 1: cout<<"Enter the Element: "; @@ -137,7 +140,7 @@ int main(){ case 7: TopBack(&tail); break; - case 5: + case 8: printList(head); break; case 9: @@ -147,3 +150,5 @@ int main(){ } + + From 76930f354d02eec7311880fb5abaf18699371b3f Mon Sep 17 00:00:00 2001 From: = Date: Fri, 30 Oct 2020 21:28:19 +0530 Subject: [PATCH 4/5] update bitmanipulation --- BitManipulation/count_ones/count_ones.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/BitManipulation/count_ones/count_ones.py b/BitManipulation/count_ones/count_ones.py index 54864751..9291794b 100644 --- a/BitManipulation/count_ones/count_ones.py +++ b/BitManipulation/count_ones/count_ones.py @@ -1,9 +1,7 @@ -def count_ones(n: int): +def count_ones(n): count = 0 - while n: - if n & 1: - count += 1 - n >>= 1 + n=bin(n).split('b')[1] + count=n.count('1') return count From 4780c33d98dbc1e0111f6dbb8e7112f10a43390b Mon Sep 17 00:00:00 2001 From: = Date: Fri, 30 Oct 2020 22:19:29 +0530 Subject: [PATCH 5/5] reverse_32_bit_manipulation --- BitManipulation/reverse_bits/reverseBits.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 BitManipulation/reverse_bits/reverseBits.py diff --git a/BitManipulation/reverse_bits/reverseBits.py b/BitManipulation/reverse_bits/reverseBits.py new file mode 100644 index 00000000..f1c1072c --- /dev/null +++ b/BitManipulation/reverse_bits/reverseBits.py @@ -0,0 +1,15 @@ +def reverse_32_bit(n): + n=bin(n).split('b')[1] + if len(n)<32: + temp=32-len(n) + s='' + for i in range(temp): + s+='0' + n=s+n + n=n[::-1] + return int(n,2) + +for _ in range(int(input())): + n=int(input()) + result=reverse_32_bit(n) + print(result)