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 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) diff --git a/Data Structures/Linked List/SinglyLinkedLIst2.cpp b/Data Structures/Linked List/SinglyLinkedLIst2.cpp index c9175d86..4ef316ea 100755 --- a/Data Structures/Linked List/SinglyLinkedLIst2.cpp +++ b/Data Structures/Linked List/SinglyLinkedLIst2.cpp @@ -1,122 +1,154 @@ -#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; + Node* temp = *head_ref; + cout<<"Top element: "<data<link; - temp->link=temp2->link; - temp2=NULL; + Node* temp = *head_ref; + cout<<"Deleted Element: "<data<next; + + if(*head_ref == NULL) + *tail = NULL; } -void display() -{ - Node* temp=A; - while(temp!=NULL) - { - cout<<"->"<data; - temp=temp->link; + +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 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) - { - int ch; - cout<<"Enter your choice:\n1-Insert\n2-Delete\n3-Display\n4-Reverse\n5-Count\n6-Exit\n->"; + +// bool find(int key){ + +// } + +int main(){ + Node *head = NULL; + Node *tail = NULL; + while(1){ + 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; - switch(ch) - { + 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 8: + printList(head); + break; + case 9: exit(0); } } + + } +